Click here to Skip to main content
15,921,113 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
Generalavtive X Pin
_tasleem22-Jun-05 18:03
_tasleem22-Jun-05 18:03 
GeneralRe: avtive X Pin
Robsori22-Jun-05 21:37
Robsori22-Jun-05 21:37 
GeneralBitBlt is not working in Service Pin
vegeakhil22-Jun-05 17:58
vegeakhil22-Jun-05 17:58 
GeneralRe: BitBlt is not working in Service Pin
John R. Shaw22-Jun-05 18:07
John R. Shaw22-Jun-05 18:07 
GeneralRe: BitBlt is not working in Service Pin
ThatsAlok22-Jun-05 18:18
ThatsAlok22-Jun-05 18:18 
GeneralRe: BitBlt is not working in Service Pin
vegeakhil23-Jun-05 5:00
vegeakhil23-Jun-05 5:00 
GeneralRe: BitBlt is not working in Service Pin
vegeakhil23-Jun-05 5:02
vegeakhil23-Jun-05 5:02 
GeneralRe: BitBlt is not working in Service Pin
vegeakhil23-Jun-05 18:13
vegeakhil23-Jun-05 18:13 
Hi Guys

Pls help me...the code is , just to remind you again, if I call this function from simple application it is fine, but if I call from windows service, BitBlt is failing with "Invalid Handle".

Thanks

BOOL CFrameBuffer::CaptureScreen(CRect rectScreen, BYTE *scrBuff)
{
HBITMAP hDesktopCompatibleBitmap=NULL;
HDC hDesktopCompatibleDC=NULL;
HDC hDesktopDC=NULL;
HWND hDesktopWnd=NULL;

int nHeight=rectScreen.bottom-rectScreen.top;/GetSystemMetrics(SM_CXSCREEN);
int nWidth=rectScreen.right-rectScreen.left;//GetSystemMetrics(SM_CYSCREEN);

hDesktopWnd=GetDesktopWindow();

hDesktopDC=::GetDC(NULL);

hDesktopCompatibleDC=CreateCompatibleDC(hDesktopDC);

hDesktopCompatibleBitmap=CreateCompatibleBitmap(hDesktopDC,nWidth,nHeight);

BOOL bRet = FALSE;
HBITMAP oldbitmap;
oldbitmap = (HBITMAP)SelectObject(hDesktopCompatibleDC,hDesktopCompatibleBitmap);

if(hDesktopCompatibleDC && hDesktopDC)
{
bRet = BitBlt(hDesktopCompatibleDC,0,0,nWidth,nHeight,hDesktopDC,0,0,SRCCOPY);
}

OPENFILENAME ofn;
char szFileName[512]; szFileName[0]='\0';
ZeroMemory(&ofn,sizeof(ofn));
ofn.lStructSize=sizeof(OPENFILENAME);
ofn.Flags=OFN_HIDEREADONLY|OFN_PATHMUSTEXIST;
ofn.lpstrFilter="Bitmap Files (*.bmp)\0*.bmp\0";
ofn.lpstrDefExt="bmp";
strcpy(szFileName,"C:\\temp\\MyBitmap.bmp");
ofn.lpstrFile=szFileName;
ofn.nMaxFile=512;

CFileFind finder;
if(finder.FindFile(szFileName))
{
DeleteFile(szFileName);
}

SetCursor(LoadCursor(NULL,IDC_WAIT));

BITMAPINFO bi;
void *pBits=NULL;

ZeroMemory(&bi,sizeof(bi));
bi.bmiHeader.biSize=sizeof(bi.bmiHeader);
bi.bmiHeader.biHeight=nHeight;
bi.bmiHeader.biWidth=nWidth;
bi.bmiHeader.biPlanes=1;
bi.bmiHeader.biBitCount=32;//16 bit
bi.bmiHeader.biCompression=BI_RGB;
bi.bmiHeader.biSizeImage=nWidth*nHeight *sizeof(DWORD);
bi.bmiHeader.biHeight = abs(nHeight);
HDC hBmpFileDC=CreateCompatibleDC(hDesktopCompatibleDC);

HBITMAP hBmpFileBitmap=CreateDIBSection(hDesktopCompatibleDC,&bi,DIB_RGB_COLORS,&pBits,NULL,0);

SelectObject(hBmpFileDC,hBmpFileBitmap);
BitBlt(hBmpFileDC,0,0,nWidth,nHeight,hDesktopCompatibleDC,0,0,SRCCOPY);

HANDLE hFile=CreateFile(szFileName,GENERIC_WRITE,FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if(hFile!=INVALID_HANDLE_VALUE)
{
DWORD dwRet=0;
BITMAPFILEHEADER bmfHeader;
ZeroMemory(&bmfHeader,sizeof(bmfHeader));
bmfHeader.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);
bmfHeader.bfSize=bi.bmiHeader.biSizeImage+bmfHeader.bfOffBits;
bmfHeader.bfType='MB';

WriteFile(hFile,&bmfHeader,sizeof(bmfHeader),&dwRet,NULL);
WriteFile(hFile,&bi.bmiHeader,sizeof(bi.bmiHeader),&dwRet,NULL);
WriteFile(hFile,pBits,bi.bmiHeader.biSizeImage,&dwRet,NULL);
CloseHandle(hFile);
AddToMessageLog(" Inside 2 CaptureScreen() is called", 1 );
}

if(scrBuff!=NULL)
{
memcpy(scrBuff,pBits,bi.bmiHeader.biSizeImage);
}

SelectObject(hDesktopCompatibleDC,oldbitmap);
DeleteDC(hBmpFileDC);
DeleteObject(hBmpFileBitmap);
ReleaseDC(hDesktopWnd,hDesktopDC);
return TRUE;
}

