How to open a file through ShellExecute in parent directory?

For everyone, just starting with C++ or programming at all. Ask newbie questions in this forum!

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

How to open a file through ShellExecute in parent directory?

Postby alienoiz » Tue Apr 24, 2012 7:40 pm

hi guys,

i would like to know if its possible to open an external .exe file through shellexecute without having to type the whole path to it?....just run it from the parent directory where is my main app...

Thanks!

Duarte V.
alienoiz
 
Posts: 38
Joined: Tue Apr 24, 2012 6:02 am

Re: How to open a file through ShellExecute in parent directory?

Postby exomo » Wed Apr 25, 2012 12:24 am

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.
Who needs a signature anyway.
User avatar
exomo
 
Posts: 935
Joined: Fri Sep 26, 2003 12:30 pm
Location: germany->baden

Re: How to open a file through ShellExecute in parent directory?

Postby alienoiz » Wed Apr 25, 2012 7:02 am

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++!
alienoiz
 
Posts: 38
Joined: Tue Apr 24, 2012 6:02 am

Re: How to open a file through ShellExecute in parent directory?

Postby alienoiz » Wed Apr 25, 2012 8:21 am

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++!


im not get it very well...how do i append my external exe file into the GetModuleFileName() function
This should gimme the path to my main application right:

Code: Select all
DWORD WINAPI GetModuleFileName;
HMODULE hModule;
PTSTR lpFilename;
DWORD nSize;

DWORD WINAPI GetModuleFileName(NULL);




now what do i need to write in order to this work:

Code: Select all

int Open = (int)ShellExecute(NULL, L"open", L"Test.exe", NULL, NULL, SW_SHOWNORMAL );



Thanks!

Duarte V.
alienoiz

Posts: 5
Joined: Tue Apr 24, 2012 6:02 am
alienoiz
 
Posts: 38
Joined: Tue Apr 24, 2012 6:02 am

Re: How to open a file through ShellExecute in parent directory?

Postby exomo » Wed Apr 25, 2012 11:33 am

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.
Who needs a signature anyway.
User avatar
exomo
 
Posts: 935
Joined: Fri Sep 26, 2003 12:30 pm
Location: germany->baden

Re: How to open a file through ShellExecute in parent directory?

Postby alienoiz » Wed Apr 25, 2012 11:43 am

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!
alienoiz
 
Posts: 38
Joined: Tue Apr 24, 2012 6:02 am

Re: How to open a file through ShellExecute in parent directory?

Postby alienoiz » Wed Apr 25, 2012 1:06 pm

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!



i think i cannot do this....i was not completely clear with my question....well... what is happenning is that im creating a .dll file that is built with a .sem extension to use in a visual programming software as a module of it (this program works with modules that are .dll files converted into .sem files wich is the extension used by it to load them)
....so basically im not executing an .exe file as main file.... the module inside the program is a .dll file and what i need is that this module/dll opens my external Test.exe file.....
and.... unfortunely i dont know the basis of C++ so if you could help me giving me the correct code for visual studio i would be very gratefull....
i saw a tuturial to open a search box for folders and use hFind etc etc... this worked...i could open my test.exe by searching for the folder where it is and handle it with hFind, then use shellexecute... but... what i realy need is to open it as soon as i click in a button of the .dll/.sem module that the other software uses!

Thanks!
alienoiz
 
Posts: 38
Joined: Tue Apr 24, 2012 6:02 am

Re: How to open a file through ShellExecute in parent directory?

Postby alienoiz » Wed Apr 25, 2012 2:21 pm

sorry to bother again ....but the "path.c_str" value on the line

Code: Select all
int Open = (int)ShellExecute(NULL, L"open", path.c_str(), NULL, NULL, SW_SHOWNORMAL );


is giving me error:


1 IntelliSense: argument of type "const char *" is incompatible with parameter of type "LPCWSTR" c:\users\duarte\documents\synthedit projects\float\float.cpp 69
(related to the word "path")

any help?
Thanks!
alienoiz
 
Posts: 38
Joined: Tue Apr 24, 2012 6:02 am

Re: How to open a file through ShellExecute in parent directory?

Postby exomo » Wed Apr 25, 2012 3:26 pm

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.
Who needs a signature anyway.
User avatar
exomo
 
Posts: 935
Joined: Fri Sep 26, 2003 12:30 pm
Location: germany->baden

Re: How to open a file through ShellExecute in parent directory?

Postby alienoiz » Wed Apr 25, 2012 3:47 pm

well...the wchar word is unrecognize :s

Thanks again!
alienoiz
 
Posts: 38
Joined: Tue Apr 24, 2012 6:02 am

Re: How to open a file through ShellExecute in parent directory?

Postby alienoiz » Wed Apr 25, 2012 4:01 pm

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.


Code: Select all
      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 );


it does not recognize anyway the value path.c_srt()

:(

Thanks for your help!
alienoiz
 
Posts: 38
Joined: Tue Apr 24, 2012 6:02 am

Re: How to open a file through ShellExecute in parent directory?

Postby exomo » Wed Apr 25, 2012 4:38 pm

It does not work the way you changed it. You must pass the buffer to GetModuleFileName, passing a string literal is a very bad idea. And you didn't change string to wstring as I told you. And you initialize the string with "260", why that?
I just tried the code in my Visual Studio and it works. What error message do you get for path.c_str()?
Who needs a signature anyway.
User avatar
exomo
 
Posts: 935
Joined: Fri Sep 26, 2003 12:30 pm
Location: germany->baden

Re: How to open a file through ShellExecute in parent directory?

Postby alienoiz » Wed Apr 25, 2012 4:56 pm

ok...everything looks good now excpet one thing:

Code: Select all
          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 );


here:

Code: Select all
path.find_last_of


The "." gives me this error:


2 IntelliSense: no instance of overloaded function "std::basic_string<_Elem, _Traits, _Ax>::find_last_of [with _Elem=wchar_t, _Traits=std::char_traits<wchar_t>, _Ax=std::allocator<wchar_t>]" matches the argument list c:\users\duarte\documents\synthedit projects\float\float.cpp 57
alienoiz
 
Posts: 38
Joined: Tue Apr 24, 2012 6:02 am

Re: How to open a file through ShellExecute in parent directory?

Postby alienoiz » Wed Apr 25, 2012 4:58 pm

sorry..i dont understand nothing about this....i just limit myself to try to understand codes that i find in tuturials and apply them to my needs... i never code ..this is an one of the execptions cause i need to build this .dll and realy need c++.....i just know a bit of VB nothing more...
alienoiz
 
Posts: 38
Joined: Tue Apr 24, 2012 6:02 am

Re: How to open a file through ShellExecute in parent directory?

Postby alienoiz » Wed Apr 25, 2012 6:20 pm

i dont know what to do with this "dot"...i spent 30 minutes try various forms of code in this simple piece, plus 15 minutes lokking to the "dot" if any idea would come but nooooooo....sorry man.... i realy need help on this :s...and im starting to get bored because i cannot solve my piece of code :(
alienoiz
 
Posts: 38
Joined: Tue Apr 24, 2012 6:02 am

Next

Return to For Beginners

Who is online

Users browsing this forum: No registered users and 2 guests