A question about CreateProcess()

This is a discussion on A question about CreateProcess() within the C++ Programming forums, part of the General Programming Boards category; Hi friends, My problem is as follow: When using createProcess() the thing I don't understand is how to define what ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    3

    A question about CreateProcess()

    Hi friends,

    My problem is as follow: When using createProcess() the thing I don't understand is how to define what the child process does? It seems that createProcess() does not allow me to do this. I saw some examples in the internet but they only demonstrated the ability of createProcess() to call an already-written process, for example, notepad.exe. And now I want the child process to execute something written by me. How and where can I put my code into the newly created process? Please tell me how to do this.

    Thank you for reading my question.

    Regards,

    Riscilla.

  2. #2
    Registered User
    Join Date
    Nov 2007
    Posts
    46
    You compile the code you have into an executable and then create a process using CreateProcess, where the lpApplicationName is set to the name of the executable you created.

    In case you wish to start a new process with the code you just wrote, compile the code you need to run in the process into its own executable and call CreateProcess with the name of that executable as the lpApplicationName parameter.

    Example:

    You have a piece of code you compile into a program that looks like this:
    Code:
    #include <iostream>
    
    int main( int argc, char *argv[] )
    {
    	for( int i = 0; i < argc; ++i )
    		std::cout<<argv[i]<<'\n';
    
    	return 0;
    }
    You then compile this into an exe named... Say in this case "Test.exe" would make sense. This just prints the command line parameters that you fed the program.

    Then you have your main program like this (for example):
    Code:
    #include <windows.h>
    #include <iostream>
    #include <string>
    
    int main( int argc, char *argv[] )
    {
    	std::string ExeName("Test.exe"), CmdLine;
    	STARTUPINFO si;
    	ZeroMemory( &si, sizeof(si) );
    	PROCESS_INFORMATION pi;
    	GetStartupInfo( &si );
    	std::getline( std::cin, CmdLine );
    
    	if( !CreateProcess( ExeName.c_str(), CmdLine.c_str(), 0, 0, 0, DETACH_PROCESS, 0, 0, &si, &pi ) )
    		return 1;
    
    	return 0;
    }
    And compile this... Then you make sure that(in this case since the path for "Test.exe" is hardcoded) both executables are in the same directory and it should work.

    Note that I didn't test this, so there might be syntax, logic or other errors.

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    3
    Hi JacobN,

    Thank you for your answer. Now I can understand how to use createProcess().

    Regards,

    Riscilla.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21