Vista&Win7 下几个有用的函数

发布时间:2011-2-11 14:19
分类名称:Private


判断 Vista 系统

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 ); }