Solved

CreateProcess

Posted on 1998-10-26
8
234 Views
Last Modified: 2010-08-05
Hi,

I'm trying to start a program from within my VC5 program with the function CreateProcess (just a simple void main() { blablabla; } program), but it doesn't work. All i get is a zero returned. Can somebody give me a small code example on CreateProcess? And if the little program run's, how do i kill it?

Tnx,

Stefaan
0
Comment
Question by:940961sl
  • 4
  • 3
8 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 1253825
This should work for you:

    BOOL                bRes;

    STARTUPINFO         si;
    PROCESS_INFORMATION pi;

    ZeroMemory  (   &sa,    sizeof  (   SECURITY_ATTRIBUTES));
    ZeroMemory  (   &pi,    sizeof  (   PROCESS_INFORMATION));


    bRes    =   CreateProcess   (   "c:\\mypath\\test.exe",
                                    NULL,
                                    NULL,
                                    NULL,
                                    FALSE,
                                    NORMAL_PRIORITY_CLASS,
                                    NULL,
                                    NULL,
                                    &si,
                                    &pi
                                );

    TerminateProcess    (   pi.hProcess);


0
 
LVL 1

Author Comment

by:940961sl
ID: 1253826
Hi,

Sorry, didn't work, is there something else I should be aware of? The two program's don't have to be in the same directory, do they? I still get a zero returned from CreateProcess, shouldn't that be a 1?

Stefaan
0
 
LVL 86

Expert Comment

by:jkr
ID: 1253827
Consider calling 'GetLastError()' after the 'CreateProcess()' call failed, which will provide additional error information. If the two programs aren't located in the same directory, you'll have to provide the full path...
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 1

Author Comment

by:940961sl
ID: 1253828
Hi

GetLastError() gives a zero (I used the function right after the CreateProcess as the help tell's me to do). The doc's say that most functions set the error on zero or NULL if they fail, but I allready know that my functioncall fails. Where can i get additional info on the failure??

0
 
LVL 86

Expert Comment

by:jkr
ID: 1253829
Well, that's really strange... i'll think about it.
0
 
LVL 1

Author Comment

by:940961sl
ID: 1253830
I've got the answer :

#include <windows.h>
#include <stdio.h>

void main()
{

    STARTUPINFO           startup_infos;
    PROCESS_INFORMATION   process_infos;
    SECURITY_ATTRIBUTES   security_attributes;

    startup_infos.cb = sizeof ( startup_infos );
    memset ( &security_attributes, 0, sizeof ( SECURITY_ATTRIBUTES ));
    security_attributes.nLength = sizeof ( SECURITY_ATTRIBUTES );
    security_attributes.bInheritHandle = FALSE;

    GetStartupInfo ( &startup_infos );

    CreateProcess ( NULL,                                                           "d:\\mydir\\testprog.exe",
            &security_attributes,
            &security_attributes,
            FALSE,
            CREATE_NEW_CONSOLE,
            NULL,
            NULL,
            &startup_infos,
            &process_infos);

    Sleep(10000);

    TerminateProcess(process_infos.hProcess, 0);
}

thanks for your help anyway, jkr.

Stefaan
0
 

Accepted Solution

by:
PavelY earned 50 total points
ID: 1253831
Hi,

Here's what you should do:

STARTUP si;
PROCESS_INFORMATION pi;

ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);   // very important!!

CreateProcess(NULL, "Notepad", NULL, NULL, FALSE, 0, NULL,
   NULL, &si, &pi);

// wait for the process to end
WaitForSingleObject(pi.hProcess, INFINITE);

// or kill it at your own time...
TerminateProcess(pi.hProcess, 0);

// don't forget to close the handles...
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);

note:
Make sure the program is in your path, or place the full command line in your second parameter (you can also put the full one in the first parameter instead of the second).

   Pavel

0
 
LVL 1

Author Comment

by:940961sl
ID: 1253832
IS it that important to close the handles? What happens if I don't? What happens if I use the handles again whitout closing them first?
0

Featured Post

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question