Enigma with ShellExecute ? [modified]
-
Hi there, I got the strangest problem. I want to start an executable with ShellExecute(). Which usually works fine, but ... (this is the code)
[edit]
char buf[512]
size = GetCurrentDirectoryA( 512, buf ); // get absolute path
[/edit]// version 1
char callString[1024];
sprintf_s( callString, "%s\\%s", buf, file.c_str() );
// version 2
char buf[1024];
sprintf_s( buf, "c:\\Users\\zwatschek\\work\\svn\\incubation\\ttsServer\\Data\\ttsWatchdog.exe" );char id[32];
sprintf_s( id, "%d", pid );long res=long(ShellExecute(0, "open", callString, id, 0, SW_SHOWNORMAL));
... with version 2 things work fine, with version 1 I get ERROR_FILE_NOT_FOUND. callString = c:\Users\zwatschek\work\svn\incubation\ttsServer\Data\ttsWatchdog.exe buf = c:\Users\zwatschek\work\svn\incubation\ttsServer\Data\ttsWatchdog.exe (I copied those strings right out from the variable watch) Does anyone have an idea on this? Souldrift
modified on Monday, October 5, 2009 5:00 AM
-
Hi there, I got the strangest problem. I want to start an executable with ShellExecute(). Which usually works fine, but ... (this is the code)
[edit]
char buf[512]
size = GetCurrentDirectoryA( 512, buf ); // get absolute path
[/edit]// version 1
char callString[1024];
sprintf_s( callString, "%s\\%s", buf, file.c_str() );
// version 2
char buf[1024];
sprintf_s( buf, "c:\\Users\\zwatschek\\work\\svn\\incubation\\ttsServer\\Data\\ttsWatchdog.exe" );char id[32];
sprintf_s( id, "%d", pid );long res=long(ShellExecute(0, "open", callString, id, 0, SW_SHOWNORMAL));
... with version 2 things work fine, with version 1 I get ERROR_FILE_NOT_FOUND. callString = c:\Users\zwatschek\work\svn\incubation\ttsServer\Data\ttsWatchdog.exe buf = c:\Users\zwatschek\work\svn\incubation\ttsServer\Data\ttsWatchdog.exe (I copied those strings right out from the variable watch) Does anyone have an idea on this? Souldrift
modified on Monday, October 5, 2009 5:00 AM
There's something wrong in the posted snippet:
Souldrift wrote:
// version 1 char callString[1024]; sprintf_s( callString, "%s\\%s", buf, file.c_str() );
Here you're using a uninitialised
buf
variable (as far as I can tell from the posted code...).Souldrift wrote:
har buf[1024]; sprintf_s( buf, "c:\\Users\\zwatschek\\work\\svn\\incubation\\ttsServer\\Data\\ttsWatchdog.exe" ); char id[32]; sprintf_s( id, "%d", pid ); long res=long(ShellExecute(0, "open", callString, id, 0, SW_SHOWNORMAL));
here you're copying (with a wrong call to
sprintf_s
- missinglen
parameter-) the file name to thebuf
variable, but, afterwards, you're not usingbuf
inShellExecute
. Please check through the posted code. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
There's something wrong in the posted snippet:
Souldrift wrote:
// version 1 char callString[1024]; sprintf_s( callString, "%s\\%s", buf, file.c_str() );
Here you're using a uninitialised
buf
variable (as far as I can tell from the posted code...).Souldrift wrote:
har buf[1024]; sprintf_s( buf, "c:\\Users\\zwatschek\\work\\svn\\incubation\\ttsServer\\Data\\ttsWatchdog.exe" ); char id[32]; sprintf_s( id, "%d", pid ); long res=long(ShellExecute(0, "open", callString, id, 0, SW_SHOWNORMAL));
here you're copying (with a wrong call to
sprintf_s
- missinglen
parameter-) the file name to thebuf
variable, but, afterwards, you're not usingbuf
inShellExecute
. Please check through the posted code. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]I edited the code block. buf was initialized, of course, further above. In ShellExecute() I tried 'callString' and 'buf' as parameter. But only 'buf' worked. 'buf' is in the example NOT in the ShellExecute(), since I tried last with 'callString'. hm ... I post the code differently ...
char buf[512]
size = GetCurrentDirectoryA( 512, buf ); // get absolute pathchar id[32];
sprintf_s( id, "%d", pid );// version 1
char callString[1024];
sprintf_s( callString, "%s\\%s", buf, file.c_str() );
long res=long(ShellExecute(0, "open", callString, id, 0, SW_SHOWNORMAL));// version 2
char callString2[1024];
sprintf_s( callString2, "c:\\Users\\zwatschek\\work\\svn\\incubation\\ttsServer\\Data\\ttsWatchdog.exe" );
res=long(ShellExecute(0, "open", callString2, id, 0, SW_SHOWNORMAL));And still, the contents of callString and callString2 are the same. Version 2 works, version 1 not. I´ll look into the len parameter of sprintf_s. But I don´t think that´s it. Thanks. Souldrift
modified on Monday, October 5, 2009 5:26 AM
-
I edited the code block. buf was initialized, of course, further above. In ShellExecute() I tried 'callString' and 'buf' as parameter. But only 'buf' worked. 'buf' is in the example NOT in the ShellExecute(), since I tried last with 'callString'. hm ... I post the code differently ...
char buf[512]
size = GetCurrentDirectoryA( 512, buf ); // get absolute pathchar id[32];
sprintf_s( id, "%d", pid );// version 1
char callString[1024];
sprintf_s( callString, "%s\\%s", buf, file.c_str() );
long res=long(ShellExecute(0, "open", callString, id, 0, SW_SHOWNORMAL));// version 2
char callString2[1024];
sprintf_s( callString2, "c:\\Users\\zwatschek\\work\\svn\\incubation\\ttsServer\\Data\\ttsWatchdog.exe" );
res=long(ShellExecute(0, "open", callString2, id, 0, SW_SHOWNORMAL));And still, the contents of callString and callString2 are the same. Version 2 works, version 1 not. I´ll look into the len parameter of sprintf_s. But I don´t think that´s it. Thanks. Souldrift
modified on Monday, October 5, 2009 5:26 AM
I made a test. Both
version 1
andversion 2
work fine on my system. Of course I had to provide a value for thefile
std::str
variable (and I used different path and executable). :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
I made a test. Both
version 1
andversion 2
work fine on my system. Of course I had to provide a value for thefile
std::str
variable (and I used different path and executable). :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
I found it. What a bugger. I got my file name from an absolute path like this
string path = wdPath.substr( 0, index+1 );
string file = wdPath.substr( index+1 );And though 'file' was displayed correctly in the watch, it seems to have contained some invalid sign. Cause like this ...
string path = wdPath.substr( 0, index+1 );
string file = wdPath.substr( index+1, wdPath.size()-index-2 );... it works. Cheers Souldrift