Questions tagged [dllmain]

In a Windows DLL, the DllMain function is automatically run when the DLL is loaded, just before it is unloaded, and whenever a process thread is started or exits cleanly.

In a Windows DLL, the DllMain function is automatically run when the DLL is loaded, just before it is unloaded (provided the process either explicitly unloads the DLL or exits cleanly) and whenever a process thread is started or exits cleanly.

DllMain is a placeholder for the function name defined by the library. The actual name must be specified when building the DLL. Recommended practice, where possible, is for a library to provide an explicit initialization function for the caller to use rather than using DllMain.

The DllMain function must be used cautiously, as only certain Win32 API functions are safe to call from DllMain. In particular, it must not call API functions that are not in kernel32.dll and it must not cause another DLL to be loaded either directly or indirectly. Communication with other threads or processes is also prohibited.

It is known to be safe to create synchronization objects and to use TLS (Thread Local Storage) in DllMain, so these are the most common uses. Another common use is to allocate per-thread objects. In C++, the runtime library will typically create global and static objects for your DLL using DllMain.

48 questions
1
vote
1 answer

Qt widgets not show up when Qt shared lib loaded

Requirements: Qt widgets show up when Qt shared lib loads, for none-Qt application. After some web searching, I found: All Qt widgets must live in "main thread", the "main thread" is the first Qt object created thread. so, create a none-Qt thread…
vg0x00
  • 588
  • 4
  • 11
1
vote
2 answers

A DLL should free heap memory only if the DLL is unloaded dynamically?

Question Purpose: Reality check on the MS docs of DllMain. It is "common" knowledge that you shouldn't do too much in DllMain, there are definite things you must never do, some best practises. I now stumbled over a new gem in the docs, that makes…
Martin Ba
  • 37,187
  • 33
  • 183
  • 337
1
vote
1 answer

Safe place to put unsafe DLL cleanup code on Windows?

We hit a case where it would be the best solution for us to put a FreeLibrary call into DllMain / DLL_PROCESS_DETACH. Of course, you must not do that: It is not safe to call FreeLibrary from DllMain. The use case is that we have a situation like…
Martin Ba
  • 37,187
  • 33
  • 183
  • 337
1
vote
1 answer

When calling D3D's CREATEDEVICE from inside DLLMAIN in VC++, it creates a deadlock(loaderlock?). Is there a way to overcome this? End goal inside

A while back I made a post regarding creating a dll, for the purpose of injection, that will cause the host application to trigger an Nvidia Optimus laptop to "awaken" the dGpu. This being necessary because of the pathetic system nvidia created here…
1
vote
1 answer

C# Implementing DllMain with DllExport

I'm using UnmanagedExports By RobertGiesecke I want to export DllMain entrypoint. Here what I've tried [DllExport("DllMain", CallingConvention.StdCall)] public static bool DllMain(IntPtr hModule, uint dwReason, byte[] lpReserved) { // I Write…
moien
  • 999
  • 11
  • 26
1
vote
2 answers

Calling managed code from DLLMain

I'm writing a unmanaged dll (with c++ and WinAPI) but I want to use some C# methods, so, I created a wrapper using C++/CLI. But the problem is: The unmanaged dll will be 'injected' (LoadLibrary) and I'm stuck here with no clue of how I can call the…
hendoe
  • 153
  • 12
1
vote
1 answer

Why does DLL_THREAD_DETACH happen twice?

I am learning COM. I wrote a simple COM component in the DLL and registered it in the registry. Then I created a simple client and tried to use my COM component. But I don't understand the DllMain behaviour (I read this also). extern "C" BOOL WINAPI…
Andrey Bushman
  • 11,712
  • 17
  • 87
  • 182
1
vote
1 answer

C++ DllMain API calls

This is probably something obvious but it doesn't make much sense to me. I'm trying to create a dll which is to be injected into a game process using C++. I have read that I shouldn't be calling anything like CreateThread from this method because…
Jordan
  • 117
  • 1
  • 12
1
vote
0 answers

Dynamic-Link Library Best Practices - How to avoid deadlock?

Background I've read Dynamic-Link Library Best Practices and understood what I can and can't do in DllMain. Now, say I have a visual studio 2013 solution containing many projects. each project generates different binaries/lib files. Say I have some…
idanshmu
  • 5,061
  • 6
  • 46
  • 92
1
vote
1 answer

instancing com object allowed in dllmain() / DLL_PROCESS_ATTACH?

Loading DLLs in in DLLMAIN() / DLL_PROCESS_ATTACH may cause trouble. But may COM Objects be instanced using e.q. CoInitialize() / CoCreateInstance()? EDIT: The question is: Could creating COM instances cause similar errors like loading DLLs in this…
marsh-wiggle
  • 2,508
  • 3
  • 35
  • 52
0
votes
0 answers

Is there any way to compile a DLL in Go with a DllMain function?

Basically I want to make a DLL in Go that works with, obviously Go code but can be executed by calling the DllMain function (e.g. "rundll32.exe main.dll DllMain"). Strangely I didn't find any solution to this. The only answer I found to this problem…
KuliYaga
  • 1
  • 1
0
votes
2 answers

How to wait for a thread created in DLLmain to finish?

I create a new thread in Dllmain() by the CreateThread() API, which does not involve thread synchronization, it is only a separate thread. Dllmain() invokes WaitForSingleObject(funcThread, INFINITE); to force the main thread to wait for funcThread…
0
votes
0 answers

How to abort global objects destruction from DllMain?

I need to find a reliable way to skip the global object deinitialization that happens just after DllMain has been called with DLL_PROCESS_DETACH. From the Microsoft Dynamic-Link Library Best Practices, we find a list of tasks that you should never…
Giovanni Cerretani
  • 1,693
  • 1
  • 16
  • 30
0
votes
1 answer

DllMain() not called when injecting but called with LoadLibrary()

Dll Code: #include BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) { MessageBoxW(NULL, L"Hello world!", L"Test DLL", MB_OK); return TRUE; } LoadLibrary Code: #include int main() { …
Cu29p
  • 51
  • 1
  • 4
0
votes
0 answers

My dialog box doesn't show when I try to display it in DllMain

I have created a Win32 dll in Visua Studio 2015 that contains a dialog box, I use the hModule passed to DllMain () and use ShowWindow () to actually show the window, but dialog box doesn't show up. I use LoadLibrary() to load this DLL in another…
G-H-T
  • 11
  • 2