internal void SyncReplicaHelper(IntPtr dsHandle, bool isADAM, string partition, string sourceServer, int option, LoadLibrarySafeHandle libHandle) { int structSize = Marshal.SizeOf(typeof(Guid)); IntPtr unmanagedGuid = (IntPtr)0; Guid guid = Guid.Empty; AdamInstance adamServer = null; DomainController dcServer = null; unmanagedGuid = Marshal.AllocHGlobal(structSize); try { if (sourceServer != null) { DirectoryContext newContext = Utils.GetNewDirectoryContext(sourceServer, DirectoryContextType.DirectoryServer, context); if (isADAM) { adamServer = AdamInstance.GetAdamInstance(newContext); guid = adamServer.NtdsaObjectGuid; } else { dcServer = DomainController.GetDomainController(newContext); guid = dcServer.NtdsaObjectGuid; } Marshal.StructureToPtr(guid, unmanagedGuid, false); } // call DsReplicaSyncW IntPtr functionPtr = UnsafeNativeMethods.GetProcAddress(libHandle, "DsReplicaSyncW"); if (functionPtr == (IntPtr)0) { throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error()); } UnsafeNativeMethods.DsReplicaSyncW dsReplicaSyncW = (UnsafeNativeMethods.DsReplicaSyncW)Marshal.GetDelegateForFunctionPointer(functionPtr, typeof(UnsafeNativeMethods.DsReplicaSyncW)); int result = dsReplicaSyncW(dsHandle, partition, unmanagedGuid, (int)option); // check the result if (result != 0) { if (!Partitions.Contains(partition)) { throw new ArgumentException(SR.ServerNotAReplica, "partition"); } string serverDownName = null; // this is the error returned when the server that we want to sync from is down if (result == ExceptionHelper.RPC_S_SERVER_UNAVAILABLE) { serverDownName = sourceServer; } // this is the error returned when the server that we want to get synced is down else if (result == ExceptionHelper.RPC_S_CALL_FAILED) { serverDownName = Name; } throw ExceptionHelper.GetExceptionFromErrorCode(result, serverDownName); } } finally { if (unmanagedGuid != (IntPtr)0) { Marshal.FreeHGlobal(unmanagedGuid); } if (adamServer != null) { adamServer.Dispose(); } if (dcServer != null) { dcServer.Dispose(); } } }
internal void SyncReplicaHelper(IntPtr dsHandle, bool isADAM, string partition, string sourceServer, int option, LoadLibrarySafeHandle libHandle) { Guid ntdsaObjectGuid; int num = Marshal.SizeOf(typeof(Guid)); AdamInstance adamInstance = null; DomainController domainController = null; IntPtr intPtr = Marshal.AllocHGlobal(num); try { if (sourceServer != null) { DirectoryContext newDirectoryContext = Utils.GetNewDirectoryContext(sourceServer, DirectoryContextType.DirectoryServer, this.context); if (!isADAM) { domainController = DomainController.GetDomainController(newDirectoryContext); ntdsaObjectGuid = domainController.NtdsaObjectGuid; } else { adamInstance = AdamInstance.GetAdamInstance(newDirectoryContext); ntdsaObjectGuid = adamInstance.NtdsaObjectGuid; } Marshal.StructureToPtr(ntdsaObjectGuid, intPtr, false); } IntPtr procAddress = UnsafeNativeMethods.GetProcAddress(libHandle, "DsReplicaSyncW"); if (procAddress != (IntPtr)0) { UnsafeNativeMethods.DsReplicaSyncW delegateForFunctionPointer = (UnsafeNativeMethods.DsReplicaSyncW)Marshal.GetDelegateForFunctionPointer(procAddress, typeof(UnsafeNativeMethods.DsReplicaSyncW)); int num1 = delegateForFunctionPointer(dsHandle, partition, intPtr, option); if (num1 != 0) { if (this.Partitions.Contains(partition)) { string name = null; if (num1 != ExceptionHelper.RPC_S_SERVER_UNAVAILABLE) { if (num1 == ExceptionHelper.RPC_S_CALL_FAILED) { name = this.Name; } } else { name = sourceServer; } throw ExceptionHelper.GetExceptionFromErrorCode(num1, name); } else { throw new ArgumentException(Res.GetString("ServerNotAReplica"), "partition"); } } } else { throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error()); } } finally { if (intPtr != (IntPtr)0) { Marshal.FreeHGlobal(intPtr); } if (adamInstance != null) { adamInstance.Dispose(); } if (domainController != null) { domainController.Dispose(); } } }