[try Beta version]
Not logged in

 
CreateProcess()

Feb 17, 2010 at 4:19pm
Hello,

I'm currently working on an mfc project where I call a number of commands via system. The problem with this is that it creates the black box which it is called in and as such I've been asked to find another method.

Some browsing of the internet has told me the only way this is possible is to use CreateProcess, unfortunately this function seems a little beyond me. My initial attempt to call it is as follows:

1
2
3
4
5
6
7
8
9
10
11
	
    STARTUPINFO siStartupInfo; 
    PROCESS_INFORMATION piProcessInfo; 
    memset(&siStartupInfo, 0, sizeof(siStartupInfo)); 
    memset(&piProcessInfo, 0, sizeof(piProcessInfo)); 
    siStartupInfo.cb = sizeof(siStartupInfo); 


	LPTSTR lpCommandLine = "ipconfig /all < mount.txt";

		int i = CreateProcess(NULL, lpCommandLine, NULL, NULL, TRUE, CREATE_NO_WINDOW, NULL, NULL, &siStartupInfo, &piProcessInfo);


And while this shows that it is at least returning true, it doesn't appear to do anything. So either I'm missing something quite simple or there is alot more to it than I thought. Does the rest of the progamme stop until this command is done? How do you tell when it's complete?

Any help or suggestions would be appreciated.
Last edited on Feb 17, 2010 at 4:20pm
Feb 18, 2010 at 2:08pm
As a follow up, I've tried implementing this function:

http://www.goffconcepts.com/techarticles/development/cpp/createprocess.html

Passing in "cmd.exe" "explorer C:" and "0" respectively and various other combinations there of. It seems to return false and thus goes into the else statement containing GetLastError(); which returns quite simply as 2. This unfortunately means little to me despite my best attempts at Google-fu.

I really am quite stuck, any help or suggestions would be greatly appreciated.
Feb 18, 2010 at 2:22pm
Scratch that, I'm an idiot.

Changing cmd.exe to C:/WINDOW/System32/cmd.exe makes it calling the window (in a black box as I was trying to avoid). However won't make it call the command.

Any advice?

The WaitForSingleObject returns 258 - i.e process time out, so while I am creating cmd.exe I'm not calling the command.
Last edited on Feb 18, 2010 at 2:39pm
Feb 18, 2010 at 4:03pm
CreateProcess(NULL, _T("C:\\WINDOWS\\System32\\cmd.exe explorer C:"), NULL, NULL, TRUE, CREATE_NO_WINDOW, NULL, NULL, &siStartupInfo, &piProcessInfo);

Should have been:

CreateProcess(NULL, _T("C:\\WINDOWS\\System32\\cmd.exe /c explorer C:"), NULL, NULL, TRUE, CREATE_NO_WINDOW, NULL, NULL, &siStartupInfo, &piProcessInfo);

Ergh.
Last edited on Feb 18, 2010 at 5:45pm
Feb 18, 2010 at 4:56pm
While staring at your last post, I really can't find the difference, it looks exactly the same?
Am I just blind or is it really the same?
Feb 18, 2010 at 5:45pm
Apologies, end of a long day - edited. Essentially I was missing /c.
Mar 2, 2010 at 2:46pm
Ok. Got round to trying to finish the program and found another issue. What I listed above works just fine for the example shown but when I try and use it with pipes to redirect the output it all falls apart.

How do I use pipes with this?
Mar 12, 2010 at 5:39pm
Apparently it's still /c, it was just being odd the first time. Solved it.
Topic archived. No new replies allowed.