systray.c
Upload User: xhy777
Upload Date: 2007-02-14
Package Size: 24088k
Code Size: 4k
Category:

Windows Kernel

Development Platform:

Visual C++

  1. /*******************************************************************************
  2. *
  3. *  (C) COPYRIGHT MICROSOFT CORP., 1993-1994
  4. *
  5. *  TITLE:       SYSTRAY.C
  6. *
  7. *  VERSION:     2.0
  8. *
  9. *  AUTHOR:      TCS/RAL
  10. *
  11. *  DATE:        08 Feb 1994
  12. *
  13. ********************************************************************************
  14. *
  15. *  CHANGE LOG:
  16. *
  17. *  DATE        REV DESCRIPTION
  18. *  ----------- --- -------------------------------------------------------------
  19. *  08 Feb 1994 TCS Original implementation.
  20. *  11 Nov 1994 RAL Converted from batmeter to systray
  21. *  11 Aug 1995 JEM Split batmeter functions into power.c & minor enahncements
  22. *  23 Oct 1995 Shawnb Unicode enabled
  23. *  07 Aug 1998 dsheldon Created systray.dll and made this into a stub exe
  24. *
  25. *******************************************************************************/
  26. #include <nt.h>
  27. #include <ntrtl.h>
  28. #include <nturtl.h>
  29. #include <windows.h>
  30. #include <shlobj.h>
  31. #include <shellapi.h>
  32. #include <systrayp.h>
  33. #include <initguid.h>
  34. #include <stclsid.h>
  35. //  Global instance handle of this application.
  36. HINSTANCE g_hInstance;
  37. static const TCHAR g_szWindowClassName[]     = SYSTRAY_CLASSNAME;
  38. /**************************************************************************/
  39. INT intval(LPCTSTR lpsz)
  40. {
  41.     INT i = 0;
  42.     while (*lpsz >= TEXT ('0') && *lpsz <= TEXT ('9'))
  43.     {
  44.         i = i * 10 + (int)(*lpsz - TEXT ('0'));
  45.         lpsz++;
  46.     }
  47.     return(i);
  48. }
  49. // stolen from the CRT, used to shrink our code
  50. int _stdcall ModuleEntry(void)
  51. {
  52.     int i;
  53.     STARTUPINFO si;
  54.     LPTSTR pszCmdLine = GetCommandLine ();
  55.     if ( *pszCmdLine == TEXT ('"') )
  56.     {
  57.         /*
  58.          * Scan, and skip over, subsequent characters until
  59.          * another double-quote or a null is encountered.
  60.          */
  61.         while ( *++pszCmdLine && (*pszCmdLine != TEXT ('"')) )
  62.             ;
  63.         /*
  64.          * If we stopped on a double-quote (usual case), skip
  65.          * over it.
  66.          */
  67.         if ( *pszCmdLine == TEXT ('"') )
  68.             pszCmdLine++;
  69.     }
  70.     else
  71.     {
  72.         while (*pszCmdLine > ' ')
  73.             pszCmdLine++;
  74.     }
  75.     /*
  76.      * Skip past any white space preceeding the second token.
  77.      */
  78.     while (*pszCmdLine && (*pszCmdLine <= ' '))
  79.     {
  80.         pszCmdLine++;
  81.     }
  82.     si.dwFlags = 0;
  83.     GetStartupInfo (&si);
  84.     i = WinMain(GetModuleHandle(NULL), NULL, (LPSTR)pszCmdLine,
  85.                 si.dwFlags & STARTF_USESHOWWINDOW ? si.wShowWindow : SW_SHOWDEFAULT);
  86.     ExitProcess(i);
  87.     return i;    // We never come here.
  88. }
  89. /*******************************************************************************
  90. *
  91. *  WinMain
  92. *
  93. *  DESCRIPTION:
  94. *
  95. *  PARAMETERS:
  96. *       if lpCmdLine contains an integer value then we'll enable that service
  97. *
  98. *******************************************************************************/
  99. int
  100.     PASCAL
  101.     WinMain(
  102.            HINSTANCE hInstance,
  103.            HINSTANCE hPrevInstance,
  104.            LPSTR lpszCmdLine,
  105.            int nCmdShow
  106.            )
  107. {
  108.     HWND hWnd;
  109.     HWND hExistWnd = FindWindow(g_szWindowClassName, NULL);
  110.     UINT iEnableServ = intval((LPTSTR)lpszCmdLine);
  111.     g_hInstance = hInstance;
  112.     if (hExistWnd)
  113.     {
  114.         //
  115.         // NOTE: Send an enable message even if the command line parameter
  116.         //       is 0 to force us to re-check for all enabled services.
  117.         //
  118.         PostMessage(hExistWnd, STWM_ENABLESERVICE, iEnableServ, TRUE);
  119.         goto ExitMain;
  120.     }
  121.     else
  122.     {
  123.         int i;
  124.         // We have to inject systray.dll into the explorer process
  125.         if (SUCCEEDED(SHLoadInProc(&CLSID_SysTrayInvoker)))
  126.         {
  127.             // Wait for up to 30 seconds for the window to be created, 
  128.             // send our message every second
  129.         
  130.             for (i = 0; i < 30; i ++)
  131.             {
  132.                 Sleep(1000);
  133.                 hExistWnd = FindWindow(g_szWindowClassName, NULL);
  134.                 if (hExistWnd)
  135.                 {
  136.                     PostMessage(hExistWnd, STWM_ENABLESERVICE, iEnableServ, TRUE);
  137.                     goto ExitMain;        
  138.                 }
  139.             }
  140.         }
  141.     }
  142. ExitMain:
  143.     return 0;
  144. }