Board index » delphi » HELP: Launching a Windows Application from Delphi v2.0

HELP: Launching a Windows Application from Delphi v2.0

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.

 

Re:HELP: Launching a Windows Application from Delphi v2.0


Mega PC <meg...@lia.co.za> wrote in article
<01bb966b$b6672260$0100a8c0@marcus>...

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
mi...@mikromarc.no

Mikromarc Library Automation software info can  be found on
http://www.mikromarc.no.

Re:HELP: Launching a Windows Application from Delphi v2.0


Quote
Geir F Kiste wrote:

> 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.

Don't forget to call
CloseHandle(ProcessInfo.hProcess);
CloseHandle(ProcessInfo.hThread);

If you don't, you have a memory leak...

M.

--
Martin Larsson, author of several unknown utilities for DOS and Windows.
mailto:martin.lars...@delfi-data.msmail.telemax.no
http://www.delfidata.no/users/~martin
X.400:G=martin;S=larsson;O=delfi-data;P=msmail;A=telemax;C=no

Re:HELP: Launching a Windows Application from Delphi v2.0


On Tue, 03 Sep 1996 20:19:49 +0200, Martin Larsson

Quote
<martin.lars...@delfi-data.msmail.telemax.no> wrote:

~> 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.
~
~Don't forget to call
~CloseHandle(ProcessInfo.hProcess);
~CloseHandle(ProcessInfo.hThread);
~
~If you don't, you have a memory leak...

Hi Martin,

I don't mean to be ignorant or stupid, but this is the first time that
I have seen "CloseHandle(ProcessInfo.hProcess);".  What does it do?

I have written a function which is listed below.  And it did call the
CreateProcess.  So, when do I need to call CloseHandle to free the
memory?  Please advise.

----->8  Begin of Code  8<-----

function TfrmRtrHelperMain.LaunchRouter32: Boolean;
var
   TSI: TStartUpInfo;
   TPI: TProcessInformation;
begin
   FillChar(TSI, SizeOf(TSI), 0); //must set the TSI structure to ,
   TSI.CB := SizeOf(TSI);     //  otherwise, CreateProcess will fail!
   try
      Result := CreateProcess(PChar(Router32File), Nil, Nil, Nil,
                False, DETACHED_PROCESS OR NORMAL_PRIORITY_CLASS,
                Nil, PChar(ExtractPath(Router32File)), TSI, TPI);
      if Logging then
         LogFile1.LogToFile('Launched Router');
   except
      MessageDlg('Error Launching Router32...'+ 'Please ensure ' + #13
                + #10 + 'that Router32 Directory is correct.', tError,
                [mbOK], 0);
   end;
end;

----->8  End of Code  8<-----

Thanks for your time.

Finest Regards,
Sam

 _       __    _____    _____            
