Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 14,952 times

Contents

Related Categories

Running State Machines Based Win32/WinCE Programs - An Example

Jerome

An Example

Suppose we have a simple player application whose state diagram is as follows :

 

Figure: The State Chart of Player Application

The following sample shows the way to hook dialog messages and dispatch external events to Player state machine application. Declare an application thread context. On dialog open, initializes the state machine engine in the thread context through SmeInitEngine(). In Windows edition of state machine engine, this function will automatically initialize the given thread context following informations implicitly:

1) SmeSetExtEventOprProc() to setup external event handling functions through Windows API GetMessage(), PostThreadMessage().
2) SmeSetMemOprProc() to setup dynamic memory management functions through new, delete operators.
3) SmeSetTlsProc() to setup thread local storage procedure functions through Windows API TlsGetValue(), TlsSetValue().

And then hooks the dialog messages. Activate the Player application in the application thread. If an external event triggers, calls MfcPostExtIntEventToWnd() function to post an external event to dialog. This function will post WM_EXT_EVNET_ID Windows message as below to the dialog.

#define WM_EXT_EVENT_ID      (0xBFFF) 


When the state machine engine receives this message, translates this message to an external event, and dispatch it to the destination application port if it is not null; otherwise dispatch it to all active applications which runs in the same application thread context.

// The application thread context.
SME_THREAD_CONTEXT_T g_AppThreadContext;
// Declare the Player state machine application variable.
SME_DEC_EXT_APP_VAR(Player);

BOOL CSamplePlayerMfcDlg::OnInitDialog()
{
   CDialog::OnInitDialog();
   
   ....
   // Initialize engine.
   g_AppThreadContext.nAppThreadID = 0;
   SmeInitEngine(&g_AppThreadContext);
   // Hook dialog message.
   MfcHookWnd(GetSafeHwnd());
   SmeActivateApp(&SME_GET_APP_VAR(Player),NULL);
}

void CSamplePlayerMfcDlg::OnButtonPower()
{
   MfcSendExtIntEventToWnd(EXT_EVENT_ID_POWER, 0, 0, NULL, GetSafeHwnd());
}

void CSamplePlayerMfcDlg::OnButtonPause()
{
   // TODO: Add your control notification handler code here
   MfcSendExtIntEventToWnd(EXT_EVENT_ID_PAUSE_RESUME, 0, 0, NULL, GetSafeHwnd());
}



When the hooked window is destroyed, the window message hook is removed automatically.

Interested by the subject ?

You may download more information at http://www.intelliwizard.com/ the official site the UML StateWizard open source project.[4]

Notes and References

[1] Computer Networks, Andrew S.Tanenbaum.
[2] This entry is from Wikipedia, the leading user-contributed encyclopedia.
[3] Microsoft Systems Journal March 1997
[4] The UML StateWizard  project is hosted by Sourceforge.net

Comments

  • Updated the software license to LGPL

    Posted by Jerome on 06 Jun 2006

    We updated StateWizard license to LGPL. (2006/6/6)

    Lesser General Public License(LGPL):

    This license, the GNU Lesser General Public License, applies to certain designated libraries, and ...