devxlogo

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

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):

=================================TypePROCESS_INFORMATION = record  hProcess: Longint;  hThread: Longint;  dwProcessID: Longint;  dwThreadID: Longint;End;Procedure ExecCmd(cmdline: String);varprocc: 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 getthat error, or do you see other errors that may be causing it? I need toget this working ASAP. I used it in VB without problems, but thenagain VB isn’t as picky as Delphi. Any help would be appreciated, particulary because Iuse this techniquein 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 youlook in the VCL source, you’ll see that the formal parameter is defined asbeing passed by VAR. Here’s some code that will help you. It’ll execute anyprogram.

{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 throughDelphi is that the help topics are directed towards C/C++ programmers,not Delphi programmers. So on the fly, Delphi programmers have totranslate the C/C++ conventions to Delphi. This has caused a lotof confusion for me and others who have been exploring threadsand processes. With luck, we’ll see better documentation emerge from either Borlandor a third-party source.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist