1
votes

I'm trying to create a dll with delphi, I set some file attributes but then I want to run a .exe file from the working directory. I tried to run the exe file with this code

ShellExecute(Handle, 'open', 'start.exe', nil, nil, SW_SHOWNORMAL);

But I get errors: Undeclared identifier 'Handle'.

Undeclared identifier 'SW_SHOWNORMAL'

What would be the best way to run the exe file ?

3

3 Answers

8
votes

Be sure to add ShellApi to your Unit's uses clause.

uses ShellApi;

The first parameter can be 0 if the program doesn't have a windows handle.

ShellExecute(0, 'open', ('start.exe'), nil, nil, SW_SHOW);

The "Handle" parameter is not defined in your start.exe procedure

Procedure TForm1.StartEXE;
begin
ShellExecute(0, 'open', ('start.exe'), nil, nil, SW_SHOW);
end;

This will make it universally-accessible from any other function or procedure within your TForm1.

6
votes

The errors you describe in the question are:

Undeclared identifier 'Handle'

Only you know which handle to pass. Either pass a handle to a form, or the Application object's handle, or perhaps even 0 if your application does not have a window handle to hand.

Undeclared identifier 'SW_SHOWNORMAL'

That symbol is defined in the Windows unit. You simply need to add that unit to your uses list.

1
votes

Add the Windows unit to the implementation clause of your unit where this call is made and your program will compile. ALthough the CreateProcess function would be a better option in this case. Something like this (not tested and off the top of my head) :-

Procedure ExecNewProcess(Const ProgramName : String; pWait : Boolean);
Var
  lOK : Boolean;
  lStartInfo : TStartupInfo;
  lProcInfo : TProcessInformation;
Begin
  FillChar(lStartInfo, SizeOf(TStartupInfo), #0);
  FillChar(lProcInfo, SizeOf(TProcessInformation), #0);
  lStartInfo.cb := SizeOf(TStartupInfo);
  lOK := CreateProcess(Nil, PChar(ProgramName), Nil, Nil, False,
    CREATE_NEW_PROCESS_GROUP + NORMAL_PRIORITY_CLASS, Nil, Nil, lStartInfo, lProcInfo);
  If lOK Then
  Begin
    If pWait Then
      WaitForSingleObject(lProcInfo.hProcess, INFINITE);
  End
  Else
    ShowMessage('Unable to run ' + ProgramName);
  CloseHandle(lProcInfo.hProcess);
  CloseHandle(lProcInfo.hThread);
End;