服务中调用BitBlt失败,为何???
我写一个程序抓当前屏幕,放在dll中,用普通方法调用时没有问题,但是将调用dll的程序写成win32服务的形式时却失败,抓图代码如下,跟踪发现BitBlt时失败,GetLastError得到的是"句柄无效"请问如何解决?
HBITMAP CopyScreenToBitmap(LPRECT lpRect)
//lpRect 代表选定区域
{
HDC hScrDC = NULL;
HDC hMemDC = NULL;
// 屏幕和内存设备描述表
HGDIOBJ hBitmap = NULL;
HGDIOBJ hOldBitmap = NULL;
// 位图句柄
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;
// 获得屏幕分辨率
xScrn = GetDeviceCaps(hScrDC, HORZRES);
yScrn = GetDeviceCaps(hScrDC, VERTRES);
//确保选定区域是可见的
if (nX < 0)
nX = 0;
if (nY < 0)
nY = 0;
if (nX2 > xScrn)
nX2 = xScrn;
if (nY2 > yScrn)
nY2 = yScrn;
nWidth = nX2 - nX;
nHeight = nY2 - nY;
// 创建一个与屏幕设备描述表兼容的位图
hBitmap = CreateCompatibleBitmap
(hScrDC, nWidth, nHeight);
// 把新位图选到内存设备描述表中
hOldBitmap = SelectObject(hMemDC, hBitmap);
DebugIntStr((int)hOldBitmap, "hOldBitmap....");
// 把屏幕设备描述表拷贝到内存设备描述表中
DebugIntStr((int)hMemDC, "hMemDC....");
DebugIntStr((int)hScrDC, "hScrDC....");
BitBlt(hMemDC, 0, 0, nWidth, nHeight,
hScrDC, nX, nY, SRCCOPY); //这里失败
//得到屏幕位图的句柄
hBitmap = SelectObject(hMemDC, hOldBitmap);
//清除
DeleteDC(hScrDC);
DeleteDC(hMemDC);
// 返回位图句柄
return (HBITMAP)hBitmap;
}