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.

Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as