Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Pascal
Expertise: Beginner
Mar 19, 1997

How can I properly use CreateProcess to instantiate a new process?

Question:

Here is the code I'm using (the error is posted where it occurs):

=================================
Type
PROCESS_INFORMATION = record
  hProcess: Longint;
  hThread: Longint;
  dwProcessID: Longint;
  dwThreadID: Longint;
End;

Procedure ExecCmd(cmdline: String);

var
procc: PROCESS_INFORMATION;
startup: TSTARTUPINFO;
ret1: longint;
ret2: longint;
ret3: longbool;

Const NORMAL_PRIORITY_CLASS = $20;
Const INFINITE = -1;
   Begin
     startup.cb:= SizeOf(startup);

     ret1:= CreateProcess(
           nil,
           PChar(cmdline),
           nil,
           nil,
           LongBool(1),
           NORMAL_PRIORITY_CLASS,
           nil,
           nil,
           startup,
           procc); {error: Types of actual and formal var parameters
                       must be identical.}

     ret2:= WaitForSingleObject(procc.hProcess, INFINITE);
     ret3:= CloseHandle(procc.hProcess);

   End;
===============================================

Do you have any ideas why I get that error, or do you see other errors that may be causing it? I need to get this working ASAP. I used it in VB without problems, but then again VB isn't as picky as Delphi. Any help would be appreciated, particulary because I use this technique in many of my VB apps that I'm converting to Delphi 2.0.

Answer:
Looking over your code, procc must be of the type "TProcessInformation" in order to work. If you look in the VCL source, you'll see that the formal parameter is defined as being passed by VAR. Here's some code that will help you. It'll execute any program.

{Supply a fully qualified path name in ProgramName}
procedure ExecNewProcess(ProgramName : String);
var
  StartInfo  : TStartupInfo;
  ProcInfo   : TProcessInformation;
  CreateOK   : Boolean;
begin

  { fill with known state }
  FillChar(StartInfo,SizeOf(TStartupInfo),#0);
  FillChar(ProcInfo,SizeOf(TProcessInformation),#0);
  StartInfo.cb := SizeOf(TStartupInfo);

  CreateOK := CreateProcess(PChar(ProgramName),nil, nil, nil,False,
              CREATE_NEW_PROCESS_GROUP+NORMAL_PRIORITY_CLASS, nil, nil, StartInfo, ProcInfo);

  { check to see if successful }
  if CreateOK then
    //may or may not be needed. Usually wait for child processes
    WaitForSingleObject(ProcInfo.hProcess, INFINITE);
end;

I think the biggest problem people have working with the WinAPI through Delphi is that the help topics are directed towards C/C++ programmers, not Delphi programmers. So on the fly, Delphi programmers have to translate the C/C++ conventions to Delphi. This has caused a lot of confusion for me and others who have been exploring threads and processes. With luck, we'll see better documentation emerge from either Borland or a third-party source.

DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date