无法调用dll文件中的线程 [英] unable to call a thread in dll file
本文介绍了无法调用dll文件中的线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试创建一个dll,该DLL将在您加载他时创建一个线程 由于某种原因,线程函数没有执行任何操作..:\
I am trying to create a dll which will create a thread when you load him for some reason the thread function is not doing anything.. :\
这是我的代码:
dllthread!= null ..为什么不起作用?
dllthread != null.. why its not working?
#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;
}
推荐答案
不是从DllMain()
启动线程,而是导出将启动线程的函数:
Instead of starting the thread from DllMain()
export a function that would launch the thread instead:
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()
函数.
After calling LoadLibrary()
use GetProcAddress()
to get access to the start_thread()
function.
希望这会有所帮助.
这篇关于无法调用dll文件中的线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文