1

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?

8
  • 3
    Your question is not clear. Can you rephrase it? Sep 6, 2011 at 19:52
  • 2
    DLLs use dynamic linking rather than static linking. Perhaps the distinction you wish to draw is between implicit and explicit linking. No matter, there is no question here and I vote to close. Sep 6, 2011 at 19:57
  • Well, now I will add an example
    – Gu.
    Sep 6, 2011 at 20:19
  • 1
    the close votes racked up before you updated the question. I understand you now. Sep 6, 2011 at 20:57
  • 1
    @David - The question is not about linking it's about loading. I don't think there's anything wrong with dynamic/static loading of a dll terminology-wise. Sep 6, 2011 at 22:40

3 Answers 3

3

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.

5
  • Yes, this that is necessary. But how it to make on Delphi? ;)
    – Gu.
    Sep 7, 2011 at 0:52
  • 1
    Assign a handler to the SysInit.DllProcEx callback in your DLL's initialization section. The problem, however, is that the DllProc/Ex callbacks are not called for DLL_PROCESS_ATTACH because they have not been assigned yet at that time. You could try processinging for the first DLL_THREAD_ATTACHevent though. Sep 7, 2011 at 1:17
  • 1
    But @Remy, the thread-attach call doesn't define the lpReserved parameter. That's only set for process-attach. Sep 7, 2011 at 7:53
  • The SysInit.DllProc/Ex callbacks are the only way Delphi exposes access to the DllMain() parameters, but DLL_PROCESS_ATTACH is triggered before the callbacks can be assigned in user code, and Delphi does not cache the lpReserved value anywhere, so you are out of luck in Delphi. Switch to C++ :-) Sep 7, 2011 at 18:35
  • You can use Delphi .pas files in a C++ project, but you cannot use C++ .c/.cpp files in a Delphi project. You would have to create a C++ project to solve this issue, as you cannot replace Delphi's default DllMain() handler with your own code like you can in C++. Sep 7, 2011 at 23:35
1

Here is a very good tutorial with the 2 methods, static and dynamic:

Static vs. Dynamic Dynamic Link Library Loading - A Comparison

6
  • The correct terminology is implicit linking when the imports are included in the loading modules import table and explicit linking when LoadLibrary/GetProcAddress is used. Static linking is what you get with $LINK. You can call it static linking if you really want to, but that is not the official terminology. Sep 6, 2011 at 20:03
  • 1
    If you wish to learn more about this topic see here: msdn.microsoft.com/en-us/library/253b8k2c(VS.80).aspx Sep 6, 2011 at 20:05
  • ok. thx for the terminology and your link. BTW, this is written like this in the doc.
    – Whiler
    Sep 6, 2011 at 20:07
  • Also there is delay load without writing any LoadLibrary code.
    – Warren P
    Sep 6, 2011 at 20:24
  • 2
    See 'DLL_PROCESS_ATTACH' in the documentation of DllMain Entry Point where MS uses "dynamic loading" and "static loading" terms for DLLs. Sep 6, 2011 at 22:54
-1

Thanks Harry Johnston!!! :)

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.  

P.s. System.TDLLProcEx not working in 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 = nil always :(

14
  • 1
    -1 for several reasons: First, there's no explanation of what this code does (or why Harry Johnston has anything to do with it). Second, I happen to understand what that code is doing, and I cannot endorse spelunking in undocumented call-stack structures, at least not without some rationale for why we should expect it to be valid in any particular version of Delphi. Third, the code meant to demonstrate the correct answer doesn't work, by your own admission. This answer will harm or mislead the people who come here for an answer later. Sep 7, 2011 at 7:51
  • @Gu: DllProcEx is not called automatically for DLL_PROCESS_ATTACH, you have to call it manually after assigning DllProcEx. Delphi does not expose access to the Reserved value of the DLL_PROCESS_ATTACH event. Sep 7, 2011 at 18:37
  • @Rob, presumably Delphi can be trusted to use the standard Windows call stack, at least provisionally? What I don't understand is why the call to DetectMethodDllLoad is happening from within the DllMain function in the first place, can someone clarify this? With the restrictions on what you can do inside DllMain you would need to be very careful about using runtime library functions. Sep 7, 2011 at 20:17
  • @Rob, I think Gu means that the second piece of code doesn't work, not that the first piece doesn't. Sep 7, 2011 at 20:18
  • @Harry, System.pas and SysInit.pas do some funny things. They don't necessarily adhere to the usual calling conventions. The detection function is called from the DLL's entry point because that's the only place where the lpReserved parameter will be available. You can't call it from any exported function because then the entry-point parameters won't be on the stack anymore. Other RTL functions are fine; it's DLL functions you need to avoid. DetectMethodDllLoad calls none. And maybe I should have said accepted instead of correct; I know the second code is what Gu was referring to. Sep 7, 2011 at 20:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.