Multithreading in a dll

I wanted to make two functions running in the background at the same time inside dll

My dll

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

#include <Windows.h>
#include <iostream>



DWORD WINAPI MainThread(LPVOID param)
{


	AllocConsole();
	FILE* f;
	freopen_s(&f, "CONOUT$", "w", stdout);
	while (true)
	{
		Sleep(10);
		


	}


	fclose(f);
	FreeConsole();



	FreeLibraryAndExitThread((HMODULE)param, 0);

	return 0;
}
BOOL WINAPI DllMain(HINSTANCE hModule, DWORD dwReason, LPVOID lpReserved) {
	switch (dwReason) {
	case DLL_PROCESS_ATTACH:
		CreateThread(0, 0, MainThread, hModule, 0, 0);
		break;
	}

	return TRUE;
}
Last edited on
My guess is that the problem has to do with the Infinite loop. You need the thread to be capable of ending and returning to DllMain.

The DllMain function also does not wait for the thread to finish, often called "joining". You may be exiting the Dll before the thread can run fully, which probably is not the desired behavior.

See this example: https://learn.microsoft.com/en-us/windows/win32/procthread/creating-threads
Note that the threadArray is collecting the thread handles returned by createThread. They use that array of thread handles and a call to WaitForMultipleObjects() function for them all to finish, that is a blocking function that joins all the threads before continuing.

I find the C++ threads library easier to work with than the Windows API equivalent, see here: https://legacy.cplusplus.com/reference/thread/thread/

Have you made any more headway since posting?
Last edited on
You can't create threads in DllMain(). If you want something like this, then the dll has to export a function that does what is required which is called by the main program. See:
https://stackoverflow.com/questions/48809595/how-to-create-threads-in-dlls-c
thanks newbieg
No, I haven't made any headway
any way thanks so much ..

Thanks seeplus
The problem is dll will be injected into the game
I find the C++ threads library easier to work with than the Windows API equivalent


Personally I prefer WIN32 threads - probably because I've used them since Win32 first appeared, our code is heavily threaded, we're Windows only and there's no ROI for us on changing to C++ threads which would involve some substantial across-the-board code changes.

However, for new systems I'd always recommend standard C++ over 3rd party libs (even though C++ threading in the case of Windows will be implemented using WIN32 APIs).

PS. When creating a thread using WIN32, always use _beginthreadEx() and NOT CreateThread(). CreateThread() may seem to work but has limitations (around CRT) which could well bite hard at some point.
Last edited on
If you're trying to inject a dll into a remote process, have you read Richter's books on Advanced Windows? Starting from the 3rd Edition he goes into detail as to how to do it.
https://www.amazon.co.uk/Advanced-Windows-NT-Jeffrey-Richter/dp/1572315482/ref=sr_1_3
https://www.amazon.co.uk/Windows%C2%AE-via-C-PRO-Developer/dp/0735624240/ref=sr_1_1

You have made a great effort for my sake, thank your
Topic archived. No new replies allowed.