user6632482
user6632482

Reputation: 19

Multithreading in a dll

I wanted to make two functions running in the background at the same time inside dll so I did CreateThread in dllmain but it doesn't work. Any tips or help?

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

void main()
{
AllocConsole();
freopen("CONOUT$", "w", stdout);
std::cout << "Press enter?";
while (true) {
    if (GetAsyncKeyState(0x0D))
        Trainer(); Sleep(50);
}
}

BOOL WINAPI DllMain(HINSTANCE hinstDll, DWORD Reason, LPVOID Reserved) {
   switch (Reason) {

case DLL_PROCESS_ATTACH:
    MessageBox(0, "DllInject", "Injected", 0);
    CreateThread(0, 0, (LPTHREAD_START_ROUTINE)main, NULL, 0, NULL);
    break;

case DLL_PROCESS_DETACH:
    break;

}
return TRUE;
}

function example

void artemis()
{
    while (true)
    {
        Sleep(300);
        ammoArtemis = 1;
    }
}

Upvotes: 0

Views: 3833

Answers (2)

Martin Drab
Martin Drab

Reputation: 697

Before creating the thread, disable thread notifications for your DLL

DisableThreadLibraryCalls(hinstDll)

That should help with the deadlock.

Your thread seems to start in the function named main. Thread start routines, however, need to use WINAPI calling convention and should accept one parameter.

DWORD WINAPI main(PVOID Parameter)

You provide the parameter value by the fourth parameter.

Upvotes: 0

cybosser
cybosser

Reputation: 91

You shouldn't call CreateThread from DllMain because it might lead to deadlock. The set of function you can call from within DllMain is very limited. For details please read the article Dynamic-Link Library Best Practices.

Upvotes: 1

Related Questions