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

Windows Kernel

Development Platform:

Visual C++

  1. //---------------------------------------------------------------------------
  2. //
  3. //---------------------------------------------------------------------------
  4. #include "control.h"
  5. #include <cpl.h>
  6. #include <cplp.h>
  7. #include "rcids.h"
  8. BOOL ImmDisableIME(DWORD dwThreadId);
  9. //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  10. //  LEAVE THESE IN ENGLISH
  11. //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  12. const TCHAR c_szCtlPanelClass[] = TEXT("CtlPanelClass");
  13. const TCHAR c_szRunDLLShell32Etc[] = TEXT("rundll32.exe Shell32.dll,Control_RunDLL ");
  14. const TCHAR c_szDoPrinters[] = 
  15.     TEXT("explorer.exe "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}\::{21EC2020-3AEA-1069-A2DD-08002B30309D}\::{2227A280-3AEA-1069-A2DE-08002B30309D}"");
  16. const TCHAR c_szDoFonts[] = 
  17.     TEXT("explorer.exe "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}\::{21EC2020-3AEA-1069-A2DD-08002B30309D}\::{D20EA4E1-3957-11d2-A40B-0C5020524152}"");
  18. const TCHAR c_szDoAdminTools[] = 
  19.     TEXT("explorer.exe "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}\::{21EC2020-3AEA-1069-A2DD-08002B30309D}\::{D20EA4E1-3957-11d2-A40B-0C5020524153}"");
  20. const TCHAR c_szDoSchedTasks[] = 
  21.     TEXT("explorer.exe "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}\::{21EC2020-3AEA-1069-A2DD-08002B30309D}\::{D6277990-4C6A-11CF-8D87-00AA0060F5BF}"");
  22. const TCHAR c_szDoNetConnections[] = 
  23.     TEXT("explorer.exe "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}\::{21EC2020-3AEA-1069-A2DD-08002B30309D}\::{7007ACC7-3202-11D1-AAD2-00805FC1270E}"");
  24. const TCHAR c_szDoUserPassword[] = 
  25.     TEXT("rundll32.exe netplwiz.dll,UsersRunDll");
  26. typedef struct
  27. {
  28.     const TCHAR *oldform;
  29.     const TCHAR *newform;
  30.     BOOL         fDoNotUseRunDLL;
  31. } COMPATCPL;
  32. typedef COMPATCPL const *LPCCOMPATCPL;
  33. COMPATCPL const c_aCompatCpls[] =
  34. {
  35.     {   TEXT("DESKTOP"),          TEXT("desk.cpl")      , 0},
  36.     {   TEXT("COLOR"),            TEXT("desk.cpl,,2")   , 0},
  37.     {   TEXT("DATE/TIME"),        TEXT("timedate.cpl")  , 0},
  38.     {   TEXT("PORTS"),            TEXT("sysdm.cpl,,1")  , 0},
  39.     {   TEXT("INTERNATIONAL"),    TEXT("intl.cpl")      , 0},
  40.     {   TEXT("MOUSE"),            TEXT("main.cpl")      , 0},
  41.     {   TEXT("KEYBOARD"),         TEXT("main.cpl @1")   , 0},
  42.     {   TEXT("FOLDERS"),          TEXT("appwiz.cpl @1") , 0},
  43.     {   TEXT("NETWARE"),          TEXT("nwc.cpl")       , 0},
  44.     {   TEXT("TELEPHONY"),        TEXT("telephon.cpl")  , 0},
  45.     {   TEXT("INFRARED"),         TEXT("irprops.cpl")   , 0},
  46.     {   TEXT("PRINTERS"),         c_szDoPrinters        , 1},
  47.     {   TEXT("FONTS"),            c_szDoFonts           , 1},
  48.     {   TEXT("ADMINTOOLS"),       c_szDoAdminTools      , 1},
  49.     {   TEXT("SCHEDTASKS"),       c_szDoSchedTasks      , 1},
  50.     {   TEXT("USERPASSWORDS"),    c_szDoUserPassword    , 1},
  51.     {   TEXT("NETCONNECTIONS"),   c_szDoNetConnections  , 1}
  52. };
  53. #define NUMCOMPATCPLS   ARRAYSIZE(c_aCompatCpls)
  54. //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  55. // Timer
  56. #define TIMER_QUITNOW   1
  57. #define TIMEOUT         10000
  58. #define DM_CPTRACE      0
  59. //----------------------------------------------------------------------------
  60. #ifdef UNICODE
  61. #define WinExec WinExecW
  62. //
  63. //  For UNICODE create a companion to ANSI only base WinExec api.
  64. //
  65. UINT WINAPI WinExecW (LPTSTR lpCmdLine, UINT uCmdShow)
  66. {
  67.     STARTUPINFO         StartupInfo;
  68.     PROCESS_INFORMATION ProcessInformation;
  69.     //
  70.     // Create the process
  71.     //
  72.     
  73.     memset (&StartupInfo, 0, sizeof(StartupInfo));
  74.     
  75.     StartupInfo.cb = sizeof(StartupInfo);
  76.     
  77.     StartupInfo.wShowWindow = (WORD)uCmdShow;
  78.     
  79.     if (CreateProcess ( NULL,
  80.                         lpCmdLine,            // CommandLine
  81.                         NULL,
  82.                         NULL,
  83.                         FALSE,
  84.                         NORMAL_PRIORITY_CLASS,
  85.                         NULL,
  86.                         NULL,
  87.                         &StartupInfo,
  88.                         &ProcessInformation
  89.                         ))
  90.     {
  91.         CloseHandle(ProcessInformation.hThread);
  92.         CloseHandle(ProcessInformation.hProcess);
  93.     }
  94.     return(1);
  95. }
  96. #endif  // UNICODE
  97. //---------------------------------------------------------------------------
  98. LRESULT CALLBACK DummyControlPanelProc(HWND hwnd, UINT uMsg, WPARAM wparam, LPARAM lparam)
  99. {
  100.     TCHAR sz[sizeof(c_szDoPrinters)];
  101.     switch (uMsg)
  102.     {
  103.         case WM_CREATE:
  104.             DebugMsg(DM_CPTRACE, TEXT("cp.dcpp: Created..."));
  105.             // We only want to hang around for a little while.
  106.             SetTimer(hwnd, TIMER_QUITNOW, TIMEOUT, NULL);
  107.             return 0;
  108.         case WM_DESTROY:
  109.             DebugMsg(DM_CPTRACE, TEXT("cp.dcpp: Destroyed..."));
  110.             // Quit the app when this window goes away.
  111.             PostQuitMessage(0);
  112.             return 0;
  113.         case WM_TIMER:
  114.             DebugMsg(DM_CPTRACE, TEXT("cp.dcpp: Timer %d"), wparam);
  115.             if (wparam == TIMER_QUITNOW)
  116.             {
  117.                 // Get this window to go away.
  118.                 DestroyWindow(hwnd);
  119.             }
  120.             return 0;
  121.         case WM_COMMAND:
  122.             DebugMsg(DM_CPTRACE, TEXT("cp.dcpp: Command %d"), wparam);
  123.             // NB Hack for hollywood - they send a menu command to try
  124.             // and open the printers applet. They try to search control panels
  125.             // menu for the printers item and then post the associated command.
  126.             // As our fake window doesn't have a menu they can't find the item
  127.             // and post us a -1 instead (ripping on the way).
  128.             if (wparam == (WPARAM)-1)
  129.             {
  130.                 lstrcpy(sz, c_szDoPrinters);
  131.                 WinExec(sz, SW_SHOWNORMAL);
  132.             }
  133.             return 0;
  134.         default:
  135.             DebugMsg(DM_CPTRACE, TEXT("cp.dcpp: %x %x %x %x"), hwnd, uMsg, wparam, lparam);
  136.             return DefWindowProc(hwnd, uMsg, wparam, lparam);
  137.     }
  138. }
  139. //---------------------------------------------------------------------------
  140. HWND _CreateDummyControlPanel(HINSTANCE hinst)
  141. {
  142.     WNDCLASS wc;
  143.     wc.style = 0;
  144.     wc.lpfnWndProc = DummyControlPanelProc;
  145.     wc.cbClsExtra = 0;
  146.     wc.cbWndExtra = 0;
  147.     wc.hInstance = hinst;
  148.     wc.hIcon = NULL;
  149.     wc.hCursor = NULL;
  150.     wc.hbrBackground = NULL;
  151.     wc.lpszMenuName = NULL;
  152.     wc.lpszClassName = c_szCtlPanelClass;
  153.     RegisterClass(&wc);
  154.     return CreateWindow(c_szCtlPanelClass, NULL, 0, 0, 0, 0, 0, NULL, NULL, hinst, NULL);
  155. }
  156. void ProcessPolicy(void)
  157. {
  158.     HINSTANCE hInst;
  159.     APPLET_PROC pfnCPLApplet;
  160.     hInst = LoadLibrary (TEXT("desk.cpl"));
  161.     if (!hInst) {
  162.         return;
  163.     }
  164.     pfnCPLApplet = (APPLET_PROC) GetProcAddress (hInst, "CPlApplet");
  165.     if (pfnCPLApplet) {
  166.         (*pfnCPLApplet)(NULL, CPL_POLICYREFRESH, 0, 0);
  167.     }
  168.     FreeLibrary (hInst);
  169. }
  170. //---------------------------------------------------------------------------
  171. int WinMainT(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
  172. {
  173.     TCHAR sz[sizeof(c_szDoPrinters)];
  174.     MSG msg;
  175.     DebugMsg(DM_TRACE, TEXT("cp.wm: Control starting."));
  176.     ImmDisableIME(0);
  177.     _CreateDummyControlPanel(hInstance);
  178.     lstrcpy(sz, c_szRunDLLShell32Etc);
  179.     // we need to check for PANEL passed in as an arg.  The run dialog
  180.     // autocomplete shows "Control Panel" as a choice and we used to
  181.     // interpret this as Control with panel as an arg.  So if we have
  182.     // panel as an arg then we do the same processing as when we have
  183.     // "Control" only.
  184.     if (*lpCmdLine && lstrcmpi(lpCmdLine, TEXT("PANEL")))
  185.     {
  186.         LPCCOMPATCPL item = c_aCompatCpls;
  187.         int i = NUMCOMPATCPLS;
  188.         //
  189.         // Policy hook.  Userenv.dll will call control.exe with the
  190.         // /policy command line switch.  If so, we need to load the
  191.         // desk.cpl applet and refresh the colors / bitmap.
  192.         //
  193.         if (lstrcmpi(TEXT("/policy"), lpCmdLine) == 0) {
  194.             ProcessPolicy();
  195.             return TRUE;
  196.         }
  197.         //
  198.         // COMPAT HACK: special case some applets since apps depend on them
  199.         //
  200.         while (i-- > 0)
  201.         {
  202.             if (lstrcmpi(item->oldform, lpCmdLine) == 0)
  203.             {
  204.                 if (item->fDoNotUseRunDLL)
  205.                     lstrcpy(sz, item->newform);
  206.                 else
  207.                     lstrcat(sz, item->newform);
  208.                 break;
  209.             }
  210.             item++;
  211.         }
  212.         //
  213.         // normal case (pass command line thru)
  214.         //
  215.         if (i < 0)
  216.             lstrcat(sz, lpCmdLine);
  217.     }
  218.     // HACK: NerdPerfect tries to open a hidden control panel to talk to
  219.     // we are blowing off fixing the communication stuff so just make
  220.     // sure the folder does not appear hidden
  221.     if (nCmdShow == SW_HIDE)
  222.         nCmdShow = SW_SHOWNORMAL;
  223.     WinExec(sz, nCmdShow);
  224.     while (GetMessage(&msg, NULL, 0, 0))
  225.     {
  226.         DispatchMessage(&msg);
  227.     }
  228.     DebugMsg(DM_TRACE, TEXT("cp.wm: Control exiting."));
  229.     return TRUE;
  230. }
  231. #ifdef WIN32
  232. //---------------------------------------------------------------------------
  233. // Stolen from the CRT, used to shrink our code.
  234. int _stdcall ModuleEntry(void)
  235. {
  236.     STARTUPINFO si;
  237.     LPTSTR pszCmdLine = GetCommandLine();
  238.     if ( *pszCmdLine == TEXT('"') ) {
  239.         /*
  240.          * Scan, and skip over, subsequent characters until
  241.          * another double-quote or a null is encountered.
  242.          */
  243.         while ( *++pszCmdLine && (*pszCmdLine
  244.              != TEXT('"')) );
  245.         /*
  246.          * If we stopped on a double-quote (usual case), skip
  247.          * over it.
  248.          */
  249.         if ( *pszCmdLine == TEXT('"') )
  250.             pszCmdLine++;
  251.     }
  252.     else {
  253.         while (*pszCmdLine > TEXT(' '))
  254.             pszCmdLine++;
  255.     }
  256.     /*
  257.      * Skip past any white space preceeding the second token.
  258.      */
  259.     while (*pszCmdLine && (*pszCmdLine <= TEXT(' '))) {
  260.         pszCmdLine++;
  261.     }
  262.     si.dwFlags = 0;
  263.     GetStartupInfo(&si);
  264.     return WinMainT(GetModuleHandle(NULL), NULL, pszCmdLine,
  265.                    si.dwFlags & STARTF_USESHOWWINDOW ? si.wShowWindow : SW_SHOWDEFAULT);
  266. }
  267. #endif