Jus*_*tMe 3 windows delphi dll delphi-7
我正在尝试从我的程序动态加载exe文件,并从该动态加载的exe运行SomeProcedure.这是我在加载的exe - library.exe中所做的事情
interface
procedure SomeProcedure; stdcall;
implementation
procedure SomeProcedure;
begin
ShowMessage('Ala has a cat');
end;
Run Code Online (Sandbox Code Playgroud)
这是我的exe加载的library.exe并尝试从它运行SomeProcedure.
type
THandle = Integer;
TProc = procedure();
var
AHandle: THandle;
Proc: TProc;
procedure TForm1.Button1Click(Sender: TObject);
begin
AHandle := LoadLibrary('library.exe');
if AHandle <> 0 then begin
@Proc := GetProcAddress(AHandle, 'SomeProcedure');
if @Proc <> nil then
try
Proc;
finally
FreeLibrary(AHandle);
end;
end;
end;
end;
Run Code Online (Sandbox Code Playgroud)
不幸的是它不起作用 - AHandle有一个地址,但GetProcAddress总是返回nil.我究竟做错了什么?
据我所知,你所尝试的是不可能的.您不能使用LoadLibrary加载.exe文件,然后调用其导出的函数.您只能将一个.exe文件加载到进程中.您需要将功能移动到库,COM服务器或其他解决方案中.
正如Sertac指出的那样,文档确实涵盖了这一点:
LoadLibrary也可用于加载其他可执行模块.例如,该函数可以指定.exe文件以获取可在FindResource或LoadResource中使用的句柄.但是,不要使用LoadLibrary来运行.exe文件.而是使用CreateProcess函数.
您可以将GetProcAddress与可执行文件的模块句柄一起使用.但是,您必须通过调用GetModuleHandle(0)来获取模块句柄.