Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usage limitations during the DllMain Attach and Detach process

Tags:

c++

dll

winapi

mfc

One colleague of mine has troubles during the DllMain Detach process. His bug seems not to appear in all cases, but fairly often.

While trying to help him, I kind of remembered of some usage limitations during the DllMain Attach and Detach process, but I am not sure I remember well since it was 2 year old technical discussions and it was not me working on thoses termination issues.

Namely I kind of remember that we should:

  • Avoid using new and delete operator and prefer HGLOBAL memory allocation
  • Avoid dealing with thread terminations here.

Could you correct me if I am wrong, explain me if ever, or point to a technical article that would deal with these issues.

like image 984
Stephane Rolland Avatar asked Apr 29 '11 16:04

Stephane Rolland


People also ask

Is DllMain required?

DllMain is not mandatory. If you have some initialization code required to run when loading the dll, you should create a DllMain function, and treat the initialization there. Otherwise it's not required.

What part of a DLL's entry point will be called during a call to LoadLibrary?

For each DLL that has not already been called with the DLL_PROCESS_ATTACH value, the system calls the DLL's entry-point function. This call is made in the context of the thread that caused the process address space to change, such as the primary thread of the process or the thread that called LoadLibrary.

Can a DLL have a main function?

It is just code and does not have an execution thread of its own. The thread execution starts in the program with main() (or WinMain). The program can choose to do all sorts of things, like loading a DLL and calling functions in it, or spawning other threads, or starting other programs and so on.


3 Answers

Avoid calling LoadLibrary and related APIs.

In addition to Steve's link, here are some good relevant posts from Raymond Chen's The Old New Thing:

  • https://devblogs.microsoft.com/oldnewthing/20040127-00/?p=40873
  • https://devblogs.microsoft.com/oldnewthing/20040128-00/?p=40853
  • https://devblogs.microsoft.com/oldnewthing/20070904-00/?p=25283
  • https://devblogs.microsoft.com/oldnewthing/20100115-00/?p=15253
like image 174
sean e Avatar answered Nov 14 '22 22:11

sean e


Most problems arise due to conflicts over the loader lock. DllMain should not be long-running, or use locks if it's avoidable.

Good background here.

like image 43
Steve Townsend Avatar answered Nov 14 '22 23:11

Steve Townsend


Find documents with topics

[1] "Dll Main Entry Point"

[2] "Constraints of Delay Loading DLLs"

[3] "Dynamic-Link Library Best Practices"

[4] Jefrey Richter, Windows via C++, chapter 20.

(Sorry, I can not give URL references due to stackoverflow policy)

Summary

  1. Maybe other DllMain have already been executed, maybe not. Don't call functions from other DLL's

  2. Don't call the following things: "FreeLibrary/LoadLibrary/CreateProcess/ExitThread/GetStringType"

  3. Don't call functions from User32.dll, Gdi32.dll

  4. If CRT is not initialized don't use memory management functions from it (my opinion that is restricted only for initialization phase)

  5. You should understand in which thread context you are from documentation.

  6. It is legal to do the following: Create and initialize synchronization objects. Open, read from, and write to files.

like image 29
Konstantin Burlachenko Avatar answered Nov 14 '22 22:11

Konstantin Burlachenko