9

I'm having a lot of trouble dealing with a DLL I've written in Delphi. I've set up a DllMain function using the following code in the library:

begin
  DllProc := DllMain;
end.

My DllMain procedure looks like this:

procedure DllMain(reason: Integer);
begin
  if reason = DLL_PROCESS_DETACH then
    OutputDebugString('DLL PROCESS DETACH')
  else if reason = DLL_PROCESS_ATTACH then
    OutputDebugString('DLL PROCESS ATTACH')
  else if reason = DLL_THREAD_ATTACH then
    OutputDebugString('DLL THREAD ATTACH')
  else if reason = DLL_THREAD_DETACH then
    OutputDebugString('DLL THREAD DETACH')
  else
    OutputDebugString('DllMain');
end;

What I'm finding is that DETACH seems to be called (twice?!) by a caller (that I don't control) before ATTACH is ever called. Is that even possible, or am I misunderstanding how this is supposed to work? My expectation would be that every ATTACH call would be met with a matching DETACH call, but that doesn't appear to be the case.

What's goin' on here?!

2 Answers 2

14

Unfortunately when begin is executed in your dll code, the OS has already called DllMain in your library. So when your DllProc := DllMain; statement executes it is already too late. The Delphi compiler does not allow user code to execute when the dll is attached to a process. The suggested workaround (if you can call that a workaround) is to call your own DllMain function yourself in a unit initalization section or in the library code:

begin
  DllProc := DllMain;
  DllMain(DLL_PROCESS_ATTACH);
end;

The relevant documentation:

Note: DLL_PROCESS_ATTACH is passed to the procedure only if the DLL's initialization code calls the procedure and specifies DLL_PROCESS_ATTACH as a parameter.

3
  • Hmm... I was hoping this would be the solution to all of my problems, but this additional knowledge (although true and answering my question) hasn't yet fixed my issues. Looks like I'll have to create another question :). Thanks for your help!
    – aardvarkk
    Apr 30, 2012 at 1:27
  • What don't you understand. It's all here right in this question. The only invocation of DllMain that occurs before your code can execute is for DLL_PROCESS_ATTACH. So you just write it the way Sertac says and it's all good. Apr 30, 2012 at 1:36
  • No, this is entirely clear. I understand it and could implement it. It has revealed, though, that the answer to my larger problem (which I was hoping was related to this), is not in fact related to this problem. So the search continues!
    – aardvarkk
    Apr 30, 2012 at 1:39
-1

What I'm finding is that DETACH seems to be called (twice?!) by a caller (that I don't control) before ATTACH is ever called.

According to "Programming Windows 5th edition" by Petzold.
DLL_PROCESS_ATTACH gets called when the application starts and
DLL_THREAD_ATTACH when a new thread inside an attached application is started.
DLL_PROCESS_DETACH gets called when an application attached to your application quits.
DLL_THREAD_DETACH gets called when a thread inside an attached application quits.

Note that it is possible for DLL_THREAD_DETACH to be called without a corresponsing earlier DLL_THREAD_ATTACH.
This occurs when the thread was started prior to the application linking to the dll.
This mainly occurs when an application manually loads the dll using LoadLibrary instead of statically linking at compile time.

2
  • I don't understand. Do you mean a thread without a process? And 'LoadLibrary' won't lead to a 'DLL_PROCESS_ATTACH'? Nov 5, 2014 at 15:13
  • This doesn't seem to explain why DLL_PROCESS_DETACH is happening multiple times.
    – nobody
    Nov 5, 2014 at 17:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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