Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 241
  • Last Modified:

CreateProcess

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
940961sl
Asked:
940961sl
  • 4
  • 3
1 Solution
 
jkrCommented:
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
 
940961slAuthor Commented:
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
 
jkrCommented:
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
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!

 
940961slAuthor Commented:
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
 
jkrCommented:
Well, that's really strange... i'll think about it.
0
 
940961slAuthor Commented:
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
 
PavelYCommented:
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
 
940961slAuthor Commented:
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

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.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now