发布时间:2011-2-11 14:19
分类名称:Private
BOOL IsVista() { OSVERSIONINFO osver; osver.dwOSVersionInfoSize = sizeof( OSVERSIONINFO ); if ( ::GetVersionEx( &osver ) && osver.dwPlatformId == VER_PLATFORM_WIN32_NT && (osver.dwMajorVersion >= 6 ) ) return TRUE; return FALSE; }
#define TokenElevation TokenUser + 19 HRESULT IsElevated(BOOL * pbElevated ) { //ASSERT( IsVista() ); HRESULT hResult = E_FAIL; // assume an error occured HANDLE hToken = NULL; if ( !::OpenProcessToken( ::GetCurrentProcess(), TOKEN_QUERY, &hToken ) ) { //ASSERT( FALSE ); return hResult; } TOKEN_ELEVATION te = { 0 }; DWORD dwReturnLength = 0; if ( !::GetTokenInformation( hToken, (TOKEN_INFORMATION_CLASS)(TokenElevation), &te, sizeof( te ), &dwReturnLength ) ) { //ASSERT( FALSE ); } else { //ASSERT( dwReturnLength == sizeof( te ) ); hResult = te.TokenIsElevated ? S_OK : S_FALSE; if ( pbElevated) *pbElevated = (te.TokenIsElevated != 0); } ::CloseHandle( hToken ); return hResult; }
#define BCM_FIRST 0x1600 //Normal button #define BCM_SETSHIELD (BCM_FIRST + 0x000C) //Elevated button void AddShieldToButton(HWND hDlgWnd,UINT nID) { HWND hBtnWnd=GetDlgItem(hDlgWnd,nID); DWORD dwOldStyle=0; dwOldStyle=GetWindowLong(hBtnWnd,GWL_STYLE); SetWindowLong(hBtnWnd,GWL_STYLE,dwOldStyle|BS_FLAT); ::SendMessage(hBtnWnd,BCM_SETSHIELD, 0, 1); }
////////////////////////////////////////////////////////////////////////// // RunElevated simply calls ShellExecuteEx with the verb "runas" to start the elevated process. // I wish there was a just as easy way to start a non-elevated process, as well. //if user cancel or other errors,return false //else return true BOOL RunElevated( HWND hwnd, LPCTSTR pszPath, LPCTSTR pszParameters = NULL, LPCTSTR pszDirectory = NULL ) { SHELLEXECUTEINFO shex; memset( &shex, 0, sizeof( shex) ); shex.cbSize = sizeof( SHELLEXECUTEINFO ); shex.fMask = 0; shex.hwnd = hwnd; shex.lpVerb = _T("runas"); shex.lpFile = pszPath; shex.lpParameters = pszParameters; shex.lpDirectory = pszDirectory; shex.nShow = SW_NORMAL; return ::ShellExecuteEx( &shex ); }