exe调用dll,当exe关闭后 宿主出错退出了。怎么回事?VC很正常
我本来用VC6.0写的DLL。当exe退出的时候``宿主程序还是正常的运行.转到vs2003后`当exe退出时`宿主就出现内存出错`查百度`说是托管和非托管的问题`我看配置都是属于非托管的;
-------------------------------------------DLL代码--------------------------------------------------------
CMain.cpp就一个dialog资源`
#include "stdafx.h"
#include "ApiHook.h"
#include "CMain.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
BEGIN_MESSAGE_MAP(CApiHookApp, CWinApp)
END_MESSAGE_MAP()
CApiHookApp::CApiHookApp()
{
}
// 唯一的一个 CApiHookApp 对象
CApiHookApp theApp;
// CApiHookApp 初始化
BOOL CApiHookApp::InitInstance()
{
CWinApp::InitInstance();
return TRUE;
}
extern "C" __declspec(dllexport) bool SetHookGame(DWORD TreadID,DWORD SHUAA,DWORD iDate); //安装HOOK
extern "C" __declspec(dllexport) bool UnstallHook(); //卸载DLL
static HHOOK hHook = NULL; // 钩子句柄
static HINSTANCE hInst; // 当前DLL句柄
static HWND hwnd;
LRESULT CALLBACK MyProc(int nCode, WPARAM wParam, LPARAM lParam);
// 安装钩子函数
bool __declspec(dllexport) SetHookGame(DWORD TreadID,DWORD SHUAA,DWORD iDate)
{
if(TreadID!=0 && SHUAA == 0x90) //0x5566
{
// 安装指定线程的钩子
hHook = SetWindowsHookEx(WH_KEYBOARD, (HOOKPROC)MyProc,theApp.m_hInstance,TreadID);
if (hHook != NULL)
{
return true;
}
else
{
UnhookWindowsHookEx(hHook);
return false;
}
}
else
{
UnhookWindowsHookEx(hHook);
return false;
}
}
//---------------------------------------------------------------------------
// 钩子函数
LRESULT CALLBACK MyProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode < 0)
return (CallNextHookEx(hHook, nCode, wParam, lParam));
if( ((DWORD)lParam&0x40000000) && (HC_ACTION==nCode) ) //有键按下
{
if( wParam == VK_F12 )
{
if (!DLLMain)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
DLLMain = new CCMain();
CWnd * pCWnd = CWnd::GetForegroundWindow();
DLLMain->Create(IDD_DLG,pCWnd);
DLLMain->ShowWindow(SW_SHOW);
}
else
{
DLLMain->ShowWindow(DLLMain->IsWindowVisible() ? SW_HIDE : SW_SHOW);
}
}
}
return (CallNextHookEx(hHook, nCode, wParam, lParam));
}
//---------------------------------------------------------------------------
bool __declspec(dllexport) UnstallHook()//卸载DLL
{
return ::UnhookWindowsHookEx(hHook);
}
//---------------------------------------------------------------------------
--------------------编程问答-------------------- 学习 --------------------编程问答-------------------- UP --------------------编程问答-------------------- 有可能是编译器问题,在05里遇到过,郁闷了很长时间,
可程序拿到其他人机器上,同样的工程、代码就Pass!!
很无奈啊@@ --------------------编程问答-------------------- 半个月了,解决了吗? --------------------编程问答-------------------- 自己解决了.. --------------------编程问答-------------------- 不知,帮顶
补充:.NET技术 , VC.NET