Home > Windows > DLL Best Practices

DLL Best Practices

Following information taken from this document maintained by microsoft!

An important excerpt from this document!

DllMain is called while the loader-lock is held. Therefore, significant restrictions are imposed on the functions that can be called within DllMain. As such, DllMain is designed to perform minimal initialization tasks, by using a small subset of the Microsoft® Windows® API. You cannot call any function in DllMain that directly or indirectly tries to acquire the loader lock. Otherwise, you will introduce the possibility that your application deadlocks or crashes. An error in a DllMain implementation can jeopardize the entire process and all of its threads.

Don’t do’s in DllMain of a Dll!

  1. Call LoadLibrary or LoadLibraryEx (either directly or indirectly). This can cause a deadlock or a crash.
  2. Synchronize with other threads. This can cause a deadlock.
  3. Acquire a synchronization object that is owned by code that is waiting to acquire the loader lock. This can cause a deadlock.
  4. COM threads by using CoInitializeEx. Under certain conditions, this function can call LoadLibraryEx.
  5. Call the registry functions. These functions are implemented in Advapi32.dll. If Advapi32.dll is not initialized before your DLL, the DLL can access uninitialized memory and cause the process to crash.
  6. Call CreateProces. Creating a process can load another DLL.
  7. Call ExitThread. Exiting a thread during DLL detach can cause the loader lock to be acquired again, causing a deadlock or a crash.
  8. Call CreateThread. Creating a thread can work if you do not synchronize with other threads, but it is risky.
  9. Create a named pipe or other named object (Windows 2000 only). In Windows 2000, named objects are provided by the Terminal Services DLL. If this DLL is not initialized, calls to the DLL can cause the process to crash.
  10. Use the memory management function from the dynamic C Run-Time (CRT). If the CRT DLL is not initialized, calls to these functions can cause the process to crash.
  11. Call functions in User32.dll or Gdi32.dll. Some functions load another DLL, which may not be initialized.
  12. Use managed code.

Safe things to do from DllMain of Dll!

  1. Initialize static data structures and members at compile time.
  2. Create and initialize synchronization objects.
  3. Allocate memory and initialize dynamic data structures (avoiding the functions listed above.)
  4. Set up thread local storage (TLS).
  5. Open, read from, and write to files.
  6. Call functions in Kernel32.dll (except the functions that are listed above).
  7. Set global pointers to NULL, putting off the initialization of dynamic members. In Microsoft Windows Vista™, you can use the one-time initialization functions to ensure that a block of code is executed only once in a multithreaded environment
Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: