GME  13
UACUtils.h
Go to the documentation of this file.
00001 #pragma once
00002 
00003 #include "strsafe.h"
00004 
00005 class CUACUtils
00006 {
00007 public:
00008         static bool isVistaOrLater() {
00009                 OSVERSIONINFO osvi;
00010 
00011                 ::ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
00012                 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
00013 
00014                 GetVersionEx(&osvi);
00015 
00016                 return (osvi.dwMajorVersion >= 6);
00017         }
00018 
00019         template <typename T>
00020         static HRESULT CreateElevatedInstance(REFCLSID classId,
00021                                T** object, HWND window = 0)
00022         {
00023                 BIND_OPTS3 bindOptions;
00024                 ::ZeroMemory(&bindOptions, sizeof (BIND_OPTS3));
00025                 bindOptions.cbStruct = sizeof (BIND_OPTS3);
00026                 bindOptions.hwnd = window;
00027                 bindOptions.dwClassContext = CLSCTX_LOCAL_SERVER;
00028 
00029                 WCHAR wszMonikerName[300];
00030                 WCHAR wszCLSID[50];
00031 
00032                 #define cntof(a) (sizeof(a)/sizeof(a[0]))
00033 
00034                 ::StringFromGUID2(classId, wszCLSID,
00035                                                                          cntof(wszCLSID));
00036 
00037                 HRESULT hr = ::StringCchPrintfW(wszMonikerName, cntof(wszMonikerName), L"Elevation:Administrator!new:%s", wszCLSID);
00038         
00039                 if (FAILED(hr))
00040                 {
00041                         return hr;
00042                 }
00043 
00044                 return ::CoGetObject(wszMonikerName,
00045                                                          &bindOptions,
00046                                                          __uuidof(T),
00047                                                          reinterpret_cast<void**>(object));
00048         }
00049 
00050         static void SetShieldIcon(const CButton& button, bool on=true);
00051 };