linuxuser 0 Report post Posted April 19, 2004 Hey there, Did u know that u can actually write Win32 application that automatically logs off or even shuts down the system ( at give time ). I am showing some snippet of code that actually does that, Also, the code demonstrates some topics of windows programming such as Synchronization, Threading, Call back functions. Here is the Visual C++ code, assuming that u have a Dialog application with two buttons, botton1 -- Start polling, when user clicks on this button, it reads the given time ( hr,min), populate those to member variables m_Hr, and m_Min, then it executes a thread which checks for the time, and logs off the computer. void CExitAppDlg::OnButton1() { // TODO: Add your control notification handler code here TimeParam tParam; tParam.hourParam = m_Hr; tParam.minParam = m_Min; tParam.eventParam = CreateEvent( NULL,TRUE,FALSE,NULL ); m_Event = tParam.eventParam; //Pass the object CWinThread* pThread = AfxBeginThread( ThreadExec, (LPVOID)this ); SetDlgItemText( IDC_STATUS, "Polling started......."); } UINT ThreadExec( LPVOID pParam ) { //TimeParam* p_Param = (TimeParam*)pParam; CExitAppDlg* pDlg = (CExitAppDlg*)pParam; int iHr,iMin; while( 1 ) { if( ::WaitForSingleObject( pDlg->m_Event,500 ) == WAIT_OBJECT_0 ) break; CTime time = CTime::GetCurrentTime(); iHr = time.GetHour(); iMin = time.GetMinute(); if(( pDlg->m_Hr > 0 ) && ( pDlg->m_Min > 0)) { if(( iHr >= pDlg->m_Hr ) && ( iMin >= pDlg->m_Min )) { ExitWindowsEx( EWX_LOGOFF|EWX_FORCE , NULL );//use EWX_SHUTDOWN for shutdown } } Sleep( 10000 ); }//end of while return 0; } Quote Share this post Link to post Share on other sites
linuxuser 0 Report post Posted April 19, 2004 button2 - this button simply stops polling by setting the event object void CExitAppDlg::OnButton2() { // TODO: Add your control notification handler code here SetEvent( m_Event ); SetDlgItemText( IDC_STATUS, "Polling stopped."); } Quote Share this post Link to post Share on other sites
prakriti 0 Report post Posted April 23, 2004 Thank you Rs_1915 dai! May be this will help me to complete my assignments. Go on posting more.................! Quote Share this post Link to post Share on other sites
linuxuser 0 Report post Posted April 26, 2004 QUOTE May be this will help me to complete my assignments. Go on posting more.................! I am glad i am being able to help u some i will post some more soon..... Quote Share this post Link to post Share on other sites