Sign in to follow this  
p0is0n

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

Recommended Posts

p0is0n    100
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[i]);
		}
		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 this post


Link to post
Share on other sites
p0is0n    100
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 this post


Link to post
Share on other sites
Evil Steve    2017
Quote:
Original post by p0is0n
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!
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 this post


Link to post
Share on other sites
Programmer16    2321
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 sure
to 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 this post


Link to post
Share on other sites
Evil Steve    2017
Quote:
Original post by Programmer16
The 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 this post


Link to post
Share on other sites
p0is0n    100
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!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this