Reputation: 828
I have a self registering COM visible dll (C++) installed in a folder in Program Files along with an appref-ms file that points at a click once application. The purpose of this dll is to allow the click once app to be launched from the right click menu in windows explorer.
I am modifying existing code that uses wchar_t* exclusively to hold strings, and I have very limited experience with C++. I am using the following to get the path to the the folder that contains the dll and appref-ms.
std::wstring DllFolder() {
wchar_t buffer[MAX_PATH];
GetModuleFileName( (HINSTANCE)&__ImageBase, buffer, MAX_PATH );
std::wstring::size_type pos = std::wstring( buffer ).find_last_of( L"\\/" );
return std::wstring( buffer ).substr( 0, pos);
}
...
const wchar_t* folder = DllFolder().c_str();
This almost works, but the drive letter is difference every time. Examples:
{:\Projects\MyAppName\x64\Release e:\Projects\MyAppName\x64\Release O:\Projects\MyAppName\x64\Release
Sometimes there is a new line after the : and no letter.
Another question: I hold the launch string in a class member wchar_t m_Launch[MAX_PATH*10]
. Since I don't know how many files the user will select, is there a way to dynamically resize it?
Upvotes: 0
Views: 313
Reputation: 5054
Function DLLFolder()
returns temporary object. When you got a pointer to it's data (via folder = DLLFolder().c_str()
) you got a pointer to symbol array, that will be destroyed when destructor of wstring
will be called (it would be at the next line). The solutions are:
Don't get a pointer, but use a copy of wstring
:
std::wstring folder = DLLFolder();
Forward pointer to function, that need it, in the same call:
do_smth_with_dll_folder( DllFolder().c_str() )
Upvotes: 1