welcome.cpp
Upload User: xhy777
Upload Date: 2007-02-14
Package Size: 24088k
Code Size: 12k
Category:

Windows Kernel

Development Platform:

Visual C++

  1. #include "resource.h"
  2. #include "welcome.h"
  3. #include "rcids.h"
  4. #include "docobj.h"
  5. #include <exdisp.h>
  6. #include <htiframe.h>
  7. #include <shdguid.h>
  8. #include <shlguid.h>
  9. #include <inetreg.h>
  10. #include <regstr.h>
  11. #include <windows.h>
  12. #include <shlwapi.h>
  13. #include <shlwapip.h>
  14. #include <mshtml.h>
  15. const VARIANT c_vaEmpty = {0};
  16. //
  17. // BUGBUG: Remove this ugly const to non-const casting if we can
  18. //  figure out how to put const in IDL files.
  19. //
  20. #define PVAREMPTY ((VARIANT*)&c_vaEmpty)
  21. #define ARRAYSIZE(a)    (sizeof(a)/sizeof(a[0]))
  22. #define USA             0x0409
  23. #define CANADA          0x1009
  24. #define SHOWIE4         "ShowIE4"
  25. #define SHOWIE4PLUS     "ShowIE4Plus"
  26. #define DOES_NOT_EXIST  0xBAADF00D
  27. #define STR_BSTR    0
  28. #define STR_OLESTR  1
  29. #define BSTRFROMANSI(x) (BSTR)MakeWideStrFromAnsi((LPSTR)(x), STR_BSTR)
  30. #define TO_ASCII(x) (char)((unsigned char)x + 0x30)
  31. #define REGSTR_PATH_ADVANCEDLIST REGSTR_PATH_IEXPLORER TEXT("\AdvancedOptions\BROWSE")
  32. char const g_szRegTips[]        = REGSTR_PATH_EXPLORER      "\Tips";
  33. char const g_szRegAdvWelcome[]  = REGSTR_PATH_ADVANCEDLIST  "\WELCOME";
  34. //  local functions
  35. //
  36. int  WinMainT (HINSTANCE, HINSTANCE, LPTSTR, int);
  37. BOOL LaunchIE4Instance (LPTSTR);
  38. BOOL ParseCommandLine(LPCTSTR);
  39. DWORD GetShowIE4State (void);
  40. void SetShowIE4State (BOOL);
  41. BOOL GetShowIE4PlusString (LPSTR, int);
  42. void LaunchIE4PlusString (LPSTR);
  43. void InitAdvOptRegKey (void);
  44. EXTERN_C int _stdcall ModuleEntry(void)
  45. {
  46.     int i;
  47.     STARTUPINFOA si;
  48.     LPTSTR pszCmdLine = GetCommandLine();
  49.     // We don't want the "No disk in drive X:" requesters, so we set
  50.     // the critical error mask such that calls will just silently fail
  51.     SetErrorMode(SEM_FAILCRITICALERRORS);
  52.     if (*pszCmdLine == TEXT('"')) {                                  
  53.         while (*++pszCmdLine && (*pszCmdLine != TEXT('"')));
  54.         if (*pszCmdLine == TEXT('"'))
  55.             pszCmdLine++;
  56.     } else
  57.         while (*pszCmdLine > TEXT(' ')) pszCmdLine++;
  58.     // Skip past any white space preceeding the second token.
  59.     while (*pszCmdLine && (*pszCmdLine <= TEXT(' '))) pszCmdLine++;
  60.     si.dwFlags = 0;
  61.     GetStartupInfoA(&si);
  62.     i = WinMainT(GetModuleHandle(NULL), NULL, pszCmdLine, si.dwFlags & STARTF_USESHOWWINDOW ? si.wShowWindow : SW_SHOWDEFAULT);
  63.     ExitThread(i);  // We only come here when we are not the shell...
  64.     return(i);
  65. }
  66. int WinMainT(HINSTANCE hinst, HINSTANCE hPrevInstance, LPTSTR lpszCmdLine, int nCmdShow)
  67. {
  68.     if (SUCCEEDED(CoInitialize(NULL))) 
  69.     {
  70.         BOOL bLocale;
  71.         LCID localeID;
  72.         TCHAR szResourceURL[MAX_PATH];
  73.         DWORD dwShowIE4 = GetShowIE4State();
  74.         BOOL bBrowserOnly = (WhichPlatform() != PLATFORM_INTEGRATED);
  75.         // loadwc in browser only happens to pass us /f every single time
  76.         BOOL bFirstTime = (dwShowIE4 == DOES_NOT_EXIST);
  77.         if (!bBrowserOnly)
  78.             bFirstTime |= ParseCommandLine(lpszCmdLine);
  79.         // we don't want a /f to do this code path...just a 'true' first time.
  80.         if (dwShowIE4 == DOES_NOT_EXIST) 
  81.         {
  82.             InitAdvOptRegKey();
  83.             SetShowIE4State(TRUE);
  84.         }
  85.         localeID = GetUserDefaultLCID();
  86.         bLocale = ((localeID == USA) || (localeID == CANADA));
  87.         if (GetShowIE4PlusString(szResourceURL, ARRAYSIZE(szResourceURL))) 
  88.         {
  89.             LaunchIE4PlusString(szResourceURL);
  90.         }
  91.         else if (dwShowIE4) 
  92.         {
  93.             LoadString(hinst, IDS_RESOURCE_URL, szResourceURL, ARRAYSIZE(szResourceURL));
  94.             wsprintf(szResourceURL, "%s#FirstTime=%c#Contest=%c#MinimalTour=%c", szResourceURL, TO_ASCII(bFirstTime), TO_ASCII(bLocale), TO_ASCII(bBrowserOnly));
  95.             LaunchIE4Instance(szResourceURL);
  96.         }
  97.         CoUninitialize();
  98.     }
  99.     ExitProcess(0);
  100.     return(0);
  101. }   /*  end WinMainT() */
  102. //=--------------------------------------------------------------------------=
  103. // MakeWideFromAnsi
  104. //=--------------------------------------------------------------------------=
  105. // given a string, make a BSTR out of it.
  106. //
  107. // Parameters:
  108. //    LPSTR         - [in]
  109. //    BYTE          - [in]
  110. //
  111. // Output:
  112. //    LPWSTR        - needs to be cast to final desired result
  113. //
  114. // Notes:
  115. //
  116. LPWSTR MakeWideStrFromAnsi (LPSTR psz, BYTE bType)
  117. {
  118.     int i;
  119.     LPWSTR pwsz;
  120.     if (!psz)
  121.         return(NULL);
  122.     
  123.     if ((i = MultiByteToWideChar(CP_ACP, 0, psz, -1, NULL, 0)) <= 0)    // compute the length of the required BSTR
  124.         return NULL;                                                                                            
  125.     switch (bType) {                                                    // allocate the widestr, +1 for null
  126.         case STR_BSTR:                                                                                                
  127.             pwsz = (LPWSTR)SysAllocStringLen(NULL, (i - 1));            // SysAllocStringLen adds 1
  128.             break;
  129.         case STR_OLESTR:
  130.             pwsz = (LPWSTR)CoTaskMemAlloc(i * sizeof(WCHAR));
  131.             break;
  132.         default:
  133.             return(NULL);
  134.     }
  135.     if (!pwsz)
  136.         return(NULL);
  137.     MultiByteToWideChar(CP_ACP, 0, psz, -1, pwsz, i);
  138.     pwsz[i - 1] = 0;
  139.     return(pwsz);
  140. }   /*  MakeWideStrFromAnsi() */
  141. BOOL LaunchIE4Instance (LPTSTR szResourceURL)
  142. {
  143.     IWebBrowser2 *pwb;
  144.     HRESULT hres = CoCreateInstance(CLSID_InternetExplorer, NULL,
  145.                                     CLSCTX_LOCAL_SERVER, IID_IWebBrowser2, (void **)&pwb);
  146.     if (SUCCEEDED(hres)) 
  147.     {
  148.         int dx, dy;
  149.         DWORD dwFlags;
  150.         //
  151.         //  this marks this window as a third party window, 
  152.         //  so that the window is not reused.
  153.         //
  154.         pwb->put_RegisterAsBrowser(VARIANT_TRUE);
  155.         // turn off scrolling & resizing
  156.         ITargetFrame2* ptgf;
  157.         if (SUCCEEDED(pwb->QueryInterface(IID_ITargetFrame2, (void **) &ptgf))) 
  158.         {
  159.             if (SUCCEEDED(ptgf->GetFrameOptions(&dwFlags))) {
  160.                 dwFlags &= ~(FRAMEOPTIONS_SCROLL_YES | FRAMEOPTIONS_SCROLL_NO | FRAMEOPTIONS_SCROLL_AUTO);
  161.                 dwFlags |= FRAMEOPTIONS_SCROLL_NO;
  162.                 ptgf->SetFrameOptions(dwFlags);
  163.             }
  164.             ptgf->Release();
  165.         }
  166.         IServiceProvider *psp;
  167.         if (SUCCEEDED(pwb->QueryInterface(IID_IServiceProvider, (void**) &psp))) 
  168.         {
  169.             IHTMLWindow2 *phw;
  170.             if (SUCCEEDED(psp->QueryService(IID_IHTMLWindow2, IID_IHTMLWindow2, (void**)&phw))) 
  171.             {
  172.                 VARIANT var;
  173.                 var.vt = VT_BOOL;
  174.                 var.boolVal = 666;
  175.                 phw->put_opener(var);
  176.                 phw->Release();
  177.             } 
  178.             psp->Release();
  179.         }
  180.         // turn off chrome
  181.         pwb->put_MenuBar(FALSE);
  182.         pwb->put_StatusBar(FALSE);
  183.         pwb->put_ToolBar(FALSE);
  184.         pwb->put_AddressBar(FALSE);
  185.         pwb->put_Resizable(FALSE);
  186.         // set client area size
  187.         int iWidth = 466L;
  188.         int iHeight = 286L;
  189.         pwb->ClientToWindow(&iWidth, &iHeight);
  190.         if (iWidth > 0)
  191.             pwb->put_Width(iWidth);
  192.         
  193.         if (iHeight > 0)
  194.             pwb->put_Height(iHeight);
  195.         if ((dx = ((GetSystemMetrics(SM_CXSCREEN) - iWidth) / 2)) > 0)     // center the on screen window 
  196.             pwb->put_Left(dx);
  197.         if ((dy = ((GetSystemMetrics(SM_CYSCREEN) - iHeight) / 2)) > 0)
  198.             pwb->put_Top(dy);    
  199.         pwb->put_Visible(TRUE);
  200. //        CreateParmsFile();
  201.         BSTR bstr = BSTRFROMANSI(szResourceURL);
  202.         HRESULT hr = pwb->Navigate(bstr, PVAREMPTY, PVAREMPTY, PVAREMPTY, PVAREMPTY);
  203.         SysFreeString(bstr);
  204.         pwb->Release();
  205.         return TRUE;
  206.     }
  207.     return FALSE;
  208. }
  209. BOOL ParseCommandLine(LPCTSTR pszCmdLine)
  210. {
  211.     return (lstrcmpi(pszCmdLine, "-f") == 0) || (lstrcmpi(pszCmdLine, "/f") == 0);
  212. }
  213. DWORD GetShowIE4State (void)
  214. {
  215.     HKEY hkey;
  216.     DWORD dwShow = DOES_NOT_EXIST, dwTemp, dwSize = sizeof(DWORD);
  217.     if (RegOpenKeyEx(HKEY_CURRENT_USER, g_szRegTips, 0, KEY_QUERY_VALUE, &hkey) == ERROR_SUCCESS) 
  218.     {
  219.         RegQueryValueEx(hkey, SHOWIE4, 0, &dwTemp, (LPBYTE)&dwShow, &dwSize);
  220.         RegCloseKey(hkey);
  221.     }
  222.     return dwShow;
  223. }
  224. void SetShowIE4State (BOOL value)
  225. {
  226.     HKEY hkey;
  227.     DWORD dwDisp;
  228.     if (RegCreateKeyEx(HKEY_CURRENT_USER, g_szRegTips, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, &dwDisp) == ERROR_SUCCESS) {
  229.         RegSetValueEx(hkey, SHOWIE4, 0, REG_DWORD, (CONST LPBYTE)&value, (DWORD)sizeof(value));
  230.         RegCloseKey(hkey);
  231.     }
  232. }   /*  SetBrowserOnlyRegKey() */
  233. BOOL GetShowIE4PlusString(LPSTR pszString, int cStringSize)
  234. {
  235.     HKEY hkey;
  236.     DWORD dwTemp, dwSize = cStringSize;
  237.     BOOL fResult = FALSE;
  238.     if (RegOpenKeyEx(HKEY_CURRENT_USER, g_szRegTips, 0, KEY_ALL_ACCESS, &hkey) == ERROR_SUCCESS) {
  239.         if (RegQueryValueEx(hkey, SHOWIE4PLUS, 0, &dwTemp, (LPBYTE)pszString, &dwSize) == ERROR_SUCCESS) {
  240.             fResult = TRUE;
  241.             // IE4 Plus!'s initial experience is a one-time thing so we should
  242.             // delete the value and make sure we never detect it again.
  243.             RegDeleteValue(hkey, SHOWIE4PLUS);
  244.         }
  245.         RegCloseKey(hkey);
  246.     }
  247.     return(fResult);
  248. }
  249. // borrowed from shdocvwutil.cpp
  250. LPTSTR _PathGetArgs(LPCTSTR pszPath)
  251. {
  252.     BOOL fInQuotes = FALSE;
  253.     if (!pszPath)
  254.         return NULL;
  255.     while (*pszPath)
  256.     {
  257.         if (*pszPath == TEXT('"'))
  258.             fInQuotes = !fInQuotes;
  259.         else if (!fInQuotes && *pszPath == TEXT(' '))
  260.             return (LPTSTR)pszPath+1;
  261.         pszPath = CharNext(pszPath);
  262.     }
  263.     return (LPTSTR)pszPath;
  264. }
  265. void LaunchIE4PlusString (LPSTR pszCmdLine)
  266. {
  267.     LPTSTR pszArgs = NULL;
  268.     pszArgs = _PathGetArgs(pszCmdLine);
  269.     if(pszArgs)
  270.         *(pszArgs - 1) = TEXT('');   // clobber the ' '
  271.     ShellExecute(NULL, NULL, pszCmdLine, pszArgs, NULL, SW_SHOWNORMAL );
  272. }
  273. #define HELP_STRING "iexplore.hlp#00000"
  274. #define CHECK_BOX   "checkbox"
  275. #define VALUE_NAME  SHOWIE4
  276. void InitAdvOptRegKey (void)
  277. {
  278.     HKEY hkey;
  279.     DWORD dwDisp, value;
  280.     char szListText[MAX_PATH];
  281.     LoadString(GetModuleHandle(NULL), IDS_LIST_TEXT, (LPTSTR)&szListText, MAX_PATH);
  282.     if (RegCreateKeyEx(HKEY_LOCAL_MACHINE, g_szRegAdvWelcome, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, &dwDisp) == ERROR_SUCCESS) {
  283.         RegSetValueEx(hkey, "CheckedValue",   0, REG_DWORD, (CONST LPBYTE)&(value = TRUE),       (DWORD)sizeof(value));
  284.         RegSetValueEx(hkey, "DefaultValue",   0, REG_DWORD, (CONST LPBYTE)&(value = TRUE),       (DWORD)sizeof(value));
  285.         RegSetValueEx(hkey, "HelpID",         0, REG_SZ,    (CONST LPBYTE)HELP_STRING,           (DWORD)sizeof(HELP_STRING));
  286.         RegSetValueEx(hkey, "HKeyRoot",       0, REG_DWORD, (CONST LPBYTE)&(value = 0x80000001), (DWORD)sizeof(value));
  287.         RegSetValueEx(hkey, "RegPath",        0, REG_SZ,    (CONST LPBYTE)g_szRegTips,           (DWORD)sizeof(g_szRegTips));
  288.         RegSetValueEx(hkey, "Text",           0, REG_SZ,    (CONST LPBYTE)szListText,            (DWORD)(strlen(szListText) + 1));
  289.         RegSetValueEx(hkey, "Type",           0, REG_SZ,    (CONST LPBYTE)CHECK_BOX,             (DWORD)sizeof(CHECK_BOX));
  290.         RegSetValueEx(hkey, "UncheckedValue", 0, REG_DWORD, (CONST LPBYTE)&(value = FALSE),      (DWORD)sizeof(value));
  291.         RegSetValueEx(hkey, "ValueName",      0, REG_SZ,    (CONST LPBYTE)VALUE_NAME,            (DWORD)sizeof(VALUE_NAME));
  292.         RegCloseKey(hkey);
  293.     }
  294. }   /*  end InitAdvOptRegKey() */