ShellExecute() not working with .bat file

2020-07-26 07:18发布

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.

标签: c++ winapi
4条回答
ゆ 、 Hurt°
2楼-- · 2020-07-26 07:22

Try this:

//#include "Shellapi.h"
#include "Windows.h"
int _tmain(int argc, _TCHAR* argv[])
{
  HINSTANCE hReturnCode=ShellExecute(NULL, _T("open"), _T("cmd.exe"), _T("/C sa.bat"), NULL, SW_SHOWNORMAL);
  DWORD LastError = GetLastError();
  return 0;
}

[EDITED] This working suggestion is from Chris:

    //#include "Shellapi.h"
    #include "Windows.h"
    int _tmain(int argc, _TCHAR* argv[])
    {
      HINSTANCE hReturnCode=ShellExecute(NULL, _T("open"), _T("sa.bat"), NULL, NULL, SW_SHOWNORMAL);
      DWORD LastError = GetLastError();
      return 0;
    }
查看更多
聊天终结者
3楼-- · 2020-07-26 07:22

ShellExecute() reports errors via its own return value, not via GetLastError(). You also need to pass the filename in the lpFile parameter, not the lpOperation parameter.

Try this:

//#include "Shellapi.h"
#include "Windows.h"
int _tmain(int argc, _TCHAR* argv[])
{
    int nErrorCode = (int) ShellExecute(NULL, NULL, "sa.bat", NULL, NULL, SW_SHOWNORMAL);
    if (nErrorCode <= 32)
      // an error occurred...
    return 0;
}
查看更多
叼着烟拽天下
4楼-- · 2020-07-26 07:24

.BAT files are not executable binaries, so batch file alone does not take off, instead it is started with command interpreter (CMD.EXE). On ShellExecute you either start it with CMD /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 to ShellExecute API, not to the contents of your batch file.

SE_ERR_NOASSOC

There is no application associated with the given file name extension. This error will also be returned if you attempt to print a file that is not printable.

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".

查看更多
Viruses.
5楼-- · 2020-07-26 07:35

This will also work:

#include <windows.h>
#include <stdio.h>
int main()
{
   STARTUPINFO si;
   PROCESS_INFORMATION pi;

   ZeroMemory( &si, sizeof(si) );
   si.cb = sizeof(si);
   ZeroMemory( &pi, sizeof(pi) );
   if( !CreateProcess( NULL, "cmd /C  sa.bat", NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)  )
   {
     printf( "CreateProcess failed (%d)\n", GetLastError() );
     return FALSE;
   }
   WaitForSingleObject( pi.hProcess, INFINITE );
   CloseHandle( pi.hProcess );
   CloseHandle( pi.hThread );
   return 0;
}
查看更多
登录 后发表回答