Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VC8.0
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 18-Apr-11 22:36pm
Edited 18-Apr-11 23:14pm
v2
Comments
Niklas Lindquist at 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 at 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 at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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().
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Well i don't know much about this topic
 
You can have some help @ Windows Services[^]
 
Hope this might help you!!! Smile | :)
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 8,149
1 Sergey Alexandrovich Kryukov 7,287
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,910


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 19 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