// Consider, V2, jruiz: implement this. How do you set the working directory // for a service? //public string InitialWorkingDirectory { // get { // } // set { // } //} /// <include file='doc\ServiceProcessInstaller.uex' path='docs/doc[@for="ServiceProcessInstaller.AccountHasRight"]/*' /> /// <devdoc> /// Enumerates through the rights of the given account and checks whether the given right /// is in the list. /// </devdoc> private static bool AccountHasRight(IntPtr policyHandle, byte[] accountSid, string rightName) { IntPtr pRights = (IntPtr)0; int rightsCount = 0; // This function gives us back a pointer to the start of an array of LSA_UNICODE_STRING structs (in pRights). int status = NativeMethods.LsaEnumerateAccountRights(policyHandle, accountSid, out pRights, out rightsCount); if (status == NativeMethods.STATUS_OBJECT_NAME_NOT_FOUND) { // this means that the accountSid has no specific rights return(false); } if (status != 0) { throw new Win32Exception(SafeNativeMethods.LsaNtStatusToWinError(status)); } bool found = false; try { // look through the rights and see if the desired one is present. IntPtr pCurRights = pRights; for (int i = 0; i < rightsCount; i++) { // Get this element in the array they gave us NativeMethods.LSA_UNICODE_STRING_withPointer uStr = new NativeMethods.LSA_UNICODE_STRING_withPointer(); Marshal.PtrToStructure(pCurRights, uStr); // copy the buffer portion to an array & create a string from that char[] rightChars = new char[uStr.length / sizeof(char)]; Marshal.Copy(uStr.pwstr, rightChars, 0, rightChars.Length); string right = new string(rightChars, 0, rightChars.Length); // see if this is the one we're looking for if (string.Compare(right, rightName, StringComparison.Ordinal) == 0) { found = true; break; } // move to the next element in the array pCurRights = (IntPtr)((long)pCurRights + Marshal.SizeOf(typeof(NativeMethods.LSA_UNICODE_STRING))); } } finally { // make sure we free the memory they allocated for us SafeNativeMethods.LsaFreeMemory(pRights); } return(found); }
private static bool AccountHasRight(IntPtr policyHandle, byte[] accountSid, string rightName) { IntPtr intPtr = (IntPtr)0; int num = 0; int num2 = NativeMethods.LsaEnumerateAccountRights(policyHandle, accountSid, out intPtr, out num); switch (num2) { case -1073741772: return(false); default: throw new Win32Exception(SafeNativeMethods.LsaNtStatusToWinError(num2)); case 0: { bool result = false; try { IntPtr intPtr2 = intPtr; for (int i = 0; i < num; i++) { NativeMethods.LSA_UNICODE_STRING_withPointer lSA_UNICODE_STRING_withPointer = new NativeMethods.LSA_UNICODE_STRING_withPointer(); Marshal.PtrToStructure(intPtr2, (object)lSA_UNICODE_STRING_withPointer); char[] array = new char[lSA_UNICODE_STRING_withPointer.length / 2]; Marshal.Copy(lSA_UNICODE_STRING_withPointer.pwstr, array, 0, array.Length); if (string.Compare(new string(array, 0, array.Length), rightName, StringComparison.Ordinal) == 0) { return(true); } intPtr2 = (IntPtr)((long)intPtr2 + Marshal.SizeOf(typeof(NativeMethods.LSA_UNICODE_STRING))); } return(result); } finally { SafeNativeMethods.LsaFreeMemory(intPtr); } } } }