# Win32 OPENFILENAME filters (conversion from std::string to LPCSTR)

This topic is 3475 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm trying to use an OPENFILENAME structure for use in GetOpenFileName. The problem i'm having is with seting the filter for the allowed file types. The filter paramater takes a LPCSTR When I set the filter to : TEXT("Images (.bmp .dds)\0*.bmp;*.dds;\0Bitmap\0*.bmp\0DDS Document\0*.dds\0\0"); Everything works fine. The thing bothering me at the moment is that I'm having no luck converting from std::string to LPCSTR, mainly because std::string dosen't seem to allow the '\0' delimitating character in them. Basically I've had no luck with using += or + for the \0 From searching I found out that the push_back function would work to add the \0
		std::string filter = fileTypeDescription;
filter.push_back('\0');
filter.push_back('*');
for(UINT i = 0; i < accepptedFileType.size(); i++)
{
filter.push_back(accepptedFileType);
}
filter.push_back('\0');
filter.push_back('\0');


The code compiles just fine, but sets the filter to jibberish. In debugging the string 'filter' looks fine, but when in the dialog it is seemingly random characters (not letters/numbers) I'm doing this so that I can put the open file dialog into functions that allow the programmer to specify any filters. Does anyone have any idea on how to fix this, or a better way of doing this? Thanks in advance!

##### Share on other sites
Write a function that accepts a vector of strings and iterates through them to add the null characters yourself. That way you maintain a high level view and can still build up the character string properly.

##### Share on other sites
I have a function that allows the addition of new file types and their descriptons, I could change it to a vector, but prefer it the way it is for now.

The problem I have is really the conversion to LPCSTR, nothing i've tried has worked successfully.
It's the null characters that seem to mess things up.

Again if anyone can see something i'm doing wrong, or knows of a better way of doing this it would be great.

Thanks!

##### Share on other sites
Quote:
 Original post by p0is0nI have a function that allows the addition of new file types and their descriptons, I could change it to a vector, but prefer it the way it is for now.The problem I have is really the conversion to LPCSTR, nothing i've tried has worked successfully.It's the null characters that seem to mess things up.Again if anyone can see something i'm doing wrong, or knows of a better way of doing this it would be great.Thanks!
You can't. std::string doesn't support null characters, it also doesn't support unicode characters - it's just a limitation.

If you need null characters, you'll have to use a std::vector<char>, or a fixed size char array.

##### Share on other sites
What are you using to convert the std::string to an LPCSTR? Perhaps a call to .data(); would work better?

##### Share on other sites
The only way I was able to get what (I think) you're trying to do to work is by using this method:
//Filter is a std::string that is setup like so: name|ext|name|ext|etc|// so for example: "C++ Source Files|*.cpp|C Header Files|*.h|"char FilterBuffer[SomeBufferSizeHere];int Index = 0;for(Index = 0; Index < Filter.length() && Index < SomeBufferSize; ++Index){    if(Filter[Index] == '|')        FilterBuffer[Index] = '\0';    else        FilterBuffer[Index] = Filter[Index];}FilterBuffer[Index] = '\0';OpenFileName.lpstrFilter = FilterBuffer;

To expand on what was said about the vector:
std::vector<char> FilterBuffer;for(std::string::const_iterator Itor = Filter.begin(); Itor != Filter.end(); ++Itor){    if(*Itor == '|')        FilterBuffer.push_back('\0');    else        FilterBuffer.push_back(*Itor);}FilterBuffer.push_back('\0');/*You should probably ensure that there are 2 null characters here.I don't do so in my code, but that's because I always make sureto have '|' as my last character and the system does the work for me.*/OpenFileName.lpstrFilter = &(FilterBuffer[0]);

The first source is pretty much exactly what I use for my stuff and I've had no problems with it thus far.

HTH!

##### Share on other sites
Quote:
 Original post by Programmer16The only way I was able to get what (I think) you're trying to do to work is by using this method:*** Source Snippet Removed ***To expand on what was said about the vector:*** Source Snippet Removed ***The first source is pretty much exactly what I use for my stuff and I've had no problems with it thus far.HTH!
That loop looks a little dodgey to me - you're modifying the vector while iterating through it. If the vector needs to resize as a result of a push_back(), you'll invalidate the iterator and crash.

Never mind, you're iterating over another container. Ignore me [smile]

##### Share on other sites
Thanks a lot for all of the replies.

I have now got it working using the vector method described by Programmer16.
I have never come across a limitation of std::string before which is probably why i thought i was missing something and didn't look for alternative methods to using it. Guess there is a first time for everything.

Thanks again for all of the help!

1. 1
2. 2
3. 3
4. 4
Rutin
16
5. 5

• 12
• 9
• 12
• 37
• 12
• ### Forum Statistics

• Total Topics
631417
• Total Posts
2999970
×