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
9
votes
2 answers

DLL Main on Windows Vs. __attribute__((constructor)) entry points on Linux

Consider code EXE: int main () { printf("Executable Main, loading library\n"); #ifdef HAVE_WINDOWS HMODULE lib = LoadLibraryA ("testdll.dll"); #elif defined(HAVE_LINUX) void * lib = dlopen("testdll.so", RTLD_LAZY); #endif if…
Abhishek Jain
  • 9,614
  • 5
  • 26
  • 40
5
votes
1 answer

In which cases is the dynamic CRT not already initialized on call to user supplied DllMain?

Preamble: This question is specifically concerned with, and only with, the behavior of the dynamic CRT used through /MD. It does not question the validity of any other recommendations wrt. DllMain. As we've been told: (ref: Dynamic-Link Library…
Martin Ba
  • 37,187
  • 33
  • 183
  • 337
5
votes
1 answer

DLLMain() is not being executed after injection

I have written a dll and injector in C++. The dll code is given below: #include #include #include #include #include #include using namespace std; #pragma comment(lib,…
Faheem
  • 509
  • 2
  • 7
  • 23
4
votes
0 answers

Why would this singleton occasionally crash when invoked from DllMain?

I have the following class which is being compiled in Visual Studio 2015: class MitigationPolicyChecker { public: static auto& getInstance() { static MitigationPolicyChecker instance; return instance; } …
Benj
  • 31,668
  • 17
  • 78
  • 127
4
votes
3 answers

Which functions are called before DllMain()?

Which functions are called prior to DllMain()? If more than one during the C runtime initialization, then the order is important.
y2k
  • 65,388
  • 27
  • 61
  • 86
3
votes
0 answers

Adding custom dllmain to cythonized module

Consider the following scenario: A python tool should be deployed for external users in a way that (a) the source is protected and (b) it is ensured that only users with valid license can use it. Furthermore, for internal users, the code should be…
3
votes
3 answers

When will DllMain be called with the DLL_PROCESS_VERIFIER flag?

On Windows, the standard DLL entry point is called DllMain. The second parameter is a DWORD, ul_reason_for_call. I have looked up the possible values for this second parameter on MSDN. The following are…
Konrad
  • 39,751
  • 32
  • 78
  • 114
3
votes
1 answer

DllMain not being called from injected dll

I am attempting to inject a dll into an existing process using the LoadLibrary and CreateRemoteThread approach. All of my code is working beautifully except for the fact that DllMain is not being called for some reason. I've wracked my brains and…
blargfoot
  • 61
  • 1
  • 6
2
votes
1 answer

How to implement DllMain entry point in Go

I'm using Go to build a DLL. I'm not able to implement windows DllMain entry point. My goal is when an app loads the dll via LoadLibrary to call the Test method, the DllMain will be called as well. However currently the app is kind of stuck in…
fooBar
  • 412
  • 6
  • 17
2
votes
1 answer

Create a Delphi Dll and load it with DllMain

Friends Im with a little problem. Im trying to create a delphi Dll with a form in RAD Studio, but i don't know how to make it load with DllMain. I want to inject this Dll in a third-party process at runtime after. I created the Dll project with the…
BlueOrange
  • 21
  • 1
2
votes
1 answer

Need a notification that DLL is shutting down before getting DLL_PROCESS_DETACH

We have a DLL built in Visual Studio using C++11. Our DLL has a fixed API at this point, and it includes an open and close function (among others). We allow our users to open and close multiple times without closing the app. Inside our DLL, we…
Beast
  • 169
  • 2
  • 7
2
votes
1 answer

Set hook on LoadLibrary which was called from DllMain of hooked delayed-dll

My goal is hook all LoadLibrary calls from particular dll and its dependencies (which may be delayed importable). Here is how I'm trying to solve this task: Load this dll using DONT_RESOLVE_DLL_REFERENCES flag. 1.1. Apply this algorithm to dll's…
Dmitry Katkevich
  • 883
  • 7
  • 26
2
votes
2 answers

Why CreateProcess must not be called from a DllMain function?

I've read in several sources that CreateProcess must not be called from a DllMain function. CreateProcess : Do not call CreateProcess from a DllMain function. This causes the application to stop responding. Dynamic-Link Library Best Practices: You…
idanshmu
  • 5,061
  • 6
  • 46
  • 92
1
vote
1 answer

MSVC DLL loading: are __declspec(dllexport) functions loaded directly without DllMain

I tried to write a DLL project in VS C++ with some exported functions like below: extern "C" __declspec(dllexport) int function_sendNumber(unsigned num); I noticed VS project comes with file dllmain.cpp containing DllMain entry function. However,…
user5005768Himadree
  • 1,375
  • 3
  • 23
  • 61
1
vote
1 answer

c++ dll infinite loop without using a thread

is it possible to use an infinite loop in a dll function without using a thread? here's some example code: BOOL WINAPI DllMain(HMODULE hModule, DWORD dwReason, LPVOID lpReserved) { switch (dwReason) { case DLL_PROCESS_ATTACH: …
user13431061
1
2 3 4