Solved

CreateProcess

Posted on 1998-10-26
8
230 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 1

Author Comment

by:940961sl
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 86

Expert Comment

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

Author Comment

by:940961sl
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
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.

743 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now