[英]How can I tell how my DLL was loaded?
我的DLL如何檢測它是隱式加載還是顯式加載?
示例MyTestDll.dll
library MyTestDll;
uses SimpleShareMem, Windows, Dialogs;
procedure DetectMethodDllLoad: bool;
begin
// ?????
// need to detect loading method - implicit or explicit
end;
procedure MyTest; stdcall;
begin
if DetectMethodDllLoad then
ShowMessage('Working Program1 (implicit dll load)')
else
ShowMessage('Working Program2 (explicit dll load)');
end;
exports MyTest;
begin
end.
Program1.exe(隱式dll加載)
procedure MyTest; stdcall; external 'MyTestDll.dll' Name 'MyTest';
procedure TForm1.Button1Click(Sender: TObject);
begin
MyTest;
end;
Program2.exe(顯式dll加載)
type
TMyTest = procedure; stdcall;
procedure TForm1.Button1Click(Sender: TObject);
var
MyTest: TMyTest;
H: HModule;
begin
H := LoadLibrary('MyTestDll.dll');
if H = 0 then
exit;
@MyTest := GetProcAddress(H, 'MyTest');
if Assigned(MyTest) then
MyTest;
FreeLibrary(H);
end;
如何實現DetectMethodDllLoad
?
如果可以創建DllMain過程,則DLL_PROCESS_ATTACH調用的lpReserved參數將告訴您負載是靜態的還是動態的。
http://msdn.microsoft.com/en-us/library/ms682583%28VS.85%29.aspx
您當然可以在C語言中執行此操作。我不知道在Delphi中是否可行。
這是一個很好的教程,介紹了靜態和動態兩種方法:
謝謝哈里·約翰斯頓!!! :)
library MyTestDll;
uses SimpleShareMem, Windows, Dialogs;
type
PDllEntryPointFrame = ^TDllEntryPointFrame;
TDllEntryPointFrame = packed record
hModule: THandle; // DLL module handle
dwReason: DWord; // reason for calling DLLEntryPoint function of DLL
bStatic: LongBool; // TRUE if DLL is loading/unloading satically, FALSE - dinamically
end;
function DetectMethodDllLoad: bool;
asm
mov edx, [hInstance]
mov eax, ebp
@@nextframe:
cmp [eax + $08].TDllEntryPointFrame.hModule, edx
je @@found
mov eax, [eax]
jmp @@nextframe
@@found:
mov eax, [eax + $08].TDllEntryPointFrame.bStatic
end;
procedure MyTest; stdcall;
begin
...
end;
exports MyTest;
begin
if DetectMethodDllLoad then
ShowMessage('Working Program1 (implicit dll load)')
else
ShowMessage('Working Program2 (explicit dll load)');
end.
ps System.TDLLProcEx在Delphi XE中不起作用 !
library MyTestDll;
....
procedure MyDLLProcEx(Reason:integer;x:pointer);
begin
if x=nil then showmessage('dyn') else showmessage('stat');
end;
begin
DLLProcEx:=@MyDLLProcEx;
end;
x =總是零:(
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.