创建一个新的线程,线程中加载一个dll。在这个线程外关闭这个线程。请问因该用什么方法?

解决方案 »

  1.   

    普通的方法:CreateThread()
    CloseHandle()需要判断是否加载完毕..
      

  2.   

    创建是没有问题,可是CloseHandle()结束不了线程啊,我的线程中的程序是不会自动停止的
      

  3.   

    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.
      

  4.   

    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
      

  5.   

    原来你代码是这样的哟。线程还在台上表演,你就舞台给折了,当然出错。
    改成下面:
    DWORD dwReturnCode = 0;
    ExitThread(dwReturnCode); // 先结束线程,
    FreeLibrary(hinstLib);  // 再释放库
      

  6.   

    ExitThread是结束本线程的,你在主线程中调用当然结束的是主线程。 子线程可以用TerminateThread强制结束。
    另外一个方法(只是理论上,不过估计可行):
    SuspendThread挂起子线程, GetThreadContext获取子线程上下文, 修改其中的EIP把它指向ExitThread入口, SetThreadContext设置子线程上下文,ResumeThread...
      

  7.   

    //创建
    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);  这次一定可以搞定了。
      

  8.   

    提醒一点,终止线程运行的最好办法是让线程自身退出而不是调用ExitThread或者TerminateThread。比如在线程的运行循环中检测一个变量或者事件来判断是否应该退出比强行终止线程要好。关于这个,可以看看windows核心编程6.5
    关于DLL的load问题,在调用LoadLibrary的线程退出的时候,除非是使用TerminateThread来退出的,否则其他情况下DLL会收到通知该线程被撤销,该DLL的DllMain函数将被调用,调用时带有参数DLL_THREAD_DETACH值。关于这个,可以看看windows核心编程20.2.4
      

  9.   

    回完帖才看到 Idle_(阿呆) 已经早我几分钟给出正确答案了。呵呵,慢了半拍。
      

  10.   

    To lisunlin0(李林):
    不到万不得已,千万不要使用TerminateThread来终止线程的运行,这种情况下线程是不能被正确的清除的
    windows核心编程6.5.3有具体说明