Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a MFC application with button click event which captures the screen shots of the window and save it in a file. I want to make a thread for looping in which the capturing of screen continues to go on. Please help me... Am using the following code which takes 10 screen shots in the interval of 10 seconds. How to make it as a service to run continuously after installing.. help me.. with this code...


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 18:25pm
v2
Comments
Gokulnath007 20-Apr-11 6:00am    
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

Look at this article here on CP: Threads with MFC[^]. It'll show you how to do threading with MFC. Some more can be found here: http://www.dotnetheaven.com/Uploadfile/mahesh/MultithreadingUsingMFC05212005025727AM/MultithreadingUsingMFC.aspx[^].

Once the thread is started in the background from pressing the button initially, the logic in your button (should be a toggle button) should assure that the thread is stopped again when the button is toggled back into it's original setting.

Best Regards,

-MRB
 
Share this answer
 
v4
Comments
Sandeep Mewara 18-Apr-11 11:39am    
My 5+!
Albert Holguin 18-Apr-11 20:13pm    
Not a fan of the first link, but my 5 anyway (second link is good though).
Gokulnath007 19-Apr-11 0:23am    
I want the code to run as a service. please help me
Gokulnath007 20-Apr-11 6:00am    
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
You could also solve the problem using a timer, which fires every n milliseconds, and calls a method you supply. Have a look at the sample code for CWnd::SetTimer()[^]
 
Share this answer
 
I don't really like the first link within CodeProject that manfred suggested, actually I think that article is kind of bad, but the second one is much better.

Anyway, in summary:
1. Create a thread, either worker thread or UI thread. Naming convention is strictly naming, UI threads don't have to be for just user interfaces, they're actually very useful since they have a message pump allowing you to message to the thread directly (using PostThreadMessage()) if there's no requirement for synchronicity.
2. Task your thread, in a worker thread, the thread will generally only have one task and will end when its finished, your main program can either wait for it or just continue. A UI thread can have multiple tasks.
3. Kill your thread, nicely mind you. Failing to bring a thread down in a clean manner will result in leaked memory.
 
Share this answer
 
To run as service, look at this article: Start Your Windows Programs From An NT Service[^]
 
Share this answer
 
Comments
Gokulnath007 20-Apr-11 6:00am    
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

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