如何添加用户权限?
1. Opening a Policy Object Handle.
#include <windows.h> #include <ntsecapi.h> void AddPrivileges(PSID AccountSID, LSA_HANDLE PolicyHandle) { LSA_UNICODE_STRING lucPrivilege; NTSTATUS ntsResult; // Create an LSA_UNICODE_STRING for the privilege names. if (!InitLsaString(&lucPrivilege, L"SeServiceLogonRight")) { wprintf(L"Failed InitLsaString\n"); return; } ntsResult = LsaAddAccountRights( PolicyHandle, // An open policy handle. AccountSID, // The target SID. &lucPrivilege, // The privileges. 1 // Number of privileges. ); if (ntsResult == STATUS_SUCCESS) { wprintf(L"Privilege added.\n"); } else { wprintf(L"Privilege was not added - %lu \n", LsaNtStatusToWinError(ntsResult)); } }
#include <windows.h> bool InitLsaString( PLSA_UNICODE_STRING pLsaString, LPCWSTR pwszString ) { DWORD dwLen = 0; if (NULL == pLsaString) return FALSE; if (NULL != pwszString) { dwLen = wcslen(pwszString); if (dwLen > 0x7ffe) // String is too large return FALSE; } // Store the string. pLsaString->Buffer = (WCHAR *)pwszString; pLsaString->Length = (USHORT)dwLen * sizeof(WCHAR); pLsaString->MaximumLength= (USHORT)(dwLen+1) * sizeof(WCHAR); return TRUE; }