简体   繁体   中英

Declaring a DLL inside a Delphi XE5 component

I'm creating a new component in Delphi, which instantiates a DLL

Unit UMyComponent

interface

type
  TMyComponent = class(TComponent)
    ... 
    procedure MyDllCall; 
  end;

procedure Register;

implementation

function MyDll: Longint; stdcall; external 'MyDllName.dll' name 'MyFunction'

procedure TMyComponent.MyDllCall;
var
  res: LongInt;
begin
  res:= MyDll;
end;

...

procedure Register;
begin 
 RegisterComponents('My Tab', [TMyComponent]); 
end;

end.

I have 2 questions:

  1. When I install the component on the IDE it searches for the physical DLL and gives an error if it's not found in path. I'd like the component to look for it when it's going to be effectively used at runtime.
  2. Is it possible to have the dll library file name to be set at runtime? ie: 'MyDllName.dll' could change to '10029.dll' or 'ajjdwawd.dll'

Note that I put DLL declaration in the implementation in order not to expose the function call to callers.

Thanks for answering.

Your current code uses what is known as load-time linking . The dependency must be resolved when the module is loaded, otherwise it will fail to load. You need to use the alternative method, run-time linking .

In Delphi there are two ways to do that:

  • Use the Win32 directly by calling LoadLibrary , GetProcAddress and FreeLibrary .
  • Get the Delphi RTL to do that for you using the delayed keyword.

Both approaches are covered in more detail in the documentation:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM