August 11, 2022, 04:56:50 AM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


CreateProcess

Started by Todd Riggins, September 06, 2007, 01:35:37 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Todd Riggins

Ok, Im getting stumped. This code keeps creating multiple processes. Also, I would figure that if it did work, the set INFINITE in the last param of WaitForSingleObject would mean that this example would never exit... right?

I think the command line parameter is throwing me off too. Is GetCommandLine() used ok here?

Thanks for any help or info... Im trying...  :-\


#include "windows.inc"
#include "stdio.inc"
#include "tchar.inc"

global sub main()
{
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );

LPTSTR cmd = GetCommandLine();

    // Start the child process.
    if( !CreateProcess( NULL,   // No module name (use command line)
        cmd,        // Command line
        NULL,           // Process handle not inheritable
        NULL,           // Thread handle not inheritable
        FALSE,          // Set handle inheritance to FALSE
        0,              // No creation flags
        NULL,           // Use parent's environment block
        NULL,           // Use parent's starting directory
        &si,            // Pointer to STARTUPINFO structure
        &pi )           // Pointer to PROCESS_INFORMATION structure
    )
    {
        //printf( "CreateProcess failed (%d)\n", GetLastError() );
writeln("CreateProcess failed "+numtostr(GetLastError())+"\n");
do{}until getkey() <> "";
        return;
    }

    // Wait until child process exits.
    WaitForSingleObject( pi.hProcess, INFINITE );

    // Close process and thread handles.
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );
do{}until getkey() <> "";
return;
}
Brought to you buy: http://www.exodev.com

sapero

Lol :) Your program runs itself multiple times and waits until all chid processes termitate (this will never happen, maybe will if you stuck out of memory).
Before out of memory is reached, you're unable to start another process, you can only reset the machine.

a. run a as b and wait
b. run b as c and wait
  c. run c as d and wait...

[out of memory]
z fails to CreateProcess, waits for a key press.
y waits for z...

Ionic Wind Support Team

GetCommandLine returns the path to your executabe and the parameters passed.  In other words you keep running your executable recursively.

You have to parse the string returned by GetCommandLine.

Paul.
Ionic Wind Support Team

Todd Riggins

yeah... I noticed in my windows task manager that it would keep making multiple processes until I hear a windows beep. I suppose it ran out of memory or something. I am able to click the x button on the console window and all the process created will all close.

So getcommandline() suppose to have the path, program name and any command arguements. And since in this progam, I pass all that info that getcommandline()  returns into the second parameter of createprocess, createprocess takes the path and program name supplied by the getcommandline() and creates a new process of the program name, the exact executable that I ran. So thats where the recursion happens and why I see the multiple processes happen in the task manager.

Oh, so instead of using the ShellAPI to execute an external program, I can somehow manipulate the string to pass in the second parameter of createprocess to run that external program... ?

Just trying to get grips on the process of createprocess. Thanks for the tips.
Brought to you buy: http://www.exodev.com

Ionic Wind Support Team

Now you are talking recursively ;)

Just parse the command line string.  Normally when you send parameters to a console executable you give it a switch

mything -p myprog.exe

Where you would search the string returned by GetCommandLine for the '-p' and go from there.

Paul.
Ionic Wind Support Team

Todd Riggins

Ah, so if I  wanted to create an application to be run at console command line where I want to enter parameters( switches ), this would be good way then.

But, I want my application to call another external application... where I suppose I need to do createprocess to call that Other external program then. In this case, I would need to create my own string to pass in the command parameter of createprocess(). right? I'll try that instead...
Brought to you buy: http://www.exodev.com

Todd Riggins

Ok, I finally understand it a bit more clearly now.

I got my main program to call the dummy program with the createprocess function.

main program:

#include "windows.inc"
#include "stdio.inc"
#include "tchar.inc"

global sub main()
{
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );

string txt;
sprintf(txt,"%sdummy.exe",GETSTARTPATH());

    // Start the child process.
    if( !CreateProcess( NULL,   // No module name (use command line)
        txt,        // Command line
        NULL,           // Process handle not inheritable
        NULL,           // Thread handle not inheritable
        FALSE,          // Set handle inheritance to FALSE
        0,              // No creation flags
        NULL,           // Use parent's environment block
        NULL,           // Use parent's starting directory
        &si,            // Pointer to STARTUPINFO structure
        &pi )           // Pointer to PROCESS_INFORMATION structure
    )
    {
        //printf( "CreateProcess failed (%d)\n", GetLastError() );
writeln("CreateProcess failed "+numtostr(GetLastError())+"\n");
do{}until getkey() <> "";
        return;
    }

    // Wait until child process exits.
    WaitForSingleObject( pi.hProcess, INFINITE );

    // Close process and thread handles.
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );

messagebox(null,"Main program says goodbye!","test",MB_OK);
return;
}



And call this dummy.src and compile this seperately so the dummy.exe exists in same folder as the main program above:

#include "windows.inc"
global sub main()
{
messagebox(null,"dummy program says hello!","test",MB_OK);
return;
}



Brought to you buy: http://www.exodev.com