Solved

CreateProcess

Posted on 1998-10-26
8
236 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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 how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

710 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