我正在Delphi中创建一个新组件,它实例化了一个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.
我有两个问题:
- 当我在 IDE 上安装组件时,它会搜索物理 DLL,如果在路径中找不到它,则会给出错误。我希望组件在运行时有效使用时寻找它。
- 是否可以在运行时设置 dll 库文件名?即:"MyDllName.dll"可以更改为"10029.dll"或"ajjdwawd.dll"
请注意,我将 DLL 声明放在实现中,以便不向调用者公开函数调用。
感谢您的回答。
您当前的代码使用所谓的加载时链接。必须在加载模块时解析依赖关系,否则将无法加载。您需要使用替代方法,即运行时链接。
在德尔福,有两种方法可以做到这一点:
- 通过
- 调用
LoadLibrary
、GetProcAddress
和FreeLibrary
直接使用 Win32。 - 让德尔福 RTL 使用
delayed
关键字为您做到这一点。
文档中更详细地介绍了这两种方法:
- http://docwiki.embarcadero.com/RADStudio/en/Libraries_and_Packages
- http://docwiki.embarcadero.com/CodeExamples/en/DelayedLoading_(德尔福)