locked
Monitor processes, applications that run in windows mobile 6.0 RRS feed

  • Question

  • Hello all, 

    I'm fairly new to Windows Mobile development, and I have run into a problem that I can't seem to find a straight answer to.  

    My application needs to know what applications/processes are running on the device, as well as which application is in focus (viewed). 

    Is there a process iterator that can run in my application, then perhaps check against each one to see if it is in the foreground. 


    Thanks!!!
    Tuesday, January 13, 2009 3:38 PM

Answers

  • Hi,

    If you are only needing to support Windows Mobile 5.0 or higher you may like to investigate the State and Notification Broker (SNAPI). This has a property called ActiveApplication which allows you to efficiently detect whenever the active application changes on the device.

    Below is a small code snippet which demonstrates using this API from native C or C++ (the API is also available for use via the SystemState class in the .NET environment).

    #include <regext.h>  
    #include <snapi.h>  
     
    // This callback is called whenever the active application changes
    void ForegroundApplicationChanged(HREGNOTIFY hNotify,  
           DWORD dwUserData,  
           const PBYTE pData,  
           const UINT cbData)  

      // Remove the previous application title from the string
      LPCTSTR pszApplicationTitle = (LPCTSTR)pData;  
      while (*pszApplicationTitle && *pszApplicationTitle++ != 0x1B)  
        ;  
     
      // Output to the Visual Studio debugger output window
      OutputDebugString(pszApplicationTitle);  
    }  
     
    int WINAPI WinMain(HINSTANCE hInstance,  
                       HINSTANCE hPrevInstance,  
                       LPTSTR    lpCmdLine,  
                       int       nCmdShow)  
    {  
      // Request to be notified whenever the active application changes
      HREGNOTIFY hNotify;  
      HNOTIFICATIONCONDITION condition = { REG_CT_ANYCHANGE };
      RegistryNotifyCallback(SN_ACTIVEAPPLICATION_ROOT,  
                             SN_ACTIVEAPPLICATION_PATH,  
                             SN_ACTIVEAPPLICATION_VALUE,  
                             ForegroundApplicationChanged,  
                             0, &condition, &hNotify);  
     
      // Your main application logic / UI would go here
      while (true)  
        Sleep(10000);  
     
      // Clean up our resources
      RegistryCloseNotification(hNotify);  
      return 0;  

    If you run this sample within the Visual Studio debugger you should see the name of the current application being listed in the output window whenever it changes.

    The only slightly tricky bit is the string manipulation found in the ForegroundApplicationChanged function. SNAPI returns both the previously active and current application names seperated by an escape (0x1B) character, so this loop strips off the previous application title.

    When the user is viewing the desktop the title 'Desktop' will be returned.

    Hope this helps,
    Christopher Fairbairn
    Visit my blog at http://www.christec.co.nz/blog/
    Tuesday, January 13, 2009 11:39 PM

All replies

  • System.Processes.Process class GetProcesses method will get the currently running processes.  Not sure how to tell which one has focus.
    Joel Ivory Johnson
    Twitter:J2iNET
    Tuesday, January 13, 2009 4:00 PM
  • I was looking on  René Stein's blog and saw the answer to the other part of your question.
    Take a look at the native function GetForegroundWindow.
    Joel Ivory Johnson
    Twitter:J2iNET
    Tuesday, January 13, 2009 5:43 PM
  • Thanks, I actually stumbled on to that a little while ago.  That seems to be my ticket.


    Now the bigger issue comes in to play.  I not only need to know what the user is looking at, I need to react when the user is looking at nothing (the desktop, today screen, what have you)

    I built an iterator that enumerates over the processes/windows that are currently running.  

    + [0] {CursorWindow} ForegroundWindow.Window
    + [1] {ms_sqlce_se_notify_wndproc} ForegroundWindow.Window
    + [2] {Form1} ForegroundWindow.Window
    + [3] {Default Ime} ForegroundWindow.Window
    + [4] {Start} ForegroundWindow.Window
    + [5] {Default Ime} ForegroundWindow.Window
    + [6] {Phone} ForegroundWindow.Window
    + [7] {SchedConnNotify} ForegroundWindow.Window
    + [8] {WinCENotify} ForegroundWindow.Window
    + [9] {ConnMgrSink} ForegroundWindow.Window
    + [10] {Phone - Incoming} ForegroundWindow.Window
    + [11] {Tray} ForegroundWindow.Window

    Form1 being my test form.  

    So it seems that the first 5 or 6 will be in that order if there is no window open (the user is looking at the today screen). 

    I'm a little unsure and tentative to believe this, soley because there is hardly any information on it on the web.  


    The other big issue is that I also need to be able to monitor the window and do something when the today screen is being viewed. 

    Thoughts?
    Tuesday, January 13, 2009 10:10 PM
  • Hi,

    If you are only needing to support Windows Mobile 5.0 or higher you may like to investigate the State and Notification Broker (SNAPI). This has a property called ActiveApplication which allows you to efficiently detect whenever the active application changes on the device.

    Below is a small code snippet which demonstrates using this API from native C or C++ (the API is also available for use via the SystemState class in the .NET environment).

    #include <regext.h>  
    #include <snapi.h>  
     
    // This callback is called whenever the active application changes
    void ForegroundApplicationChanged(HREGNOTIFY hNotify,  
           DWORD dwUserData,  
           const PBYTE pData,  
           const UINT cbData)  

      // Remove the previous application title from the string
      LPCTSTR pszApplicationTitle = (LPCTSTR)pData;  
      while (*pszApplicationTitle && *pszApplicationTitle++ != 0x1B)  
        ;  
     
      // Output to the Visual Studio debugger output window
      OutputDebugString(pszApplicationTitle);  
    }  
     
    int WINAPI WinMain(HINSTANCE hInstance,  
                       HINSTANCE hPrevInstance,  
                       LPTSTR    lpCmdLine,  
                       int       nCmdShow)  
    {  
      // Request to be notified whenever the active application changes
      HREGNOTIFY hNotify;  
      HNOTIFICATIONCONDITION condition = { REG_CT_ANYCHANGE };
      RegistryNotifyCallback(SN_ACTIVEAPPLICATION_ROOT,  
                             SN_ACTIVEAPPLICATION_PATH,  
                             SN_ACTIVEAPPLICATION_VALUE,  
                             ForegroundApplicationChanged,  
                             0, &condition, &hNotify);  
     
      // Your main application logic / UI would go here
      while (true)  
        Sleep(10000);  
     
      // Clean up our resources
      RegistryCloseNotification(hNotify);  
      return 0;  

    If you run this sample within the Visual Studio debugger you should see the name of the current application being listed in the output window whenever it changes.

    The only slightly tricky bit is the string manipulation found in the ForegroundApplicationChanged function. SNAPI returns both the previously active and current application names seperated by an escape (0x1B) character, so this loop strips off the previous application title.

    When the user is viewing the desktop the title 'Desktop' will be returned.

    Hope this helps,
    Christopher Fairbairn
    Visit my blog at http://www.christec.co.nz/blog/
    Tuesday, January 13, 2009 11:39 PM
  • Dude.

    Perfect!

    Thanks so much, exactly what I needed!
    Wednesday, January 14, 2009 4:58 PM