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
0
votes
0 answers

Why am i getting "LoaderLock" detection?

i am calling a c++ DLL (not my code) from my c# code using my P/Invoked LoadLibrary API. but when i run the application, a LoaderLock error is being detected. at first i blame my DllImport C# wrappers for this, but when i try to load other C++ DLL,…
Rhen Bon
  • 87
  • 7
0
votes
2 answers

How to start a thread in DLLMain?

How can I start a thread in DLLMain means std :: thread - fundamentally. No means WinApi, and STL means. When I run the function in the flow, then I crash the application is called from this DLL. Thank you in advance. This code gets the hash sum on…
continue98
  • 29
  • 4
0
votes
2 answers

Call dll file with and without visual studio

Here is my code - #include "main.h" #include using namespace std; extern "C" __declspec(dllexport) BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) { ofstream outfile; …
Ujjaval Moradiya
  • 222
  • 1
  • 12
0
votes
0 answers

C++ Code in DLL_PROCESS_DETACH in DllMain is not being executed

I have a client and a server written in C++. The client is in dll and the server is in .exe. I first start the server and then inject the dll in the explorer.exe with the client in it. When I start the client and send a message in the…
Nikolaj
  • 145
  • 3
  • 14
0
votes
0 answers

How to avoid DLL Main?

I have asked this before, although people ignored it. Then again it was a lengthy topic, therefore I'll try to keep this shorter. When you Inject a DLL into a program you do not own, how can you avoid using DLL Main? A lot of you may know that when…
user1591117
  • 287
  • 2
  • 5
  • 13
0
votes
2 answers

Error with apientry dllmain

I try to create *.dll file in Borland c++ 5.02, i have problem with this function BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { return…
cahyo16
  • 1
  • 1
  • 1
0
votes
1 answer

Loading/calling ntdll from DllMain

One should not use functions other than those in kernel32.dll from DllMain: From MS documentation: Because Kernel32.dll is guaranteed to be loaded in the process address space when the entry-point function is called, calling functions in…
Roland Pihlakas
  • 4,246
  • 2
  • 43
  • 64
0
votes
0 answers

Why does dllMain overrides the control-word of my FPU?

I've seen here and here that dllMain changes the control-word of my FPU, and I need to: _control87(_CW_DEFAULT, _CW_DEFAULT); so it will return to normal. But no one states why it is done. Does anyone know why does my dllMain do that? On what…
speller
  • 1,641
  • 2
  • 20
  • 27
0
votes
1 answer

Why does calling WSASocket from DllMain lead to a hang?

I need to destroy some objects when my DLL is unloaded. This object contains a thread that sometimes calls the WSASocket function (for reconnecting a connection). So, I call the destructor from DllMain in response to DLL_PROCESS_DETACH, but that…
-1
votes
1 answer

Running libcurl http request from DllMain() in a DLL file on a separate thread

#pragma once #include #include #include #include "json.hpp" inline std::string GetData() { CURL* curl = curl_easy_init(); std::ofstream file; file.open("C:/google_logs/test10.txt"); file << "t"; …
Ryan Glenn
  • 1,325
  • 4
  • 17
  • 30
-1
votes
1 answer

'glfwCreateWindow' freezes when called in Dllmain

When I call glfwCreateWindow in DllMain, the program freezes and CPU usage drops to 0%. My code works fine if I change the type of program from .dll to .exe, and replace DllMain with main. Here is part of my code: BOOL WINAPI DllMain( HINSTANCE…
vsnm
  • 1
-1
votes
2 answers

Removing extra files generated while creating a dll project in visual studio

While creating a dll project in VS17, I see multiple files were created on initialization. But whichever project on C++ I work on, I don't see any such files in their environment. How can I get rid of these files in my environment. Is there any…
user12595757
-1
votes
1 answer

CreateRemoteThread succeeded, but LoadLibrary failed for some target app

I am using CreateRemoteThread() + LoadLibrary() method to inject code. Everything is OK when I running my injector in my Windows7 64bit OS laptop, and it still work in Windows Server 2012 R2 64bit for some target app. BUT, in this Windows Server…
Gang Li
  • 37
  • 10
-1
votes
1 answer

DllMain DLL_PROCESS_DETACH and GetMessage Function reentrancy

I have written a global hook that hooks using SetWindowsHookEx the WH_GETMESSAGE, WH_CALLWNDPROC and WH_CALLWNDPROCRET. The hook dll creates a new thread in the hooked process, which, among other things, checks the audio state of the process and…
Roland Pihlakas
  • 4,246
  • 2
  • 43
  • 64
-2
votes
1 answer

"LNK2005 DLLMain already defined ..." conflict in Linker using MSVC

I have a C++ project which up to now had no problems compiling and linking but due to a recent computer crash I had to re-install everything including Visual Studio and all my VCPKG packages. (I am using VS 2022 Preview, and was before.) My project…
David A
  • 123
  • 5