Click here to Skip to main content
Rate this: bad
good
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);
	HDC hMemDC = NULL;
 
	BYTE *lpBitmapBits = NULL; 
 
	int nWidth = GetSystemMetrics(SM_CXSCREEN);
	int nHeight = GetSystemMetrics(SM_CYSCREEN); 
 
	hMemDC = ::CreateCompatibleDC(hScrDC); 
 
	BITMAPINFO bi; 
	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);
 
	BITMAPFILEHEADER bh;
	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++)
	{
		 sprintf_s(buffer,"D:\image%u.jpg",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);
			file.Close();
			Sleep(10000);
		}
		
	}
	::SelectObject(hMemDC, oldbmp);
	::DeleteObject(bitmap);
	::DeleteObject(hMemDC);
	::ReleaseDC(NULL, hScrDC); 
 
Posted 27-Apr-11 19:43pm
Comments
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
good
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:
What are TCHAR, WCHAR, LPSTR, LPWSTR, LPCTSTR etc?[^].
 
Instead of CFile, HFILE handle should be used with functions like OpenFile, CreateFile, etc., http://msdn.microsoft.com/en-us/library/aa364232(v=vs.85).aspx[^].
 
—SA
  Permalink  
v3
Rate this: bad
good
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
 
http://www.devx.com/cplus/Article/9857[^]
 

A Windows Service Application[^]
 

CNTService v1.06 - NT Service Framework[^]
  Permalink  
v2
Comments
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
good
Please Sign up or sign in to vote.

Solution 3

  Permalink  
Comments
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 OriginalGriff 520
1 Maciej Los 290
2 Richard MacCutchan 265
3 BillWoodruff 265
4 Suraj Sahoo | Coding Passion 155
0 OriginalGriff 8,764
1 Sergey Alexandrovich Kryukov 7,437
2 DamithSL 5,639
3 Maciej Los 5,279
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web04 | 2.8.1411028.1 | Last Updated 28 Apr 2011
Copyright © CodeProject, 1999-2014
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