Now we have a way to display a menu on the serial debug port and are getting user input to select from the menu.  So it is time to actually do something, like put some control behind the menu.

I like to start with a main function, in this case WinMain().  I am going to keep it simple, not parsing command line options and just calling MainMenu() which will handle the menu from here.

int WINAPI WinMain( HINSTANCE hInstance,
     HINSTANCE hPrevInstance,
     LPTSTR    lpCmdLine,
     int       nCmdShow)
{
 MainMenu();
}

MainMenu is where I will do the work.  It starts by defining an array of pointers to strings that will be displayed to create the menu.

 TCHAR *MenuStrings[] = {
   TEXT( "Get Tick Count"),
   TEXT( "Show Running Processes"),
   NULL
  };

If you remember from last time, the NULL indicates the end of the menu and the menu will include three items.  It will look like this:

        a : Get Tick Count
        b : Show Running Processes
        c : Quit

Where the Quit item is automatically included in every menu.

With the strings we can now call MenuHandler to display the menu and wait for user input:

 int MenuSelection;
 MenuSelection = MenuHandler( MenuStrings );

MenuSelection will contain the index of the item that the user selected.  A switch statement is all that is needed to handle the request:

 switch( MenuSelection  )
 {
  case 0:
   RETAILMSG(1,(TEXT("Tick Count %X\n"), GetTickCount() ));
   break;
  case 1:
   ShowRunningProcesses();
   break;
  default:
   Done = TRUE;
   break;
 }

The menu should run until the user selects Quit, so all that is left is to put the code in a loop.  Note that the default case sets Done to TRUE which we can use to exit the menu.  Her is the whole MainMenu() function:

void MainMenu()
{
 TCHAR *MenuStrings[] = {
   TEXT( "Get Tick Count"),
   TEXT( "Show Running Processes"),
   NULL
  };
 int Done = FALSE;
 int MenuSelection;
 while( ! Done )
 {
  MenuSelection = MenuHandler( MenuStrings );
  switch( MenuSelection  )
  {
   case 0:
    RETAILMSG(1,(TEXT("Tick Count %X\n"), GetTickCount() ));
    break;
   case 1:
    ShowRunningProcesses();
    break;
   default:
    Done = TRUE;
    break;
  }
 }
}

ShowRunningProcesses() is more complex and is an intersting example of using the ToolHelpAPI.  I am going to save it for later.

For now, we have a working model of a menu driven application using the debug serial port.  Of course the menu system can be extended to have sub-menus by simply calling on another function that controls another menu.

Copyright © 2008 – Bruce Eitman
All Rights Reserved