我找到了一个单元,其功能允许直接从内存加载DLL,但我不知道如何使用它..
这是单位:http://www.delphibasics.info/home/delphibasicssnippets/udllfrommem-loadadllfrommemory
我知道这个功能是:
function memLoadLibrary(FileBase : Pointer) : Pointer;
但我不知道如何使用它,我需要定义的FileBase等等。
任何人都可以帮助我吗?
答案 0 :(得分:4)
您只需将DLL放入内存,并将指向DLL的位置的指针传递给memLoadLibrary。
例如,来自资源:
hRes := LoadResource(HInstance, 'MYRESNAME');
if hres=0 then
RaiseLastOSError;
BaseAddress := LockResource(hRes);
if BaseAddress=nil then
RaiseLastOSError;
lib := memLoadLibrary(BaseAddress);
.....
答案 1 :(得分:2)
假设您希望从内存加载DLL中的过程和函数都使用它们:
** GLOBAL ** (both, exe -> dll)
type
TTest1Proc = record
Proc: procedure; StdCall;
hLib: THandle;
end;
TTest2Func = record
Func: function: Boolean; StdCall;
hLib: THandle;
end;
** DLL **
procedure Test1; StdCall;
begin
ShowMessage('Test proc');
end;
function Test2: Boolean; StdCall;
begin
Result := True;
end;
exports
Test1.
Test2;
这是你可以在.EXE项目中加载dll并使用这两种方法(过程和函数)的方法:
procedure Test1;
var
Test1Proc: TTest1Proc;
begin
with Test1Proc do
begin
hLib := LoadLibrary(PChar('DLL_PATH.dll'));
if hLib <> 0 then
begin
@Proc := GetProcAddress(hLib, 'Test1');
if Assigned(Proc) then
try
Proc; //will execute dll procedure Test1
finally
FreeLibrary(hLib);
end
else
ShowMessage('Procedure not found on DLL');
end
else
ShowMessage('DLL not found.');
end;
end;
作为功能:
function Test2: Boolean;
var
Test2Func: TTest2Func;
begin
Result := False;
with Test2Func do
begin
hLib := LoadLibrary(PChar('DLL_PATH.dll'));
if hLib <> 0 then
begin
@Func := GetProcAddress(hLib, 'Test2');
if Assigned(Func) then
try
Result := Func; //will execute the function Test2
finally
FreeLibrary(hLib);
end
else
ShowMessage('Function not found on DLL.');
end
else
ShowMessage('DLL not found.');
end;
end;