Server in dllmain #4039
-
|
Can I use ZeroC Ice server inside DllMain? In my current code, the library gets unloaded immediately after injection. HookerI* hooker;
DWORD WINAPI ServerThread(LPVOID) {
try {
MessageBoxA(NULL, "DLL Loaded", "Debug", MB_OK);
int argc = 0;
char** argv = nullptr;
Ice::CommunicatorHolder ich(argc, argv);
auto adapter = ich->createObjectAdapterWithEndpoints("HookerAdapter", "default -p 10000");
hooker = new HookerI;
adapter->add(hooker, Ice::stringToIdentity("Hooker"));
adapter->activate();
ich->waitForShutdown();
}
catch (const std::exception& ex) {
std::cerr << ex.what() << std::endl;
}
return 0;
}
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
if (ul_reason_for_call == DLL_PROCESS_ATTACH) {
DisableThreadLibraryCalls(hModule);
CreateThread(nullptr, 0, ServerThread, nullptr, 0, nullptr);
}
else if (ul_reason_for_call == DLL_PROCESS_DETACH) {
delete hooker;
}
return TRUE;
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
Looking at the DllMain documentation, there are significant restrictions on what can safely be done inside DllMain: https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-best-practices Given that, it’s unclear whether initializing an Ice server from DllMain can work reliably—especially since it may indirectly perform operations (like loading other libraries or starting threads) that are not safe during DLL initialization. Have you checked the Windows Event Viewer for any related errors? |
Beta Was this translation helpful? Give feedback.
Looking at the DllMain documentation, there are significant restrictions on what can safely be done inside DllMain:
https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-best-practices
Given that, it’s unclear whether initializing an Ice server from DllMain can work reliably—especially since it may indirectly perform operations (like loading other libraries or starting threads) that are not safe during DLL initialization.
Have you checked the Windows Event Viewer for any related errors?