Application crashes when trying to get string::substr

I have this function which is supposed to get me the directory of the application. I am using UNICODE, that's why I use wstring (always somebody must ask things that have nothing to do with my problem)

1
2
3
4
5
6
7
8
wstring Location() {
    wstring fulllocation;
    wstring location;
    GetModuleFileName(NULL, (WCHAR*)fulllocation.c_str(), MAX_PATH);
    int last_slash = fulllocation.find_last_of(L"/\\");
    location = fulllocation.substr(0, last_slash);
    return location;
}


The application crashes when it tries to get the sub-string with substr() and I don't understand what is wrong. Thanks for your help!
The problem resides in find_last_of((WCHAR*)"/\\"); It returns -1
That is a brain-dead function mate.

GetModuleFileName(NULL, (WCHAR*)fulllocation.c_str(), MAX_PATH); //NO

There will also be no double slashes in the pathname - just single slashes
Last edited on
So, what is the correct one to use for this?
This should work

1
2
3
4
5
6
7
8
9
10
11
12
13
std::wstring Location() 
{
	wchar_t buff[MAX_PATH+10]={0};
    
    GetModuleFileNameW(NULL, buff, MAX_PATH);
    
	std::wstring fulllocation(buff);
  
	int last_slash = fulllocation.find_last_of(L"\\");
 	fulllocation = fulllocation.substr(0, last_slash);

	return fulllocation;
}
It did. Thanks!
Topic archived. No new replies allowed.