我正在尝试创建一个dll,当你加载它时会创建一个线程 由于某种原因,线程函数没有做任何事情......:\
这是我的代码:
dllthread!= null ..为什么它无效?
#include "stdafx.h"
DWORD WINAPI ThreadProc(
__in LPVOID lpParameter
)
{
std::ofstream myfile;
myfile.open ("example.txt");
myfile << "Writing this to a file.\n";
myfile.close();
return 0;
}
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
DWORD DllThreadID;
HANDLE DllThread; //thread's handle
DllThread=CreateThread(NULL,0,&ThreadProc,0,0,&DllThreadID);
//
if (DllThread == NULL)
MessageBox(NULL, L"Error", L"Error", MB_OK);
CloseHandle(DllThread);
break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
答案 0 :(得分:11)
而不是从DllMain()
启动一个线程,而不是导出一个启动线程的函数:
extern "C" __declspec(dllexport) void start_thread()
{
DWORD DllThreadID;
HANDLE DllThread; //thread's handle
DllThread=CreateThread(NULL,0,ThreadProc,0,0,&DllThreadID);
if (DllThread == NULL)
MessageBox(NULL, L"Error", L"Error", MB_OK);
else
CloseHandle(DllThread);
}
致电LoadLibrary()
后,使用GetProcAddress()
访问start_thread()
功能。
希望这有帮助。