我如何知道我的DLL是如何加载的? [英] How can I tell how my DLL was loaded?

查看:125
本文介绍了我如何知道我的DLL是如何加载的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的DLL如何检测是否被隐式或明确加载?

 库MyTestDll; 

使用SimpleShareMem,Windows,Dialogs;

procedure DetectMethodDllLoad:bool;
begin
//
//需要检测加载方法 - 隐式或显式
end;

程序MyTest; STDCALL;
begin
如果DetectMethodDllLoad然后
ShowMessage('Working Program1(implicit dll load)')
else
ShowMessage('Working Program2(explicit dll load)');
结束

出口MyTest;

开始
结束。

Program1.exe(隐式dll加载)

 程序MyTest; STDCALL;外部'MyTestDll.dll'名称'MyTest'; 

procedure TForm1.Button1Click(Sender:TObject);
begin
MyTest;
结束

Program2.exe(显式dll加载)

  type 
TMyTest = procedure; STDCALL;

procedure TForm1.Button1Click(Sender:TObject);
var
MyTest:TMyTest;
H:HModule;
begin
H:= LoadLibrary('MyTestDll.dll');
如果H = 0,那么
exit;
@MyTest:= GetProcAddress(H,'MyTest');
如果分配(MyTest)然后
MyTest;
FreeLibrary(H);
结束

如何实现 DetectMethodDllLoad


解决方案

如果您可以创建DllMain过程,则DLL_PROCESS_ATTACH调用的lpReserved参数将告诉您负载是静态还是动态。 / p>

http://msdn.microsoft.com/en-us/library/ms682583%28VS.85%29.aspx



你当然可以这样做在C.我不知道是否可能在Delphi。


How can my DLL detect whether it was loaded implicitly or explicitly?

Example 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 (implicit dll load)

procedure MyTest; stdcall; external 'MyTestDll.dll' Name 'MyTest';

procedure TForm1.Button1Click(Sender: TObject);
begin
  MyTest;
end;

Program2.exe (explicit dll load)

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;

How can I implement DetectMethodDllLoad?

解决方案

If you can create a DllMain procedure, the lpReserved parameter for the DLL_PROCESS_ATTACH call will tell you whether the load is static or dynamic.

http://msdn.microsoft.com/en-us/library/ms682583%28VS.85%29.aspx

You could certainly do this in C. I don't know whether it is possible in Delphi.

这篇关于我如何知道我的DLL是如何加载的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