Jump to content
Sign in to follow this  
linuxuser

Windows Programming

Recommended Posts

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;

}

 

 

 

Share this post


Link to post
Share on other sites

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.");

}

Share this post


Link to post
Share on other sites

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 smile.gif i will post some more soon.....smile.gif

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.