Using CreateProcess to invoke an exe file?

萝らか妹 提交于 2019-12-07 15:22:07

问题


Been trying to invoke the Truecrypt exe from my Visual Studio C++ application, but CreateProcess just isn't working. GetLastError() shows 127.
The objective is to invoke the exe without showing the command window. Please help. I've tried searching and also reading the CreateProcess parameter explanation.

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include<Windows.h>

int main( void )
{
    HANDLE StdInHandles[2]; 
    HANDLE StdOutHandles[2]; 
    HANDLE StdErrHandles[2]; 

    CreatePipe(&StdInHandles[0], &StdInHandles[1], NULL, 4096); 
    CreatePipe(&StdOutHandles[0], &StdOutHandles[1], NULL, 4096); 
    CreatePipe(&StdErrHandles[0], &StdErrHandles[1], NULL, 4096); 

    STARTUPINFO si;   memset(&si, 0, sizeof(si));  // zero out

    si.dwFlags =  STARTF_USESTDHANDLES; 
    si.hStdInput = StdInHandles[0];  // read handle
    si.hStdOutput = StdOutHandles[1];  // write handle 
    si.hStdError = StdErrHandles[1];  // write handle 
    PROCESS_INFORMATION pi; 
    std::cout<< CreateProcess("\"C:\\Program Files\\TrueCrypt\\cmd.exe\\TrueCrypt.exe\"", " //b" , NULL, NULL, FALSE, CREATE_NO_WINDOW , NULL, NULL, &si, &pi)<< "\n" << GetLastError() << "\n";
    std::cin.get();
}

回答1:


Ok, so cracked it finally after trying out a lot of flags from the documentation. Hope it's helpful for anyone else struggling with it.

#include<Windows.h>

int main()
{
    STARTUPINFO si = { sizeof(STARTUPINFO) };
    si.cb = sizeof(si);
    si.dwFlags = STARTF_USESHOWWINDOW;
    si.wShowWindow = SW_HIDE;
    PROCESS_INFORMATION pi;
    CreateProcess("C:\\Program Files\\Nero\\Nero 7\\Core\\nero.exe", NULL , NULL, NULL, FALSE, CREATE_NO_WINDOW , NULL, NULL, &si, &pi);
}//main

Note that Nero's GUI will show up, but some other exe's which you may try will start, but the window won't be visible. You'll be able to see the application in TaskManager though.




回答2:


The most likely cause is your STARTUPINFO struct. At a minimum, you need to set the cb member to sizeof(STARTUPINFO). Here's how I like to do it:

STARTUPINFO si = {sizeof(STARTUPINFO)};

Also, you're not checking the results of CreatePipe for failure.



来源:https://stackoverflow.com/questions/16917137/using-createprocess-to-invoke-an-exe-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!