delphi中怎么用ShellExecuteEx执行一个bat文件,并等待其运行结束啊? 希望能给个简单的例子 谢谢了

时间:2021-03-31 17:34:41
delphi中怎么用ShellExecuteEx执行一个bat文件,并等待其运行结束啊? 希望能给个简单的例子

6 个解决方案

#1


sleep(2000);

#2



if WinExec(PChar(filename),SW_SHOWNORMAL) > 31 then
begin
  //--
end;

#3




function WinExecAndWait32_v1(FileName: string; Visibility: integer): Cardinal;
var
  zAppName: array[0..512] of char;
  zCurDir: array[0..255] of char;
  WorkDir: string;
  StartupInfo: TStartupInfo;
  ProcessInfo: TProcessInformation;
begin
  StrPCopy(zAppName, FileName);
  GetDir(0, WorkDir);
  StrPCopy(zCurDir, WorkDir);
  FillChar(StartupInfo, Sizeof(StartupInfo), #0);
  StartupInfo.cb := Sizeof(StartupInfo);
  StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
  StartupInfo.wShowWindow := Visibility;
  if not CreateProcess(nil,
    zAppName,               { pointer to command line string }
    nil,                    { pointer to process security attributes }
    nil,                    { pointer to thread security attributes }
    true,                   { handle inheritance flag }
    CREATE_NEW_CONSOLE or   { creation flags }
    NORMAL_PRIORITY_CLASS,
    nil,                    { pointer to new environment block }
    nil,                    { pointer to current directory name, PChar}
    StartupInfo,            { pointer to STARTUPINFO }
    ProcessInfo)            { pointer to PROCESS_INF }
    then Result := INFINITE {-1} else
  begin
    WaitforSingleObject(ProcessInfo.hProcess, INFINITE);
    GetExitCodeProcess(ProcessInfo.hProcess, Result);
    CloseHandle(ProcessInfo.hProcess);  { to prevent memory leaks }
    CloseHandle(ProcessInfo.hThread);
  end;
end;

//调用方法
WinExecAndWait32_v1('c:\a.bat ', SW_NORMAL);


#4


谢谢楼上的大哥 

结贴

#5


     
String strAttchPath ="";
        if(strDestIPAddress != "")
        {
              PROCESS_INFORMATION   pi;
              STARTUPINFO   si;
              si.cb   =   sizeof(STARTUPINFO);
              si.lpReserved   =   NULL;
              si.lpDesktop   =   NULL;
              si.lpTitle   =   NULL;
              si.dwFlags   =   0;
              si.cbReserved2   =   0;
              si.lpReserved2   =   NULL;
              String cmd = "";
              if(nDestPort != -1)
              {
                cmd = "cmd.exe /c .\\test.bat" + strDestIPAddress+" "+AnsiString(nDestPort)
                        +" "+Filename;
              }
              else
              {
                cmd = "cmd.exe /c .\\test.bat " + strDestIPAddress+" 80 "+Filename;
              }
              BOOL   bRet   =   CreateProcess(NULL,cmd.c_str(),NULL,NULL,false,
                                                      NORMAL_PRIORITY_CLASS,
                                                      NULL,NULL,&si,&pi);
            if(bRet == false)
            {
                  //Application->MessageBox("执行test.bat出错!","错误t",MB_OK);
            }
            else
            {
                CloseHandle(pi.hThread);
                DWORD   dwret=WaitForSingleObject(pi.hProcess,   INFINITE);  //INFINITE

                CloseHandle(pi.hProcess);
                strAttchPath = ExtractFilePath(Application->ExeName)+ Filename;
            }
        }

#6


dfdsfsdf

#1


sleep(2000);

#2



if WinExec(PChar(filename),SW_SHOWNORMAL) > 31 then
begin
  //--
end;

#3




function WinExecAndWait32_v1(FileName: string; Visibility: integer): Cardinal;
var
  zAppName: array[0..512] of char;
  zCurDir: array[0..255] of char;
  WorkDir: string;
  StartupInfo: TStartupInfo;
  ProcessInfo: TProcessInformation;
begin
  StrPCopy(zAppName, FileName);
  GetDir(0, WorkDir);
  StrPCopy(zCurDir, WorkDir);
  FillChar(StartupInfo, Sizeof(StartupInfo), #0);
  StartupInfo.cb := Sizeof(StartupInfo);
  StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
  StartupInfo.wShowWindow := Visibility;
  if not CreateProcess(nil,
    zAppName,               { pointer to command line string }
    nil,                    { pointer to process security attributes }
    nil,                    { pointer to thread security attributes }
    true,                   { handle inheritance flag }
    CREATE_NEW_CONSOLE or   { creation flags }
    NORMAL_PRIORITY_CLASS,
    nil,                    { pointer to new environment block }
    nil,                    { pointer to current directory name, PChar}
    StartupInfo,            { pointer to STARTUPINFO }
    ProcessInfo)            { pointer to PROCESS_INF }
    then Result := INFINITE {-1} else
  begin
    WaitforSingleObject(ProcessInfo.hProcess, INFINITE);
    GetExitCodeProcess(ProcessInfo.hProcess, Result);
    CloseHandle(ProcessInfo.hProcess);  { to prevent memory leaks }
    CloseHandle(ProcessInfo.hThread);
  end;
end;

//调用方法
WinExecAndWait32_v1('c:\a.bat ', SW_NORMAL);


#4


谢谢楼上的大哥 

结贴

#5


     
String strAttchPath ="";
        if(strDestIPAddress != "")
        {
              PROCESS_INFORMATION   pi;
              STARTUPINFO   si;
              si.cb   =   sizeof(STARTUPINFO);
              si.lpReserved   =   NULL;
              si.lpDesktop   =   NULL;
              si.lpTitle   =   NULL;
              si.dwFlags   =   0;
              si.cbReserved2   =   0;
              si.lpReserved2   =   NULL;
              String cmd = "";
              if(nDestPort != -1)
              {
                cmd = "cmd.exe /c .\\test.bat" + strDestIPAddress+" "+AnsiString(nDestPort)
                        +" "+Filename;
              }
              else
              {
                cmd = "cmd.exe /c .\\test.bat " + strDestIPAddress+" 80 "+Filename;
              }
              BOOL   bRet   =   CreateProcess(NULL,cmd.c_str(),NULL,NULL,false,
                                                      NORMAL_PRIORITY_CLASS,
                                                      NULL,NULL,&si,&pi);
            if(bRet == false)
            {
                  //Application->MessageBox("执行test.bat出错!","错误t",MB_OK);
            }
            else
            {
                CloseHandle(pi.hThread);
                DWORD   dwret=WaitForSingleObject(pi.hProcess,   INFINITE);  //INFINITE

                CloseHandle(pi.hProcess);
                strAttchPath = ExtractFilePath(Application->ExeName)+ Filename;
            }
        }

#6


dfdsfsdf