Click here to Skip to main content
13,004,094 members (109,817 online)
Rate this:
Please Sign up or sign in to vote.
See more:
How to add MFC coding as a windows service??? please help me

<code>void CscreensaveDlg::OnBnClickedOk()
	// TODO: Add your control notification handler code here   
	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 18-Apr-11 21:36pm
Updated 18-Apr-11 22:14pm
Niklas Lindquist 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
Rate this: bad
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().
Rate this: bad
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!!! :)

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170627.1 | Last Updated 19 Apr 2011
Copyright © CodeProject, 1999-2017
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