Generalprintf(...) clone Pin
Weiye Chen22-Jun-05 17:49
Weiye Chen22-Jun-05 17:49 
GeneralRe: printf(...) clone Pin
Christian Graus22-Jun-05 18:16
protectorChristian Graus22-Jun-05 18:16 
GeneralRe: printf(...) clone Pin
Weiye Chen22-Jun-05 19:57
Weiye Chen22-Jun-05 19:57 
GeneralRe: printf(...) clone Pin
John R. Shaw22-Jun-05 18:25
John R. Shaw22-Jun-05 18:25 
GeneralRe: printf(...) clone Pin
Weiye Chen22-Jun-05 19:42
Weiye Chen22-Jun-05 19:42 
GeneralRe: printf(...) clone Pin
ThatsAlok22-Jun-05 18:30
ThatsAlok22-Jun-05 18:30 
GeneralRe: printf(...) clone Pin
Weiye Chen22-Jun-05 19:59
Weiye Chen22-Jun-05 19:59 
Generalreading a comma delimited file Pin
formula_200222-Jun-05 16:29
formula_200222-Jun-05 16:29 
GeneralRe: reading a comma delimited file Pin
John R. Shaw22-Jun-05 17:21
John R. Shaw22-Jun-05 17:21 
Questionhow can i change "no_namespace" ? Pin
liuyue22-Jun-05 15:28
liuyue22-Jun-05 15:28 
AnswerRe: how can i change "no_namespace" ? Pin
Christian Graus22-Jun-05 18:19
protectorChristian Graus22-Jun-05 18:19 
Questionhow to sort a linked list? Pin
suroor45322-Jun-05 15:01
suroor45322-Jun-05 15:01 
AnswerRe: how to sort a linked list? Pin
Chris Losinger22-Jun-05 15:57
professionalChris Losinger22-Jun-05 15:57 
GeneralRe: how to sort a linked list? Pin
suroor45322-Jun-05 16:39
suroor45322-Jun-05 16:39 
GeneralRe: how to sort a linked list? Pin
Jack Puppy22-Jun-05 17:29
Jack Puppy22-Jun-05 17:29 
GeneralRe: how to sort a linked list? Pin
suroor45322-Jun-05 18:08
suroor45322-Jun-05 18:08 
GeneralRe: how to sort a linked list? Pin
Trollslayer23-Jun-05 1:36
mentorTrollslayer23-Jun-05 1:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.