Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Comments
Sandeep Mewara 28-Apr-11 0:46am    
Did you try? You got stuck? Where?
Gokulnath007 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 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??

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, <code>CreateFile, etc., http://msdn.microsoft.com/en-us/library/aa364232(v=vs.85).aspx[^].

—SA
 
Share this answer
 
v3
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[^]
 
Share this answer
 
v2
Comments
Gokulnath007 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.
LaxmikantYadav 28-Apr-11 4:41am    
Please refer the second link it will help you.
 
Share this answer
 
Comments
Gokulnath007 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 L 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 L 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900