检查当前用户是否具有管理员权限

//最后修改日期 2009-10-26

#include <lm.h> #pragma comment (lib,"Netapi32.lib") bool Is_Admin_Privilege() { bool privilege = true; OSVERSIONINFO OSV = {sizeof(OSVERSIONINFO),0}; if(GetVersionEx(&OSV)) { if(OSV.dwPlatformId == VER_PLATFORM_WIN32_NT) //NT 2000 XP { privilege = false; HMODULE hModule = NULL; if(hModule = LoadLibrary( _T("netapi32.dll") )) { NET_API_STATUS (NET_API_FUNCTION * pNet_Api_Fun) (LPCWSTR,LPCWSTR,DWORD,LPBYTE *) = (NET_API_STATUS(NET_API_FUNCTION*)(LPCWSTR,LPCWSTR,DWORD,LPBYTE *))GetProcAddress(hModule,"NetUserGetInfo"); if(pNet_Api_Fun) { WCHAR WuserName[UNLEN + 1] = {0}; DWORD nSize = UNLEN; if(GetUserNameW(WuserName,&nSize)) { LPUSER_INFO_1 pUserInfo = NULL; NET_API_STATUS nStatus = pNet_Api_Fun(NULL,WuserName,1,(LPBYTE*)&pUserInfo); if(nStatus == NERR_Success) { if(USER_PRIV_ADMIN == (pUserInfo->usri1_priv & USER_PRIV_MASK)) { privilege = true; }// } if(pUserInfo != NULL) { NET_API_STATUS (NET_API_FUNCTION *pApi_Fun1) (LPVOID) = ((NET_API_STATUS (NET_API_FUNCTION *)(LPVOID) ) GetProcAddress(hModule,"NetApiBufferFree")); if(pApi_Fun1) { pApi_Fun1(pUserInfo); } } } else { DWORD err = GetLastError(); } } else { DWORD err = GetLastError(); } FreeLibrary(hModule); } else { DWORD err = GetLastError(); } if(!privilege) { AfxMessageBox(_T("请以管理员方式运行本软件"),MB_OK | MB_ICONSTOP); } } } return(privilege); }

你可能感兴趣的:(api,function,user,XP,null,fun)