Click here to Skip to main content
15,867,835 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to add MFC coding as a windows service??? please help me


void CscreensaveDlg::OnBnClickedOk()
{
	// TODO: Add your control notification handler code here   
	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
Updated 18-Apr-11 22:14pm
v2
Comments
Niklas L 19-Apr-11 4:08am    
Do you want to use MFC to build a windows service? MFC is mainly UI classes, which doesn't go with services very well. Could you give some more input?
Gokulnath007 19-Apr-11 4:13am    
I need to develop a service which will take the screenshots with a specified time interval.. I have done a simple project in MFC which will take 10 screen shots in 10 interval. How to make it as a win service??? please help me
Gokulnath007 20-Apr-11 5:59am    
I have found the codes for install and uninstall the services . Now both installation and uninstallation working fine. Next step is bringing the above codes inside. The above code should be executed once the service gets started. Please help me

As far as I can see, the only MFC code you have in there is the CFile object. Replace it with CreateFile()[^] or fopen_s()[^], and it will not depend on MFC anymore.

Edit: And the use of CString. But if you create an ATL service, CString will be usable as is.

PS. You might want to have a look at CString::Format().
 
Share this answer
 
v2
Well i don't know much about this topic

You can have some help @ Windows Services[^]

Hope this might help you!!! :)
 
Share this answer
 

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