| |     / /   / ___/   / ___/ ____   ____   ____ _
| | /| / /    \__ \    \__ \ / __ \ / __ \ / __ `/
| |/ |/ /  _ ___/ / _ ___/ // /_/ // / / // /_/ /
|__/|__/  (_)____/ (_)____/ \____//_/ /_/ \__, /
                                         /____/
**************************************************
   From the desk of Song Weng Sam

   Email : son...@pacific.net.sg
   WWW   : http://home.pacific.net.sg/~songws/SAM
   Tel   : (65)-271-7075 (0ffice)
   Pager : 9-492-4014
**************************************************

Re:HELP: Launching a Windows Application from Delphi v2.0


Quote
Song Weng Sam wrote:

> I don't mean to be ignorant or stupid, but this is the first time that
> I have seen "CloseHandle(ProcessInfo.hProcess);".  What does it do?

In Win32, most everything you create is referenced through a handle,
it be a font, a bitmap, a file, a thread or a process. A handle is
a resource, so you have to release it when you're done. This is what
CloseHandle does. According to the help file for CreatProcess, you
will be given two handles, one to the process and one to the main
thread. You have to release both these when you're not interested
anymore. This will not stop the program/thread, just tell the system
that you're done.

Quote
> I have written a function which is listed below.  And it did call the
> CreateProcess.  So, when do I need to call CloseHandle to free the
> memory?  Please advise.

You need to call CloseHandle when you don't need to manipulate
Router32 anymore.

Note that I doubt CreateProcess will throw an exception, so the
process might not have started, even though you write 'Launched
Router' to the log file. The return value gives the true facts.

M.

--
Martin Larsson, author of several unknown utilities for DOS and Windows.
mailto:martin.lars...@delfi-data.msmail.telemax.no
http://www.delfidata.no/users/~martin
X.400:G=martin;S=larsson;O=delfi-data;P=msmail;A=telemax;C=no

Re:HELP: Launching a Windows Application from Delphi v2.0


This is from the Win32.hlp file from CreateProcess:

The created process remains in the system until all threads within the
process have terminated and all handles to the process and any of its
threads have been closed through calls to CloseHandle. The handles for both
the process and the main thread must be closed through calls to
CloseHandle. If these handles are not needed, it is best to close them
immediately after the process is created.

--
Paul Motyer  11:15 Tues, 10th September, 1996 EST
pa...@linuxserver.pccity.com.au
SoftStuff, Croydon, Vic,  Australia, 3136.

Song Weng Sam <son...@pacific.net.sg> wrote in article
<32344da9.6457...@news.singnet.com.sg>...

Quote
> On Tue, 03 Sep 1996 20:19:49 +0200, Martin Larsson
> <martin.lars...@delfi-data.msmail.telemax.no> wrote:

> ~> 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.
> ~
> ~Don't forget to call
> ~CloseHandle(ProcessInfo.hProcess);
> ~CloseHandle(ProcessInfo.hThread);
> ~
> ~If you don't, you have a memory leak...

> Hi Martin,

> I don't mean to be ignorant or stupid, but this is the first time that
> I have seen "CloseHandle(ProcessInfo.hProcess);".  What does it do?

> I have written a function which is listed below.  And it did call the
> CreateProcess.  So, when do I need to call CloseHandle to free the
> memory?  Please advise.

> ----->8  Begin of Code  8<-----

> function TfrmRtrHelperMain.LaunchRouter32: Boolean;
> var
>    TSI: TStartUpInfo;
>    TPI: TProcessInformation;
> begin
>    FillChar(TSI, SizeOf(TSI), 0); //must set the TSI structure to ,
>    TSI.CB := SizeOf(TSI);     //  otherwise, CreateProcess will fail!
>    try
>       Result := CreateProcess(PChar(Router32File), Nil, Nil, Nil,
>            False, DETACHED_PROCESS OR NORMAL_PRIORITY_CLASS,
>            Nil, PChar(ExtractPath(Router32File)), TSI, TPI);
>       if Logging then
>          LogFile1.LogToFile('Launched Router');
>    except
>       MessageDlg('Error Launching Router32...'+ 'Please ensure ' + #13
>            + #10 + 'that Router32 Directory is correct.', tError,
>            [mbOK], 0);
>    end;
> end;

> ----->8  End of Code  8<-----

> Thanks for your time.

> Finest Regards,
> Sam

>  _       __    _____    _____            
> | |     / /   / ___/   / ___/ ____   ____   ____ _
> | | /| / /    \__ \    \__ \ / __ \ / __ \ / __ `/
> | |/ |/ /  _ ___/ / _ ___/ // /_/ // / / // /_/ /
> |__/|__/  (_)____/ (_)____/ \____//_/ /_/ \__, /
>                                          /____/
> **************************************************
>    From the desk of Song Weng Sam

>    Email : son...@pacific.net.sg
>    WWW   : http://home.pacific.net.sg/~songws/SAM
>    Tel   : (65)-271-7075 (0ffice)
>    Pager : 9-492-4014
> **************************************************

Other Threads