Click here to Skip to main content
11,721,289 members (87,097 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: VC8.0
It should not depend on MFC. And it should run as a service. How to do this

HDC hScrDC = ::GetDC(NULL);
	BYTE *lpBitmapBits = NULL; 
	int nWidth = GetSystemMetrics(SM_CXSCREEN);
	int nHeight = GetSystemMetrics(SM_CYSCREEN); 
	hMemDC = ::CreateCompatibleDC(hScrDC); 
	ZeroMemory(&bi, sizeof(BITMAPINFO));
	bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
	bi.bmiHeader.biWidth = nWidth;
	bi.bmiHeader.biHeight = nHeight;
	bi.bmiHeader.biPlanes = 1;
	bi.bmiHeader.biBitCount = 24;
	HBITMAP bitmap = ::CreateDIBSection(hMemDC, &bi, DIB_RGB_COLORS, (LPVOID*)&lpBitmapBits, NULL, 0);
	HGDIOBJ oldbmp = ::SelectObject(hMemDC, bitmap); 
	::BitBlt(hMemDC, 0, 0, nWidth, nHeight, hScrDC, 0, 0, SRCCOPY);
	ZeroMemory(&bh, sizeof(BITMAPFILEHEADER));
	bh.bfType = 0x4d42; //bitmap 
	bh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
	bh.bfSize = bh.bfOffBits + ((nWidth*nHeight)*3);
	CFile file;	
	char buffer[1000];
	DWORD threadId;
  int value = 10;
  //hThread = CreateThread( NULL, 0, runThread, &value, 0, &threadId);
	for(int c=0;c<10;c++)
		 CString sName(buffer);  
         LPCTSTR lpszName = sName;  
		if(file.Open(lpszName, CFile::modeCreate | CFile::modeWrite))
			file.Write(&bh, sizeof(BITMAPFILEHEADER));
			file.Write(&(bi.bmiHeader), sizeof(BITMAPINFOHEADER));
			file.Write(lpBitmapBits, 3 * nWidth * nHeight);
	::SelectObject(hMemDC, oldbmp);
	::ReleaseDC(NULL, hScrDC); 
Posted 27-Apr-11 18:43pm
Sandeep Mewara at 28-Apr-11 0:46am
Did you try? You got stuck? Where?
Gokulnath007 at 28-Apr-11 2:26am
I have added the above codes in the OnStart() function of the windows service. It is not showing any errors. But while installing the service, it fails. Getting exceptions in it. I want the above code to run as a service in the system taking the screenshots.
Gokulnath007 at 29-Apr-11 9:41am
How to convert the image from the above code into blob?? in order to save it in the database as blob??
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

It is almost all written in Win32 API. I only see MFC CString and CFile.

Windows API works with LPCSTR and LPCTR instead of CString, but this is for non-Unicode version. For Unicode version, use LPCWSTR and LPWSTR. The declarations LPCTSTR and LPTSTR are used as generic ones, depending on compilation — as Unicode or not.

See this CodeProject article for more information:

Instead of CFile, HFILE handle should be used with functions like OpenFile, CreateFile, etc.,[^].

Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Non Mfc means in which format do you want ? First decide in which language you want to implelemt service. here are some good links which will help you lot[^]

A Windows Service Application[^]

CNTService v1.06 - NT Service Framework[^]
Gokulnath007 at 28-Apr-11 1:56am
In VC++ windows service, i want to implement the above coding to run as a service and taking the screenshots.
Laxmikant_Yadav at 28-Apr-11 4:41am
Please refer the second link it will help you.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Gokulnath007 at 28-Apr-11 3:09am
I have added those codes into the service application.But its not working.. I just want to take the screenshots as a service??
Niklas Lindquist at 28-Apr-11 4:07am
How do you get hold of your hScrDC? If I were you I would try running the service as the interactive user. It might be a security issue.
Niklas Lindquist at 28-Apr-11 4:08am
If you can compile the service, and then start it successfully, it's not an MFC related problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 515
1 OriginalGriff 279
2 Mika Wendelius 170
3 F-ES Sitecore 150
4 cyycoish 70
0 Sergey Alexandrovich Kryukov 2,209
1 OriginalGriff 969
2 F-ES Sitecore 660
3 Maciej Los 609
4 Richard MacCutchan 560

Advertise | Privacy | Mobile
Web01 | 2.8.150901.1 | Last Updated 28 Apr 2011
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100