I trying to execute following bat file in c++ using win32 api.Following is code for execution.
//#include "Shellapi.h"
#include "Windows.h"
int _tmain(int argc, _TCHAR* argv[])
{
ShellExecute(GetDesktopWindow(), "sa.bat","", NULL, NULL, SW_SHOWNORMAL);
DWORD LastError = GetLastError();
return 0;
}
but it is not working.
following is content "sa.bat"
C:\windows\system32\wusa /uninstall /kb:2718695 /quiet /forcerestart
but last error returns 1155.
Try this:
[EDITED] This working suggestion is from Chris:
ShellExecute()
reports errors via its own return value, not viaGetLastError()
. You also need to pass the filename in thelpFile
parameter, not thelpOperation
parameter.Try this:
.BAT
files are not executable binaries, so batch file alone does not take off, instead it is started with command interpreter (CMD.EXE). OnShellExecute
you either start it withCMD /C
directly, or you leverage registry assotiation which starts.BAT
file with an "open" verb.See:
You apparently don't attempt to start CMD.EXE, so the association.
The error code you get is
ERROR_NO_ASSOCIATION
"No application is associated with the specified file for this operation." and it refers toShellExecute
API, not to the contents of your batch file.This means that
ShellExecute
could not figure out your arguments. The file should go third, not second, and then second argument would be "open" or NULL (verb). Applying the verb onto.BAT
file would make the.BAT
file "run".This will also work: