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

Solution 1

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
  Permalink  
v4
Comments
Sandeep Mewara at 18-Apr-11 11:39am
   
My 5+!
Albert Holguin at 18-Apr-11 20:13pm
   
Not a fan of the first link, but my 5 anyway (second link is good though).
Gokulnath007 at 19-Apr-11 0:23am
   
I want the code to run as a service. please help me
Gokulnath007 at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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

Solution 3

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

Solution 4

To run as service, look at this article: Start Your Windows Programs From An NT Service[^]
  Permalink  
Comments
Gokulnath007 at 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)

  Print Answers RSS
0 OriginalGriff 480
1 Maciej Los 330
2 Richard MacCutchan 225
3 BillWoodruff 185
4 Suraj Sahoo | Coding Passion 155
0 OriginalGriff 8,759
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,639
3 Maciej Los 5,269
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web02 | 2.8.1411028.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