简体   繁体   中英

c++ Execute batch file with createprocess

I tried to execute a batch file which is in same directory as in the application is and hide the batch file window as in hidden but the code doesn't work for some reason.

Please help me what is wrong in the code. Below is the code.

STARTUPINFO StartupInfo;
PROCESS_INFORMATION ProcessInfo;


memset(&StartupInfo, 0, sizeof(StartupInfo));
StartupInfo.cb = sizeof(STARTUPINFO);
StartupInfo.dwFlags = STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow = SW_HIDE;

CreateProcess(NULL, _T("Testfile.bat"), NULL, NULL, FALSE,CREATE_NEW_CONSOLE, NULL, NULL,&StartupInfo,&ProcessInfo);

The application crashes when the control comes to this portion of the code.

Below is a pseudo code for running bat in background.

 //... Prepare
 //
 // Get Current Dir  
 #define BUFSIZE MAX_PATH             
 TCHAR myBat[BUFSIZE];
 DWORD dwRet;    
 dwRet = GetCurrentDirectory(BUFSIZE, myBat);

 // Append .bat name
 myBat += "this_one.bat"


//... Execute
//
STARTUPINFO si = { sizeof(STARTUPINFO) };
PROCESS_INFORMATION pi;

wchar_t cmdline[] = L"cmd.exe /C "+ myBat ;
wchar_t cmdline2[] = L"start /MIN /B \"\" \""+ myBat +"\"";

// For kiosk windows
//wchar_t cmdline[] = L"%systemroot%\\system32\\cmd.exe /C "+ myBat ;

if (!CreateProcess(NULL, cmdline, NULL, NULL, false, CREATE_UNICODE_ENVIRONMENT,
   NULL, NULL, &si, &pi))
{
    std::cout << GetLastError();
    abort();
}

CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM