Board index » delphi » CreateProcess function

CreateProcess function

Hello everybody,

  I'am relatively a new delphi user, can any one help me to find out why
my call to CreateProcess WinAPI function doesn't compile in this piece of
code:

*******************************************************************
procedure Tmainpg.BitBtn9Click(Sender: TObject);

var
   Buffer: Pchar;
   Size: byte;
   StartUp : ^TStartUpInfo;

begin
Size := Edit6.GetTextLen;
Inc(Size);
GetMem(Buffer, Size);
Edit6.GetTextBuf(Buffer, Size);
StartUp^ := (8,nil,nil,STARTF_USESHOWWINDOW,SW_SHOWDEFAULT,0,nil);
CreateProcess(Buffer,nil,nil,nil,True,
              DETACHED_PROCESS,nil,nil,StartUp,nil);
FreeMem(Buffer, Size);
end;

********************************************************************

 Thanks for any help.

------ Adil Sbai ---------
-- mas...@orca.vub.ac.be -
--- Lab. of Hydrology ----
---- VUB University ------
--- Brussels, Belgium ----

 

Re:CreateProcess function


Quote
Adil Sbai <mas...@vub.ac.be> wrote:
>   I'am relatively a new delphi user, can any one help me to find out why
> my call to CreateProcess WinAPI function doesn't compile in this piece of
> code:

> *******************************************************************
> procedure Tmainpg.BitBtn9Click(Sender: TObject);

> var
>    Buffer: Pchar;
>    Size: byte;
>    StartUp : ^TStartUpInfo;

> begin
> Size := Edit6.GetTextLen;
> Inc(Size);
> GetMem(Buffer, Size);
> Edit6.GetTextBuf(Buffer, Size);
> StartUp^ := (8,nil,nil,STARTF_USESHOWWINDOW,SW_SHOWDEFAULT,0,nil);
> CreateProcess(Buffer,nil,nil,nil,True,
>               DETACHED_PROCESS,nil,nil,StartUp,nil);
> FreeMem(Buffer, Size);
> end;

One thing that immediately jumps out at me is you are declaring StartUp to
be a pointer to a TStartUp record.  Even if you got it to compile, you'd
probably get an app exeception.  The assignment to startup doesn't give
values for every value in the record, so that isn't going to work, even when
memory is allocated to it.  Also, you don't need the PChar stuff with the
new Delphi 2.0 long strings.  Here's what I'd suggest you try:

*******************************************************************
{ I don't have anything here except Delphi help files, so these }
{ some of the type and member names may not be right.           }
procedure TMainPg.BitBtn9Click(Sender: TObject);
var
  CmdLine: string;
  StartUp: TStartUpInfo;
  ProcessInfo: TProcessInfo;
begin
  CmdLine := Edit6.Text;
  FillChar(StartUp, SizeOf(StartUp), #0); { Zero out all values }
  with StartUp do begin
    cb := SizeOf(StartUp);
    dwFlags := STARTF_USESHOWWINDOW;
    wShowWindow := SW_SHOWDEFAULT;
  end;
  CreateProcess(@CmdLine[1], NIL, NIL, NIL, TRUE, DETACHED_PROCESS,
                NIL, NIL, @StartUp, @ProcessInfo);
end;

You can check the type names and member names that I'm not sure about in the
VCL source file SOURCE\RTL\WIN\WINDOWS.PAS.  If you don't have the VCL
source, let me know and I'll check them tonight.

Regards,
Brad
bstow...@pobox.com
Free Delphi Stuff:  http://www.pobox.com/~bstowers/delphi/

Other Threads