Вызов CreateThread в DllMain

Категория: C / C++ Комментариев: 1

Возникла необходимость создавать новый поток прямо во время загрузки динамической библиотеки, в функции DllMain. Взял, создал, скомпилировал, запустил — поток не создается. Точнее сказать поток создается, но не стартует.
Гугл сказал по этому вопросу следующее:
You should never perform the following tasks from within DllMain:

  • Call LoadLibrary or LoadLibraryEx (either directly or indirectly). This can cause a deadlock or a crash.
  • Synchronize with other threads. This can cause a deadlock.
  • Acquire a synchronization object that is owned by code that is waiting to acquire the loader lock. This can cause a deadlock.
  • Initialize COM threads by using CoInitializeEx. Under certain conditions, this function can call LoadLibraryEx.
  • 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.
  • Call CreateProces. Creating a process can load another DLL.
  • Call ExitThread. Exiting a thread during DLL detach can cause the loader lock to be acquired again, causing a deadlock or a crash.
  • Call CreateThread. Creating a thread can work if you do not synchronize with other threads, but it is risky.
  • 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.
  • 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.
  • Call functions in User32.dll or Gdi32.dll. Some functions load another DLL, which may not be initialized.
  • Use managed code.

То есть перед созданием потока динамическая библиотека должна быть проинициализирована, для чего и служит функция DllMain.
Вывод: в DllMain создавать новые потоки нельзя.
Чтобы создать новый поток в динамической библиотеке, создаем новую экспортируемую функцию и описываем процедуру создания потока в ней.

Автор: Кто-то   @   11 марта 2010 Комментариев: 1
Метки : , ,

Поблагодарить автора

Webmoney Z163628999150, R617151845974

Комментариев: 1

Комментарии
Янв 7, 2012
6:14
#1 kappz :

если писать на чистом WinAPI то создать поток в DLLMain можно,

для этого перед CteatheThread нужно вызвать DisableThreadLibraryCalls

оставить комментарий

Предыдущая запись
«
Следующая запись
»