public static extern int LsaOpenPolicy(LSA_UNICODE_STRING target, LSA_OBJECT_ATTRIBUTES objectAttributes, int access, out IntPtr handle);
public static extern int LsaOpenPolicy(LSA_UNICODE_STRING target, LSA_OBJECT_ATTRIBUTES objectAttributes, int access, out IntPtr handle);
internal static IntPtr GetMachineDomainSid() { IntPtr intPtr; IntPtr zero = IntPtr.Zero; IntPtr zero1 = IntPtr.Zero; IntPtr intPtr1 = IntPtr.Zero; try { LSA_OBJECT_ATTRIBUTES lSAOBJECTATTRIBUTE = new LSA_OBJECT_ATTRIBUTES(); intPtr1 = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(LSA_OBJECT_ATTRIBUTES))); Marshal.StructureToPtr(lSAOBJECTATTRIBUTE, intPtr1, false); int num = UnsafeNativeMethods.LsaOpenPolicy(IntPtr.Zero, intPtr1, 1, ref zero); if (num == 0) { num = UnsafeNativeMethods.LsaQueryInformationPolicy(zero, 5, ref zero1); if (num == 0) { POLICY_ACCOUNT_DOMAIN_INFO structure = (POLICY_ACCOUNT_DOMAIN_INFO)Marshal.PtrToStructure(zero1, typeof(POLICY_ACCOUNT_DOMAIN_INFO)); int lengthSid = UnsafeNativeMethods.GetLengthSid(structure.domainSid); IntPtr intPtr2 = Marshal.AllocHGlobal(lengthSid); bool flag = UnsafeNativeMethods.CopySid(lengthSid, intPtr2, structure.domainSid); if (flag) { intPtr = intPtr2; } else { int lastWin32Error = Marshal.GetLastWin32Error(); object[] objArray = new object[1]; objArray[0] = lastWin32Error; throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Res.GetString("UnableToRetrievePolicy"), objArray)); } } else { object[] winError = new object[1]; winError[0] = NativeMethods.LsaNtStatusToWinError(num); throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Res.GetString("UnableToRetrievePolicy"), winError)); } } else { object[] winError1 = new object[1]; winError1[0] = NativeMethods.LsaNtStatusToWinError(num); throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Res.GetString("UnableToRetrievePolicy"), winError1)); } } finally { if (zero != IntPtr.Zero) { UnsafeNativeMethods.LsaClose(zero); } if (zero1 != IntPtr.Zero) { UnsafeNativeMethods.LsaFreeMemory(zero1); } if (intPtr1 != IntPtr.Zero) { Marshal.FreeHGlobal(intPtr1); } } return intPtr; }
internal static IntPtr GetPolicyHandle(string serverName) { IntPtr intPtr; IntPtr intPtr1 = (IntPtr)0; LSA_OBJECT_ATTRIBUTES lSAOBJECTATTRIBUTE = new LSA_OBJECT_ATTRIBUTES(); int pOLICYVIEWLOCALINFORMATION = Utils.POLICY_VIEW_LOCAL_INFORMATION; LSA_UNICODE_STRING lSAUNICODESTRING = new LSA_UNICODE_STRING(); IntPtr hGlobalUni = Marshal.StringToHGlobalUni(serverName); UnsafeNativeMethods.RtlInitUnicodeString(lSAUNICODESTRING, hGlobalUni); try { int num = UnsafeNativeMethods.LsaOpenPolicy(lSAUNICODESTRING, lSAOBJECTATTRIBUTE, pOLICYVIEWLOCALINFORMATION, out intPtr1); if (num == 0) { intPtr = intPtr1; } else { throw ExceptionHelper.GetExceptionFromErrorCode(UnsafeNativeMethods.LsaNtStatusToWinError(num), serverName); } } finally { if (hGlobalUni != (IntPtr)0) { Marshal.FreeHGlobal(hGlobalUni); } } return intPtr; }
internal static IntPtr GetPolicyHandle(string serverName) { IntPtr ptr3; IntPtr zero = IntPtr.Zero; LSA_OBJECT_ATTRIBUTES objectAttributes = new LSA_OBJECT_ATTRIBUTES(); IntPtr s = IntPtr.Zero; int access = POLICY_VIEW_LOCAL_INFORMATION; LSA_UNICODE_STRING result = new LSA_UNICODE_STRING(); s = Marshal.StringToHGlobalUni(serverName); System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.RtlInitUnicodeString(result, s); try { int status = System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.LsaOpenPolicy(result, objectAttributes, access, out zero); if (status != 0) { throw System.DirectoryServices.ActiveDirectory.ExceptionHelper.GetExceptionFromErrorCode(System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.LsaNtStatusToWinError(status), serverName); } ptr3 = zero; } finally { if (s != IntPtr.Zero) { Marshal.FreeHGlobal(s); } } return ptr3; }
internal static IntPtr GetMachineDomainSid() { IntPtr pPolicyHandle = IntPtr.Zero; IntPtr pBuffer = IntPtr.Zero; IntPtr pOA = IntPtr.Zero; try { LSA_OBJECT_ATTRIBUTES oa = new LSA_OBJECT_ATTRIBUTES(); pOA = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(LSA_OBJECT_ATTRIBUTES))); Marshal.StructureToPtr(oa, pOA, false); int err = UnsafeNativeMethods.LsaOpenPolicy( IntPtr.Zero, pOA, 1, // POLICY_VIEW_LOCAL_INFORMATION ref pPolicyHandle); if (err != 0) { throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, Res.GetString(Res.UnableToRetrievePolicy), NativeMethods.LsaNtStatusToWinError(err))); } Debug.Assert(pPolicyHandle != IntPtr.Zero); err = UnsafeNativeMethods.LsaQueryInformationPolicy( pPolicyHandle, 5, // PolicyAccountDomainInformation ref pBuffer); if (err != 0) { throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, Res.GetString(Res.UnableToRetrievePolicy), NativeMethods.LsaNtStatusToWinError(err))); } Debug.Assert(pBuffer != IntPtr.Zero); POLICY_ACCOUNT_DOMAIN_INFO info = (POLICY_ACCOUNT_DOMAIN_INFO) Marshal.PtrToStructure(pBuffer, typeof(POLICY_ACCOUNT_DOMAIN_INFO)); Debug.Assert(UnsafeNativeMethods.IsValidSid(info.domainSid)); // Now we make a copy of the SID to return int sidLength = UnsafeNativeMethods.GetLengthSid(info.domainSid); IntPtr pCopyOfSid = Marshal.AllocHGlobal(sidLength); bool success = UnsafeNativeMethods.CopySid(sidLength, pCopyOfSid, info.domainSid); if (!success) { int lastError = Marshal.GetLastWin32Error(); throw new InvalidOperationException( String.Format(CultureInfo.CurrentCulture, Res.GetString(Res.UnableToRetrievePolicy), lastError)); } return pCopyOfSid; } finally { if (pPolicyHandle != IntPtr.Zero) UnsafeNativeMethods.LsaClose(pPolicyHandle); if (pBuffer != IntPtr.Zero) UnsafeNativeMethods.LsaFreeMemory(pBuffer); if (pOA != IntPtr.Zero) Marshal.FreeHGlobal(pOA); } }
internal static IntPtr GetPolicyHandle(string serverName) { IntPtr handle = (IntPtr)0; LSA_UNICODE_STRING systemName; LSA_OBJECT_ATTRIBUTES objectAttribute = new LSA_OBJECT_ATTRIBUTES(); IntPtr target = (IntPtr)0; int mask = s_POLICY_VIEW_LOCAL_INFORMATION; systemName = new LSA_UNICODE_STRING(); target = Marshal.StringToHGlobalUni(serverName); UnsafeNativeMethods.RtlInitUnicodeString(systemName, target); try { int result = UnsafeNativeMethods.LsaOpenPolicy(systemName, objectAttribute, mask, out handle); if (result != 0) { throw ExceptionHelper.GetExceptionFromErrorCode(UnsafeNativeMethods.LsaNtStatusToWinError(result), serverName); } return handle; } finally { if (target != (IntPtr)0) Marshal.FreeHGlobal(target); } }