سؤال

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();
}
هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top