| How to start a new process? |
|
This page presents the two Delphi 7 functions that start a new process on a
local or remote computer. They are is based on
TWmiProcessControl component from
WmiSet component collection.
When starting the process on a remote computer, the following must be taken
into account: starting from Windows 2000 Service Pack 3 it is only possible to
start the processes in non-interactive mode. This means that interactive programs
will be able to display their user interface elements. This limitation does not
apply when starting processes on a local host.
The source code below creates and destroys the components on the fly, so these functions may be used UI and in console type applications. |
How to start a new process?
Download
|
// this function starts a new process.
// It may be used for local or remote host. When used for local host
// TargetHost, UserName, Password parameters must be empty.
// For remote computer the parameters must be:
// TargetHost: name or IP address of the remote host, like '10.8.36.54'
// UserName: name of the user, may include domain, like 'MOON\Administrator';
// Password: the user's password.
// AFullPath: the full path to the executable, as seen on a destination host.
// The function returns a handle to a new process
function StartProcesses(TargetHost, UserName, Password, AFullPath: string): cardinal;
var
ProcessControl: TWmiProcessControl;
vDir: string;
begin
ProcessControl := TWmiProcessControl.Create(nil);
try
ProcessControl.MachineName := TargetHost;
ProcessControl.Credentials.UserName := UserName;
ProcessControl.Credentials.Password := Password;
// exception may happen on Win9x, WinNT if WMI core is not installed;
// The provided credentials may also be invalid.
ProcessControl.Active := true;
vDir := ExtractFilePath(AFullPath);
// if full path was not provided, it is still possible to start
// the process whose executable may be found through path.
// But the start dir must be specified.
if vDir = '' then vDir := 'c:\';
Result := ProcessControl.StartProcess(AFullPath, vDir, false);
finally
ProcessControl.Free;
end;
end;
|
Configuring appearance of console applications
Download
|
This example starts command prompt and configures the appearance of the window.
It also sets the priority and passes an environment variable to a child process.
Event though you normally cannot configure the appearance of the main window for
UI applications, the parameters like environment variables, priority work well.
program process_start_cmd;
{$APPTYPE CONSOLE}
uses
Windows, WmiProcessControl, Classes, SysUtils, DetectWinOs;
var
ProcessControl: TWmiProcessControl;
vCommand: string;
begin
ProcessControl := TWmiProcessControl.Create(nil);
try
// exception may happen on Win9x, WinNT if WMI core is not installed;
// The provided credentials may also be invalid.
try
ProcessControl.Active := true;
except
on E: Exception do
begin
writeln('Cannot connect to WMI service: '+E.Message);
Exit;
end;
end;
if IsWindowsNt then vCommand := 'cmd.exe'
else vCommand := 'command.com';
// configure appearance of console application.
// It will have black font on light gray background;
// It will have a low priority;
// one environment variable is passed to a child process;
// console window opens maximized.
ProcessControl.StartupInfo.Title := 'Command prompt window';
ProcessControl.StartupInfo.FillAttribute :=
BACKGROUND_BLUE + BACKGROUND_GREEN + BACKGROUND_RED;
ProcessControl.StartupInfo.PriorityClass := PRIORITY_IDLE;
ProcessControl.StartupInfo.EnvironmentVariables.Add('CHILDPROCESS=TRUE');
ProcessControl.StartupInfo.ShowWindow := SW_SHOWMAXIMIZED;
ProcessControl.StartProcess(vCommand, 'c:\', true);
finally
ProcessControl.Free;
end;
end.
|