Unhandled exception jztsHook.exe:0x000005:Access Violation,该怎么解决
Unhandled exception jztsHook.exe:0x000005:Access Violation
我写的是一个对话框程序,主要复制桌面上的某个窗口成图片,程序有两个按钮。
思路:
用HOOK返回鼠标在点击是的坐标点返回坐标点后用windowsfrompoint返回坐标对应的窗口句柄得到句柄后用getwindowrect获得窗口的坐标然后后就复制图片但是在图片分配内存的时候出错了
HBITMAP CJZTSHookDlg::CopyScreenToBitmap(LPRECT lpRect)
bool CJZTSHookDlg::SaveBitmapToFile(HBITMAP hBitmap, CString FileName)
这两个函数在单独调用的时候是正常的可以得到一副位图后来加了HOOK后就出现这个问题。请前辈纠错教我改下!
备注:错误调试的时候提示错误的地方:*lpbi = bi;但我不知道要怎么改。
主要代码:
_declspec(dllimport) void fhsbzb();
_declspec(dllimport) POINT zmzb();
void CJZTSHookDlg::OnButton1()
{
// TODO: Add your control notification handler code here
HWND hwnd=NULL;
LPRECT lpRect=NULL;
/*
fhsbzb();
point=zmzb();
*/
hwnd=::WindowFromPoint(point);
::GetWindowRect(hwnd,lpRect);
/////////////////////////////////////////////////////////////
//隐藏程序界面
ShowWindow(SW_HIDE);
//延时1s,使界面隐藏
Sleep(500);
HBITMAP hBitmap;
hBitmap = CopyScreenToBitmap(lpRect);
SaveBitmapToFile(hBitmap, "screen.bmp");
//显示程序界面
ShowWindow(SW_SHOW);
}
////////////////////////获得屏幕的图像///////////////////////////////////
HBITMAP CJZTSHookDlg::CopyScreenToBitmap(LPRECT lpRect)
{
HDC hScrDC,hMemDC; //屏幕和内存设备描述表
HBITMAP hBitmap,hOldBitmap; //位图句柄
int nX,nY,nX2,nY2; //选定区域坐标
int nWidth, nHeight; //位图宽度和高度
int xScrn, yScrn; //屏幕分辨率
//确保选定区域不为空矩形
if(IsRectEmpty(lpRect)) return NULL;
//为屏幕创建设备描述表
hScrDC=CreateDC("DISPLAY", NULL, NULL, NULL);
//为屏幕设备描述表创建兼容的内存设备描述表
hMemDC=CreateCompatibleDC(hScrDC);
//获得选定区域坐标
nX = lpRect->left;
nY = lpRect->top;
nX2 = lpRect->right;
nY2 = lpRect->bottom;
/*
nX = 3;
nY = 97;
nX2 = 76;
nY2 = 131;
*/
//获得屏幕分辨率
xScrn = GetDeviceCaps(hScrDC, HORZRES);
yScrn = GetDeviceCaps(hScrDC, VERTRES);
//确保选定区域是可见的
if(nX < 0)
{
我写的是一个对话框程序,主要复制桌面上的某个窗口成图片,程序有两个按钮。
思路:
用HOOK返回鼠标在点击是的坐标点返回坐标点后用windowsfrompoint返回坐标对应的窗口句柄得到句柄后用getwindowrect获得窗口的坐标然后后就复制图片但是在图片分配内存的时候出错了
HBITMAP CJZTSHookDlg::CopyScreenToBitmap(LPRECT lpRect)
bool CJZTSHookDlg::SaveBitmapToFile(HBITMAP hBitmap, CString FileName)
这两个函数在单独调用的时候是正常的可以得到一副位图后来加了HOOK后就出现这个问题。请前辈纠错教我改下!
备注:错误调试的时候提示错误的地方:*lpbi = bi;但我不知道要怎么改。
主要代码:
_declspec(dllimport) void fhsbzb();
_declspec(dllimport) POINT zmzb();
void CJZTSHookDlg::OnButton1()
{
// TODO: Add your control notification handler code here
HWND hwnd=NULL;
LPRECT lpRect=NULL;
/*
fhsbzb();
point=zmzb();
*/
hwnd=::WindowFromPoint(point);
::GetWindowRect(hwnd,lpRect);
/////////////////////////////////////////////////////////////
//隐藏程序界面
ShowWindow(SW_HIDE);
//延时1s,使界面隐藏
Sleep(500);
HBITMAP hBitmap;
hBitmap = CopyScreenToBitmap(lpRect);
SaveBitmapToFile(hBitmap, "screen.bmp");
//显示程序界面
ShowWindow(SW_SHOW);
}
////////////////////////获得屏幕的图像///////////////////////////////////
HBITMAP CJZTSHookDlg::CopyScreenToBitmap(LPRECT lpRect)
{
HDC hScrDC,hMemDC; //屏幕和内存设备描述表
HBITMAP hBitmap,hOldBitmap; //位图句柄
int nX,nY,nX2,nY2; //选定区域坐标
int nWidth, nHeight; //位图宽度和高度
int xScrn, yScrn; //屏幕分辨率
//确保选定区域不为空矩形
if(IsRectEmpty(lpRect)) return NULL;
//为屏幕创建设备描述表
hScrDC=CreateDC("DISPLAY", NULL, NULL, NULL);
//为屏幕设备描述表创建兼容的内存设备描述表
hMemDC=CreateCompatibleDC(hScrDC);
//获得选定区域坐标
nX = lpRect->left;
nY = lpRect->top;
nX2 = lpRect->right;
nY2 = lpRect->bottom;
/*
nX = 3;
nY = 97;
nX2 = 76;
nY2 = 131;
*/
//获得屏幕分辨率
xScrn = GetDeviceCaps(hScrDC, HORZRES);
yScrn = GetDeviceCaps(hScrDC, VERTRES);
//确保选定区域是可见的
if(nX < 0)
{