寫了一個C#的程序,調用了一個C++的dll,在C++的DLLMain中下面這樣寫的,特意在DLL_PROCESS_DETACH中關閉了線程,但那個MessageBox在關閉C#程序時並沒有彈出來,是不是意味著我所有在dll中開的線程都沒關閉?為什麼會這樣呢?
BOOL APIENTRY DllMain( HMODULE hModule, // handle to DLL module
DWORD ul_reason_for_call, // reason for calling function
LPVOID lpReserved // reserved
)
{
// Perform actions based on the reason for calling.
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH: // Initialize once for each new process.Return FALSE to fail DLL load.
{
SetDllNCCallback(rtm_InterpStartEnd);
SendNCDriverUserDecodeEvent();
pctr_dataShm = (LPCTRDATA)malloc(sizeof(Shmctr_data));//初始化全局控制變量
if(pctr_dataShm == NULL)
{
MessageBox(NULL,TEXT("can not alloc heapmemory in pctr_dataShm"),TEXT("Interpolation Error"),NULL);
return 1;
}
hrtm_PositCtrl_Trd = CreateThread(NULL, 0, rtm_PositCtrl_Trd, NULL,CREATE_SUSPENDED, NULL);
SetThreadPriority(hrtm_PositCtrl_Trd, THREAD_PRIORITY_TIME_CRITICAL);
ResumeThread(hrtm_PositCtrl_Trd);
hrtm_VelCtrl_Trd = CreateThread(NULL, 0, rtm_VelCtrl_Trd, NULL,CREATE_SUSPENDED, NULL);
SetThreadPriority(hrtm_VelCtrl_Trd, THREAD_PRIORITY_HIGHEST);
ResumeThread(hrtm_VelCtrl_Trd);
hrtm_DecodFifo_Trd = CreateThread(NULL, 0, rtm_DecodFifo_Trd, NULL,CREATE_SUSPENDED, NULL);
SetThreadPriority(hrtm_DecodFifo_Trd, THREAD_PRIORITY_ABOVE_NORMAL);
ResumeThread(hrtm_DecodFifo_Trd);
hrtm_Intrp_Trd = CreateThread(NULL, 0, rtm_Intrp_Trd, NULL,CREATE_SUSPENDED, NULL);
SetThreadPriority(hrtm_Intrp_Trd, THREAD_PRIORITY_NORMAL);
ResumeThread(hrtm_Intrp_Trd);
hrtm_Prd_2ms = CreateThread(NULL,0,rtm_Prd_2ms,0,0,NULL);
break;
}
case DLL_THREAD_ATTACH:
//MessageBox(NULL,TEXT("Enter DLL_THREAD_ATTACH"),TEXT("Interpolation Inform"),NULL);
break;
case DLL_THREAD_DETACH:
//MessageBox(NULL,TEXT("Enter DLL_THREAD_DETACH"),TEXT("Interpolation Inform"),NULL);
break;
case DLL_PROCESS_DETACH:
CloseHandle(hrtm_PositCtrl_Trd);
free(pctr_dataShm); //釋放全局控制量
pctr_dataShm = NULL;
CloseHandle(hrtm_PositCtrl_Trd);
CloseHandle(hrtm_VelCtrl_Trd);
CloseHandle(hrtm_DecodFifo_Trd);
CloseHandle(hrtm_Intrp_Trd);
CloseHandle(hrtm_Prd_2ms);
MessageBox(NULL,TEXT("Leave Interpolation dll"),TEXT("Interpolation Inform"),NULL);
break;
}
return TRUE;
}
用
Process.GetCurrentProcess().Kill();
關閉下看看