1

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 Answers 3

10

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.

Sign up to request clarification or add additional context in comments.

Comments

7

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.

Comments

1

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;

6 Comments

and how to use the CreateProcess function in this case ?
@Dany Seems to be a new question?
@Dany I amended my answer to include an (untested) example of CreateProcess.
lProcInfo is an out parameter; hence no need to initialise it. What's more, the second parameter of CreateProcess must be a modifiable string. You get away with it in D7 because you call the ANSI API which happens to let you off, but it's best practise to pass a modifiable string.
Because CreateProcessW uses the buffer you provide as its workspace for parsing the string. You get away with it in CreateProcessA because that is a wrapper around CreateProcessW and CreateProcessA creates a modifiable buffer to pass to CreateProcessW. That's all implementation detail though, and the docs are clear. It's a very common mistake. Results in AV when you call the Unicode version. Copy the string to a local variable and call UniqueString on it. Job done.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.