Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard
exomo wrote:Of course this is possible. You can get the path of your application by using the GetModuleFileName() function: http://msdn.microsoft.com/en-us/library/windows/desktop/ms683197%28v=vs.85%29.aspx
From that filename you can remove the last part which is the actual filename to get the path, if want to have the parent directory just remove the last portion of the pathname again. Append the name of the file you want to run and give it to ShellExecute.
alienoiz wrote:exomo wrote:Of course this is possible. You can get the path of your application by using the GetModuleFileName() function: http://msdn.microsoft.com/en-us/library/windows/desktop/ms683197%28v=vs.85%29.aspx
From that filename you can remove the last part which is the actual filename to get the path, if want to have the parent directory just remove the last portion of the pathname again. Append the name of the file you want to run and give it to ShellExecute.
Thank you...im gonna check if i can along with this im a real newbie on C++!
DWORD WINAPI GetModuleFileName;
HMODULE hModule;
PTSTR lpFilename;
DWORD nSize;
DWORD WINAPI GetModuleFileName(NULL);
int Open = (int)ShellExecute(NULL, L"open", L"Test.exe", NULL, NULL, SW_SHOWNORMAL );
char buffer[MAX_PATH]; // define a char buffer to get the filename
GetModuleFileName(NULL, buffer, MAX_PATH); // get the name of the current executable
string path(buffer); // create a c++ string from the buffer
path = path.substr(0, path.find_last_of("\\")); // remove the filename, only keep the path
// repeat the line above to go one directory up
path = path + "\\Test.exe"; // append the name of the executable file
int Open = (int)ShellExecute(NULL, "open", path.c_str(), NULL, NULL, SW_SHOWNORMAL );exomo wrote:Here is an example how you can start a Test.exe in the same directory as your program.
- Code: Select all
char buffer[MAX_PATH]; // define a char buffer to get the filename
GetModuleFileName(NULL, buffer, MAX_PATH); // get the name of the current executable
string path(buffer); // create a c++ string from the buffer
path = path.substr(0, path.find_last_of("\\")); // remove the filename, only keep the path
// repeat the line above to go one directory up
path = path + "\\Test.exe"; // append the name of the executable file
int Open = (int)ShellExecute(NULL, "open", path.c_str(), NULL, NULL, SW_SHOWNORMAL );
I used the normal character versions, it looks like you are using visual studio so you have to convert the strings to wide character strings if you want to use them.
alienoiz wrote:exomo wrote:Here is an example how you can start a Test.exe in the same directory as your program.
- Code: Select all
char buffer[MAX_PATH]; // define a char buffer to get the filename
GetModuleFileName(NULL, buffer, MAX_PATH); // get the name of the current executable
string path(buffer); // create a c++ string from the buffer
path = path.substr(0, path.find_last_of("\\")); // remove the filename, only keep the path
// repeat the line above to go one directory up
path = path + "\\Test.exe"; // append the name of the executable file
int Open = (int)ShellExecute(NULL, "open", path.c_str(), NULL, NULL, SW_SHOWNORMAL );
I used the normal character versions, it looks like you are using visual studio so you have to convert the strings to wide character strings if you want to use them.
tkx im gonna try!
int Open = (int)ShellExecute(NULL, L"open", path.c_str(), NULL, NULL, SW_SHOWNORMAL );exomo wrote:That's exactly what i have expected. the function needs a wide character string. Change the type string to wstring, char to wchar. But I'm not exactly sure if this works or you have to change something else, too. I don't have VS here so I can't test it.
It shouldn't make any difference if you call the GetModuleFileName from exe or dll.
WCHAR buffer[MAX_PATH];
GetModuleFileName(NULL, L"260", MAX_PATH); // get the name of the current executable
std::string path("260"); // create a c++ string from the buffer
path = path.substr(0, path.find_last_of("\\")); // remove the filename, only keep the path
// repeat the line above to go one directory up
path = path + "\\Test.exe"; // append the name of the executable file
int Open = (int)ShellExecute(NULL, L"open", path.c_str(), NULL, NULL, SW_SHOWNORMAL ); WCHAR buffer[MAX_PATH];
GetModuleFileName(NULL, buffer, MAX_PATH); // get the name of the current executable
std::wstring path(buffer); // create a c++ string from the buffer
path = path.substr(0, path.find_last_of("\\")); // remove the filename, only keep the path
// repeat the line above to go one directory up
path = path + L"\\Test.exe"; // append the name of the executable file
int Open = (int)ShellExecute(NULL, L"open", path.c_str(), NULL, NULL, SW_SHOWNORMAL );path.find_last_ofUsers browsing this forum: No registered users and 0 guests