执行*.bat文件(cmd命令)

时间:2022-09-30 12:35:36
如何执行*.bat文件,但是我要不出现cmd窗口,并且等这个bat文件执行完成以后再再执行下面的代码

具体如下:

ShellExecute(Handle,"open","start.bat","",NULL,SW_HIDE);

--等待,等这个start.bat文件执行完成后再执行下面的代码

MessageBox("ok");


望高手指点

6 个解决方案

#1


帮你作一函数:作用  WaitCall("c:\\start.bat");
void __fastcall TForm1::WaitCall(AnsiString FName)
{
        //TODO: Add your source code here
  if(FileExists(FName))
  {
    STARTUPINFO  StartupInfo;
    ZeroMemory( &StartupInfo, sizeof(STARTUPINFO));
    StartupInfo.cb = sizeof(STARTUPINFO);
    StartupInfo.wShowWindow=SW_MINIMIZE;//SW_SHOWNORMAL;
    StartupInfo.dwFlags=1;
    PROCESS_INFORMATION ProcessInfo;
    if(CreateProcess(FName.c_str,NULL,  NULL, NULL, TRUE, NORMAL_PRIORITY_CLASS, NULL, NULL, &StartupInfo, &ProcessInfo))
    {
        DWORD dwExitStatus = STILL_ACTIVE;
        do
        {
            if( !GetExitCodeProcess(ProcessInfo.hProcess, &dwExitStatus)) break;
            Application->ProcessMessages();
        } while (dwExitStatus == STILL_ACTIVE);
        ShowMessage("结束!");
    }
   }
   else
   {
     SystemLog("启动进程出错");
   }        
}

#2


bat文件应该也可以使用CreateProcess运行的,设置lpStartupInfo->wShowWindow=SW_HIDE;隐藏窗口

得到进程句柄,然后用WaiteSingleObject等待进程结束
然后再执行MessageBox("ok");

#3


用ShellExecute是不行地。
用这个:

SHELLEXECUTEINFO ShExecInfo = {0};
ShExecInfo.cbSize        = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask         = SEE_MASK_NOCLOSEPROCESS;
ShExecInfo.hwnd          = NULL;
ShExecInfo.lpVerb        = NULL;
ShExecInfo.lpFile        = "C:\\123\\123.bat";
ShExecInfo.lpParameters  = "";
ShExecInfo.lpDirectory   = NULL;
ShExecInfo.nShow         = SW_HIDE;
ShExecInfo.hInstApp      = NULL;
ShellExecuteEx(&ShExecInfo);
WaitForSingleObject(ShExecInfo.hProcess, INFINITE);
MessageBox(Handle, "ok", "ok", MB_OK);

#4


打错了,应该是WaiteForSingleObject

这么用
hProcess=CreateProcess(......);
WaitForSingleObject(hProcess,INFINITE);
MessageBox("ok");

#5


肯定是bat执行完后,执行下面得语句啊。不过不是bat里面得还要执行别得exe得话,就不是了

#6


谢谢各位了,问题解决了

#1


帮你作一函数:作用  WaitCall("c:\\start.bat");
void __fastcall TForm1::WaitCall(AnsiString FName)
{
        //TODO: Add your source code here
  if(FileExists(FName))
  {
    STARTUPINFO  StartupInfo;
    ZeroMemory( &StartupInfo, sizeof(STARTUPINFO));
    StartupInfo.cb = sizeof(STARTUPINFO);
    StartupInfo.wShowWindow=SW_MINIMIZE;//SW_SHOWNORMAL;
    StartupInfo.dwFlags=1;
    PROCESS_INFORMATION ProcessInfo;
    if(CreateProcess(FName.c_str,NULL,  NULL, NULL, TRUE, NORMAL_PRIORITY_CLASS, NULL, NULL, &StartupInfo, &ProcessInfo))
    {
        DWORD dwExitStatus = STILL_ACTIVE;
        do
        {
            if( !GetExitCodeProcess(ProcessInfo.hProcess, &dwExitStatus)) break;
            Application->ProcessMessages();
        } while (dwExitStatus == STILL_ACTIVE);
        ShowMessage("结束!");
    }
   }
   else
   {
     SystemLog("启动进程出错");
   }        
}

#2


bat文件应该也可以使用CreateProcess运行的,设置lpStartupInfo->wShowWindow=SW_HIDE;隐藏窗口

得到进程句柄,然后用WaiteSingleObject等待进程结束
然后再执行MessageBox("ok");

#3


用ShellExecute是不行地。
用这个:

SHELLEXECUTEINFO ShExecInfo = {0};
ShExecInfo.cbSize        = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask         = SEE_MASK_NOCLOSEPROCESS;
ShExecInfo.hwnd          = NULL;
ShExecInfo.lpVerb        = NULL;
ShExecInfo.lpFile        = "C:\\123\\123.bat";
ShExecInfo.lpParameters  = "";
ShExecInfo.lpDirectory   = NULL;
ShExecInfo.nShow         = SW_HIDE;
ShExecInfo.hInstApp      = NULL;
ShellExecuteEx(&ShExecInfo);
WaitForSingleObject(ShExecInfo.hProcess, INFINITE);
MessageBox(Handle, "ok", "ok", MB_OK);

#4


打错了,应该是WaiteForSingleObject

这么用
hProcess=CreateProcess(......);
WaitForSingleObject(hProcess,INFINITE);
MessageBox("ok");

#5


肯定是bat执行完后,执行下面得语句啊。不过不是bat里面得还要执行别得exe得话,就不是了

#6


谢谢各位了,问题解决了