-4

How do I export this function ExecuteFile from ShellExecute to a DLL. It does not work if I call via DLL. However if I use ShellExecution directly in the project it works

procedure ExecuteFile(FileName:WideString);stdcall;
begin
  ShellExecute(0,nil,PChar(FileName),nil,nil,SW_NORMAL);
end;

procedure ExecuteFile(FileName:WideString);stdcall; external 'my.dll';
André
  • 93
  • 1
  • 1
  • 9
  • 1
    What specifically does *does not work* mean? We can't see your screen from here. In what way **specifically** does it **not work**? – Ken White Nov 20 '17 at 23:02
  • 2
    It looks like you're confused between `external` and `exports`. – Stijn Sanders Nov 20 '17 at 23:04
  • Also, what version of Delphi are you using? `ExecuteFile()` takes a `WideString` (why?) as input, so you should be calling `ShellExecuteW()` with `PWideChar` to match. `ShellExecute()` does not map to `ShellExecuteW()`, and `PChar` does not map to `PWideChar`, unless you are using Delphi 2009+. – Remy Lebeau Nov 20 '17 at 23:06

1 Answers1

1

You need two separate projects, one for the DLL itself, and one for the project that uses the DLL.

In the DLL project, you use exports, eg:

library my;

uses
  Shellapi;

{$R *.res}

procedure ExecuteFile(FileName: WideString); stdcall;
begin
  ShellExecuteW(0, nil, PWideChar(FileName), nil, nil, SW_NORMAL);
end;

exports
  ExecuteFile;

begin
end.

Then, in the consuming project, you use an external declaration to import the function from the DLL, eg:

procedure ExecuteFile(FileName: WideString); stdcall; external 'my.dll';

Then the project can call ExecuteFile() like any other function, eg:

ExecuteFile('filename');

Just make sure the exporting DLL is:

  1. located somewhere on the OS's search path, preferably in the same folder as the consuming app.

  2. compiled to use the same bitness (32bit or 64bit) as the consuming project.

Otherwise, it won't load correctly.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • It worked. He was unaware of this other function. I am using export pair to a DLL from an SDK that I am designing. Thank you! – André Nov 21 '17 at 00:53