3

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();
}
Nav
  • 19,885
  • 27
  • 92
  • 135

2 Answers2

4

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.

Nav
  • 19,885
  • 27
  • 92
  • 135
1

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.

Scott Jones
  • 2,880
  • 13
  • 19
  • `CreatePipe` is working fine. They return 1. Setting the size of `STARTUPINFO` is being done in `memset(&si, 0, sizeof(si));`. I tried your technique too, but it isn't working. Need help. – Nav Jun 05 '13 at 08:48
  • @Nav: No, your memset() line zeros out the whole structure. Scott is telling you that the first member (cb) needs to itself contain sizeof(STARTUPINFO). – Drew Hall Jun 05 '13 at 08:50
  • @Drew: I had tried his code after commenting out `memset`. Still didn't work. – Nav Jun 05 '13 at 09:29
  • Okay, so with the original code I had posted, it worked when I used it like this: `CreateProcess("C:\\Program Files\\Nero\\Nero 7\\Core\\nero.exe", NULL , NULL, NULL, FALSE, CREATE_NEW_CONSOLE , NULL, NULL, &si, &pi);`. But the command window is still getting created when I use `CreateProcess`. Can't get rid of it even if I use `CREATE_NO_WINDOW`. Help!!! – Nav Jun 05 '13 at 11:19