/// <summary> /// The NspiBind method initiates a session between a client and the server. /// </summary> /// <param name="flags">A DWORD value that contains a set of bit flags.</param> /// <param name="stat">A STAT block that describes a logical position in a specific address book container.</param> /// <param name="serverGuid">The value NULL or a pointer to a GUID value that is associated with the specific server.</param> /// <returns>Status of NSPI method.</returns> public ErrorCodeValue Bind(uint flags, STAT stat, ref FlatUID_r? serverGuid) { ErrorCodeValue result; BindRequestBody bindRequestBody = this.BuildBindRequestBody(stat, flags); byte[] rawBuffer = null; ChunkedResponse chunkedResponse = null; BindResponseBody bindResponseBody = null; // Send the execute HTTP request and get the response HttpWebResponse response = MapiHttpAdapter.SendMAPIHttpRequest(this.site, this.addressBookUrl, this.userName, this.domainName, this.password, bindRequestBody, RequestType.Bind.ToString(), AdapterHelper.SessionContextCookies); // Read the HTTP response buffer and parse the response to correct format rawBuffer = MapiHttpAdapter.ReadHttpResponse(response); result = (ErrorCodeValue)int.Parse(response.Headers["X-ResponseCode"]); if (result == ErrorCodeValue.Success) { chunkedResponse = ChunkedResponse.ParseChunkedResponse(rawBuffer); bindResponseBody = BindResponseBody.Parse(chunkedResponse.ResponseBodyRawData); result = (ErrorCodeValue)bindResponseBody.ErrorCode; if (bindResponseBody.ServerGuid != null) { FlatUID_r newGuid = new FlatUID_r(); newGuid.Ab = bindResponseBody.ServerGuid.ToByteArray(); serverGuid = newGuid; } else { serverGuid = null; } } response.GetResponseStream().Close(); AdapterHelper.SessionContextCookies = response.Cookies; return result; }
/// <summary> /// Allocate memory for GUIDs. /// </summary> /// <param name="fuid_r">Instance of GUIDs.</param> /// <returns>A pointer points to memory allocated.</returns> public static IntPtr AllocFlatUID_r(FlatUID_r fuid_r) { IntPtr ptr = Marshal.AllocHGlobal(16); for (int i = 0; i < 16; i++) { Marshal.WriteByte(ptr, i, fuid_r.Ab[i]); } return(ptr); }
/// <summary> /// Compare two FlatUID_r structures whether equal or not. /// </summary> /// <param name="flatUid">The second FlatUID_r structure.</param> /// <returns>True: the two FlatUID_r structure equal; Otherwise, false.</returns> public bool Compare(FlatUID_r flatUid) { for (int i = 0; i < Constants.FlatUIDByteSize; i++) { if (this.Ab[i] != flatUid.Ab[i]) { return(false); } } return(true); }
/// <summary> /// Compare two FlatUID_r structures whether equal or not. /// </summary> /// <param name="flatUid">The second FlatUID_r structure.</param> /// <returns>True: the two FlatUID_r structure equal; Otherwise, false.</returns> public bool Compare(FlatUID_r flatUid) { for (int i = 0; i < Constants.FlatUIDByteSize; i++) { if (this.Ab[i] != flatUid.Ab[i]) { return false; } } return true; }
/// <summary> /// Parse GUIDs. /// </summary> /// <param name="ptr">A pointer points to memory allocated.</param> /// <returns>Instance of GUIDs.</returns> public static FlatUID_r ParseFlatUID_r(IntPtr ptr) { FlatUID_r fuid_r = new FlatUID_r(); const int UIDLengthInBytes = 16; fuid_r.Ab = new byte[UIDLengthInBytes]; for (int i = 0; i < UIDLengthInBytes; i++) { fuid_r.Ab[i] = Marshal.ReadByte(ptr, i); } return(fuid_r); }
/// <summary> /// The NspiBind method initiates a session between a client and the server. /// </summary> /// <param name="flags">A DWORD value that contains a set of bit flags.</param> /// <param name="stat">A STAT block that describes a logical position in a specific address book container.</param> /// <param name="serverGuid">The value NULL or a pointer to a GUID value that is associated with the specific server.</param> /// <returns>Status of NSPI method.</returns> public ErrorCodeValue Bind(uint flags, STAT stat, ref FlatUID_r?serverGuid) { ErrorCodeValue result; BindRequestBody bindRequestBody = this.BuildBindRequestBody(stat, flags); byte[] rawBuffer = null; ChunkedResponse chunkedResponse = null; BindResponseBody bindResponseBody = null; // Send the execute HTTP request and get the response HttpWebResponse response = MapiHttpAdapter.SendMAPIHttpRequest(this.site, this.addressBookUrl, this.userName, this.domainName, this.password, bindRequestBody, RequestType.Bind.ToString(), AdapterHelper.SessionContextCookies); // Read the HTTP response buffer and parse the response to correct format rawBuffer = MapiHttpAdapter.ReadHttpResponse(response); result = (ErrorCodeValue)int.Parse(response.Headers["X-ResponseCode"]); if (result == ErrorCodeValue.Success) { chunkedResponse = ChunkedResponse.ParseChunkedResponse(rawBuffer); bindResponseBody = BindResponseBody.Parse(chunkedResponse.ResponseBodyRawData); result = (ErrorCodeValue)bindResponseBody.ErrorCode; if (bindResponseBody.ServerGuid != null) { FlatUID_r newGuid = new FlatUID_r(); newGuid.Ab = bindResponseBody.ServerGuid.ToByteArray(); serverGuid = newGuid; } else { serverGuid = null; } } response.GetResponseStream().Close(); AdapterHelper.SessionContextCookies = response.Cookies; return(result); }
/// <summary> /// Allocate memory for GUIDs. /// </summary> /// <param name="fuid_r">Instance of GUIDs.</param> /// <returns>A pointer points to the allocated memory.</returns> public static IntPtr AllocFlatUID_r(FlatUID_r fuid_r) { IntPtr ptr = Marshal.AllocHGlobal(Constants.FlatUIDByteSize); for (int i = 0; i < Constants.FlatUIDByteSize; i++) { Marshal.WriteByte(ptr, i, fuid_r.Ab[i]); } return ptr; }
/// <summary> /// Parse GUIDs. /// </summary> /// <param name="ptr">A pointer points to the allocated memory.</param> /// <returns>Instance of GUIDs.</returns> public static FlatUID_r ParseFlatUID_r(IntPtr ptr) { FlatUID_r fuid_r = new FlatUID_r { Ab = new byte[Constants.FlatUIDByteSize] }; for (int i = 0; i < Constants.FlatUIDByteSize; i++) { fuid_r.Ab[i] = Marshal.ReadByte(ptr, i); } return fuid_r; }
/// <summary> /// The NspiBind method initiates a session between a client and the server. /// </summary> /// <param name="flags">A DWORD value that contains a set of bit flags.</param> /// <param name="stat">A pointer to a STAT block that describes a logical position in a specific address book container.</param> /// <param name="serverGuid">The value NULL or a pointer to a GUID value that is associated with the specific server.</param> /// <param name="needRetry">A Boolean value indicates if need to retry to get an expected result. This parameter is designed to avoid meaningless retry when an error response is expected.</param> /// <returns>Status of NSPI method.</returns> public ErrorCodeValue NspiBind(uint flags, STAT stat, ref FlatUID_r? serverGuid, bool needRetry = true) { int result; IntPtr ptrServerGuid = IntPtr.Zero; IntPtr ptrStat = AdapterHelper.AllocStat(stat); if (serverGuid.HasValue && serverGuid.Value.Ab != null) { ptrServerGuid = AdapterHelper.AllocFlatUID_r(serverGuid.Value); } int retryCount = 0; do { try { result = OxnspiInterop.NspiBind(this.rpcBinding, flags, ref stat, ptrServerGuid, ref this.contextHandle); } catch (SEHException e) { result = (int)NativeMethods.RpcExceptionCode(e); this.site.Log.Add(LogEntryKind.Comment, "RPC component throws exception, the error code is {0}, the error message is: {1}", result, new Win32Exception(result).ToString()); } if ((ErrorCodeValue)result == ErrorCodeValue.GeneralFailure && needRetry) { Thread.Sleep(this.waitTime); } else { break; } retryCount++; } while ((ErrorCodeValue)result == ErrorCodeValue.GeneralFailure && retryCount < this.maxRetryCount); if (!Enum.IsDefined(typeof(ErrorCodeValue), (uint)result)) { throw new ArgumentException(string.Format("An unknown error is returned, the error code is: {0} and the error message is: {1}", result, new Win32Exception(result).ToString())); } // Parse ServerGuid from ptrServerGuid. if (ptrServerGuid == IntPtr.Zero) { serverGuid = null; } else { serverGuid = AdapterHelper.ParseFlatUID_r(ptrServerGuid); } // Free allocated memory for serverGuid. if (serverGuid.HasValue) { if (serverGuid.Value.Ab != null) { Marshal.FreeHGlobal(ptrServerGuid); } } // Free allocated memory for stat. Marshal.FreeHGlobal(ptrStat); return (ErrorCodeValue)result; }
/// <summary> /// Get properties from NSPI table. /// </summary> /// <param name="server">Server address.</param> /// <param name="userName">The value of user name.</param> /// <param name="domain">The value of Domain.</param> /// <param name="password">Password of the user.</param> /// <param name="columns">PropertyTags to be query.</param> /// <returns>Results in PropertyRowSet format.</returns> public PropertyRowSet_r? GetRecipientInfo(string server, string userName, string domain, string password, PropertyTagArray_r? columns) { #region Call NspiBind method to initiate a session between the client and the server. uint flags = 0; STAT stat = new STAT(); stat.CodePage = 0x4e4; // Set a valid code page. stat.TemplateLocale = 0x409; // Set a valid LCID. stat.SortLocale = 0x409; // Set a valid LCID. // Set value for serverGuid FlatUID_r guid = new FlatUID_r { Ab = new byte[16] }; FlatUID_r? serverGuid = guid; ErrorCodeValue result = this.nspiAdapter.NspiBind(flags, stat, ref serverGuid); Site.Assert.AreEqual<ErrorCodeValue>(ErrorCodeValue.Success, result, "NspiBind should return Success!"); #endregion #region Call NspiQueryRows method to get the recipient information. stat.ContainerID = 0; // Set the container id to the id of default global address book container uint tableCount = 0; uint[] table = null; uint requestCount = 5000; PropertyRowSet_r? propertyRowSet = null; result = this.nspiAdapter.NspiQueryRows(flags, ref stat, tableCount, table, requestCount, columns, out propertyRowSet); Site.Assert.AreEqual<ErrorCodeValue>(ErrorCodeValue.Success, result, "NspiQueryRows should return Success!"); #endregion uint returnValue = this.nspiAdapter.NspiUnbind(0); Site.Assert.AreEqual<uint>(1, returnValue, "NspiUnbind method should return 1 (Success)."); return propertyRowSet; }