[Author: zork]
[google-gears.git] / gears / base / common / vista_utils.cc
blobaecbe82104856a24f674f81d7c830a1e9d1cb8ef
1 // Copyright 2006, Google Inc.
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are met:
5 //
6 // 1. Redistributions of source code must retain the above copyright notice,
7 // this list of conditions and the following disclaimer.
8 // 2. Redistributions in binary form must reproduce the above copyright notice,
9 // this list of conditions and the following disclaimer in the documentation
10 // and/or other materials provided with the distribution.
11 // 3. Neither the name of Google Inc. nor the names of its contributors may be
12 // used to endorse or promote products derived from this software without
13 // specific prior written permission.
15 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
16 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
18 // EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include <assert.h>
27 #include <tchar.h>
28 #include "gears/base/common/vista_utils.h"
29 #ifdef WINCE
30 // Force GetProcAddress to be defined to the ASCII version in order to
31 // make the call compile.
32 #undef GetProcAddress
33 #define GetProcAddress GetProcAddressA
34 #endif
37 bool VistaUtils::IsRunningOnVista() {
38 static DWORD os_major_version = 0;
39 static DWORD platform_id = 0;
41 if (0 == os_major_version) {
42 OSVERSIONINFO os_version = {0};
43 os_version.dwOSVersionInfoSize = sizeof(os_version);
44 GetVersionEx(&os_version);
45 os_major_version = os_version.dwMajorVersion;
46 platform_id = os_version.dwPlatformId;
49 return (6 == os_major_version) && (VER_PLATFORM_WIN32_NT == platform_id);
54 bool VistaUtils::IEIsProtectedModeProcess() {
55 if (!IsRunningOnVista()) {
56 return false;
59 typedef HRESULT (WINAPI *IEIsProtectedModeProcessProc)(BOOL *result);
61 // We use GetModuleHandle here because this call is really only
62 // valid inside IE. IF we are already in IE then ieframe.dll is
63 // already loaded. If we are not running in IE we want to return
64 // a failure code anyway.
65 IEIsProtectedModeProcessProc function =
66 reinterpret_cast<IEIsProtectedModeProcessProc>(
67 GetProcAddress(
68 GetModuleHandle(_T("ieframe.dll")),
69 "IEIsProtectedModeProcess"));
70 if (!function) {
71 return false;
74 BOOL is_protected_mode = FALSE;
75 HRESULT hr = function(&is_protected_mode);
76 if (FAILED(hr)) {
77 return false;
80 return is_protected_mode ? true : false;
85 bool VistaUtils::GetLocalAppDataLowPath(std::string16 *fullpath) {
86 if (!IsRunningOnVista()) {
87 return false;
90 HMODULE shell32module = GetModuleHandle(_T("shell32.dll"));
91 if (!shell32module) {
92 // We depend on shell32.dll having already been loaded
93 assert(false);
94 return false;
97 // Defined in the platform SDK for Vista
98 const DWORD KF_FLAG_CREATE = 0x00008000;
99 const GUID FOLDERID_LocalAppDataLow =
100 {0xA520A1A4, 0x1780, 0x4FF6,
101 {0xBD, 0x18, 0x16, 0x73, 0x43, 0xC5, 0xAF, 0x16}};
103 typedef HRESULT (WINAPI *SHGetKnownFolderPathProc)(REFGUID rfid,
104 DWORD wdFlags,
105 HANDLE hToken,
106 PWSTR *ppszPath);
107 SHGetKnownFolderPathProc function =
108 reinterpret_cast<SHGetKnownFolderPathProc>(
109 GetProcAddress(shell32module,
110 "SHGetKnownFolderPath"));
111 if (!function) {
112 return false;
115 WCHAR *path = NULL;
116 HRESULT hr = function(FOLDERID_LocalAppDataLow, KF_FLAG_CREATE, NULL, &path);
117 if (FAILED(hr)) {
118 return false;
121 *fullpath = path;
122 CoTaskMemFree(path);
123 return true;