HELP: Launching a Windows Application from Delphi v2.0
Quote:
> Hi All,
> I am trying to launch a 32-bit Windows-based application developed in
> C/C++. When I try execute it with WINEXEC, I get an error message. (The
> program reports an error in C2SYS)
> How else can I launch the application? Can it be launched in a window on
> my form?
> I recall that the WinExec procedure is only for backwards compatibility.
> Is this true? Must I use CreateProcess? If so, how do I use it?
> Thanks in advance,
> - Craig.
Yes you should use CreateProcess.
Here is a small procedure that takes a command line as input, executes the
process (or program if you like) and wait until it terminates. You can
remove the call to WaitForSingleObject if you just want to launch the app
and let the calling app continue.
procedure CallExecutable(CommandLine: string);
var
StartUpInfo: TStartUpInfo;
ProcessInfo: TProcessInformation;
begin
with StartUpInfo do
begin
lpReserved := NIL;
lpDesktop := NIL;
lpTitle := NIL;
{ ignored:
dwX;
dwY;
dwXSize;
dwYSize;
dwXCountChars;
dwYCountChars;
dwFillAttribute; }
dwFlags := 0;
{ ignored:
WORD wShowWindow;
WORD cbReserved2;
LPBYTE lpReserved2;
HANDLE hStdInput;
HANDLE hStdOutput;
HANDLE hStdError;
}
cb := sizeof(StartUpInfo); { size of structure }
end;
if not CreateProcess(
NIL, { LPCTSTR lpApplicationName, // pointer to name of executable
module }
PChar(CommandLine), { LPTSTR lpCommandLine, // pointer to command
line string }
NIL, { LPSECURITY_ATTRIBUTES lpProcessAttributes, // pointer to
process security attributes }
NIL, { LPSECURITY_ATTRIBUTES lpThreadAttributes, // pointer to thread
security attributes }
False, { BOOL bInheritHandles, // handle inheritance flag }
0, { DWORD dwCreationFlags, // creation flags }
NIL, { LPVOID lpEnvironment, // pointer to new environment block }
NIL, { LPCTSTR lpCurrentDirectory, // pointer to current directory
name }
StartUpInfo, { LPSTARTUPINFO lpStartupInfo, // pointer to STARTUPINFO
Quote:
}
ProcessInfo { LPPROCESS_INFORMATION lpProcessInformation // pointer
to PROCESS_INFORMATION }
)
then
raise Exception.Create(Format('Can not create process (%s), error-code:
%d', [CommandLine, GetLastError]))
else
WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
end;
--
Geir F Kiste
Mikromarc Library Automation software info can be found on
http://www.mikromarc.no.