Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

CreateProcess

Posted on 1998-10-26
8
Medium Priority
?
239 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 150 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files 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.

730 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