创建、关闭线程的问题 创建一个新的线程,线程中加载一个dll。在这个线程外关闭这个线程。请问因该用什么方法? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 普通的方法:CreateThread()CloseHandle()需要判断是否加载完毕.. 创建是没有问题,可是CloseHandle()结束不了线程啊,我的线程中的程序是不会自动停止的 use the FreeLibraryAndExitThread function.The FreeLibraryAndExitThread function gives threads that are created and executed within a dynamic-link library an opportunity to safely unload the DLL and terminate themselves. DWORD WINAPI ThreadFunc( LPVOID lpParam ) { typedef bool (__stdcall *MYPROC)(); MYPROC Proc; BOOL fFreeResult, fRunTimeLinkSuccess = FALSE; // Get a handle to the DLL module. hinstLib = LoadLibrary("Caling.dll"); // If the handle is valid, try to get the function address. if (hinstLib != NULL) { Proc = (MYPROC) GetProcAddress(hinstLib, "Caling"); // If the function address is valid, call the function. if (fRunTimeLinkSuccess = (ProcAdd != NULL)) (ProcAdd)(); // Free the DLL module. fFreeResult = FreeLibrary(hinstLib); } // If unable to call the DLL function, use an alternative. if (! fRunTimeLinkSuccess) printf("message via alternative method\n"); return 0; } //创建hThread = CreateThread( NULL, // no security attributes 0, // use default stack size ThreadFunc, // thread function 0, // argument to thread function 0, // use default creation flags &dwThreadId); // returns the thread identifier //关闭DWORD dwReturnCode = 0;FreeLibrary(hinstLib); ExitThread(dwReturnCode);FreeLibrary 运行报错:未知的软件异常,runtime error 217 at 00f3ccd2 原来你代码是这样的哟。线程还在台上表演,你就舞台给折了,当然出错。改成下面:DWORD dwReturnCode = 0;ExitThread(dwReturnCode); // 先结束线程,FreeLibrary(hinstLib); // 再释放库 ExitThread是结束本线程的,你在主线程中调用当然结束的是主线程。 子线程可以用TerminateThread强制结束。另外一个方法(只是理论上,不过估计可行):SuspendThread挂起子线程, GetThreadContext获取子线程上下文, 修改其中的EIP把它指向ExitThread入口, SetThreadContext设置子线程上下文,ResumeThread... //创建hThread = CreateThread( NULL, // no security attributes 0, // use default stack size ThreadFunc, // thread function 0, // argument to thread function 0, // use default creation flags &dwThreadId); // returns the thread identifier Sleep(5000);等待5s//关闭DWORD dwReturnCode = 0;TerminateThread(hThread , dwReturnCode);FreeLibrary(hinstLib); 这次一定可以搞定了。 提醒一点,终止线程运行的最好办法是让线程自身退出而不是调用ExitThread或者TerminateThread。比如在线程的运行循环中检测一个变量或者事件来判断是否应该退出比强行终止线程要好。关于这个,可以看看windows核心编程6.5关于DLL的load问题,在调用LoadLibrary的线程退出的时候,除非是使用TerminateThread来退出的,否则其他情况下DLL会收到通知该线程被撤销,该DLL的DllMain函数将被调用,调用时带有参数DLL_THREAD_DETACH值。关于这个,可以看看windows核心编程20.2.4 回完帖才看到 Idle_(阿呆) 已经早我几分钟给出正确答案了。呵呵,慢了半拍。 To lisunlin0(李林):不到万不得已,千万不要使用TerminateThread来终止线程的运行,这种情况下线程是不能被正确的清除的windows核心编程6.5.3有具体说明 得到内存的地址绝对值,这样取法对不对? 关于WlxWkstaLockedSAS VC读取STL文件并用OpenGL显示 关于在VC里生成报表再打印的问题 急:问一个消息队列的问题 Java 在windows下 可以用 vc编写的ocx 吗 我想向access中插入一个日期,该怎么做? 关于操作注册表 RegSaveKey RegLoadKey 请问大哥们,半透明的窗体怎么做出来的? 高手近来:如何隐藏form的caption栏(祥见下) VS2005下编译通不过???--VS2003可以通过 模拟鼠标按下
CloseHandle()需要判断是否加载完毕..
{
typedef bool (__stdcall *MYPROC)();
MYPROC Proc;
BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;
// Get a handle to the DLL module.
hinstLib = LoadLibrary("Caling.dll");
// If the handle is valid, try to get the function address.
if (hinstLib != NULL)
{
Proc = (MYPROC) GetProcAddress(hinstLib, "Caling");
// If the function address is valid, call the function.
if (fRunTimeLinkSuccess = (ProcAdd != NULL))
(ProcAdd)();
// Free the DLL module.
fFreeResult = FreeLibrary(hinstLib);
}
// If unable to call the DLL function, use an alternative.
if (! fRunTimeLinkSuccess)
printf("message via alternative method\n");
return 0;
} //创建
hThread = CreateThread(
NULL, // no security attributes
0, // use default stack size
ThreadFunc, // thread function
0, // argument to thread function
0, // use default creation flags
&dwThreadId); // returns the thread identifier
//关闭
DWORD dwReturnCode = 0;
FreeLibrary(hinstLib);
ExitThread(dwReturnCode);FreeLibrary 运行报错:未知的软件异常,runtime error 217 at 00f3ccd2
改成下面:
DWORD dwReturnCode = 0;
ExitThread(dwReturnCode); // 先结束线程,
FreeLibrary(hinstLib); // 再释放库
另外一个方法(只是理论上,不过估计可行):
SuspendThread挂起子线程, GetThreadContext获取子线程上下文, 修改其中的EIP把它指向ExitThread入口, SetThreadContext设置子线程上下文,ResumeThread...
hThread = CreateThread(
NULL, // no security attributes
0, // use default stack size
ThreadFunc, // thread function
0, // argument to thread function
0, // use default creation flags
&dwThreadId); // returns the thread identifier Sleep(5000);等待5s
//关闭
DWORD dwReturnCode = 0;
TerminateThread(hThread , dwReturnCode);
FreeLibrary(hinstLib); 这次一定可以搞定了。
关于DLL的load问题,在调用LoadLibrary的线程退出的时候,除非是使用TerminateThread来退出的,否则其他情况下DLL会收到通知该线程被撤销,该DLL的DllMain函数将被调用,调用时带有参数DLL_THREAD_DETACH值。关于这个,可以看看windows核心编程20.2.4
不到万不得已,千万不要使用TerminateThread来终止线程的运行,这种情况下线程是不能被正确的清除的
windows核心编程6.5.3有具体说明