示例#1
0
        public IntPtr AddEvent(ContactInfo associatedContact, object data, string owner, DatabaseEventType type, DatabaseEventProperties flags, DateTime? timestamp, bool throwOnFailure)
        {
            if (associatedContact == null)
                throw new ArgumentNullException("associatedContact");

            if (String.IsNullOrEmpty(owner))
                throw new ArgumentNullException("owner");

            if (data == null)
                throw new ArgumentNullException("data");

            IntPtr pBlob = IntPtr.Zero;
            UnmanagedStructHandle<DBEVENTINFO> nativeStruct = UnmanagedStructHandle<DBEVENTINFO>.Empty;

            try
            {
                int totalBytes;

                if (data is string)
                {
                    totalBytes = DBEVENTINFO.LayoutAnsiUniString((string)data, out pBlob);
                }
                else if (data is byte[])
                {
                    byte[] dataBytes = (byte[])data;
                    totalBytes = dataBytes.Length;

                    pBlob = Marshal.AllocHGlobal(totalBytes);
                    Marshal.Copy(dataBytes, 0, pBlob, dataBytes.Length);
                }
                else
                    throw new ArgumentOutOfRangeException("data");

                DBEVENTINFO info = new DBEVENTINFO(0, IntPtr.Zero);
                info.Module = Translate.ToHandle(owner, StringEncoding.Ansi).IntPtr;
                info.BlobSize = (uint)totalBytes;
                info.BlobPtr = pBlob;
                info.EventType = (ushort)type;
                info.Flags = (uint)flags;
                info.Timestamp = Utilities.GetTimestamp(timestamp.HasValue ? timestamp.Value : DateTime.Now);

                nativeStruct = new UnmanagedStructHandle<DBEVENTINFO>(ref info, pBlob, info.Module);
                IntPtr eventHandle = (IntPtr)MirandaContext.Current.CallService(MS_DB_EVENT_ADD, associatedContact.MirandaHandle, nativeStruct.IntPtr);

                if (eventHandle == IntPtr.Zero && throwOnFailure)
                    throw new MirandaException(String.Format(TextResources.ExceptionMsg_Formatable2_MirandaServiceReturnedFailure, MS_DB_EVENT_ADD, eventHandle.ToString()));
                else
                    return eventHandle;
            }
            finally
            {
                nativeStruct.Free();
            }
        }
示例#2
0
        public IntPtr AddEvent(ContactInfo associatedContact, object data, ISettingOwner owner, DatabaseEventType type, DatabaseEventProperties flags, DateTime? timestamp, bool throwOnFailure)
        {
            if (owner == null)
                throw new ArgumentNullException("owner");

            return AddEvent(associatedContact, data, owner.Name, type, flags, timestamp, throwOnFailure);
        }
示例#3
0
        /// <summary>
        /// Get the event information from a <see cref="DBEVENTINFO"/> struct.
        /// </summary>
        /// <param name="dbEventInfo">[REF] <see cref="DBEVENTINFO"/> struct.</param>
        /// <param name="mirandaHandle">Event handle (the blob buffer will be populated if not null).</param>
        /// <param name="blobBuffer">Buffer to use for blob marshaling.</param>
        /// <param name="type">[OUT] Event type.</param>
        /// <param name="flags">[OUT] Event flags.</param>
        /// <param name="data">[OUT] Event data.</param>
        /// <param name="owningModule">[OUT] Event related module.</param>
        /// <param name="timestamp">[OUT] Event timestamp.</param>
        private static void GetEventInfo(ref DBEVENTINFO dbEventInfo, IntPtr eventHandle, InteropBuffer blobBuffer, out DatabaseEventType type, out DatabaseEventProperties flags, out string data, out Protocol owningModule, out DateTime timestamp)
        {
            MirandaContext context = MirandaContext.Current;

            unsafe
            {
                // If the event handle is set, we probably want to populate the blob buffer...
                if (eventHandle != IntPtr.Zero)
                    PopulateBlobBuffer(ref dbEventInfo, eventHandle);

                type = (DatabaseEventType)dbEventInfo.EventType;
                flags = (DatabaseEventProperties)dbEventInfo.Flags;
                data = GetEventData(ref dbEventInfo);
            }

            owningModule = GetEventModule(ref dbEventInfo);
            GetEventTimestamp(ref dbEventInfo, blobBuffer, out timestamp);
        }
示例#4
0
 public IntPtr AddEvent(ContactInfo associatedContact, object data, ISettingOwner owner, DatabaseEventType type, DatabaseEventProperties flags, DateTime? timestamp)
 {
     return AddEvent(associatedContact, data, owner, type, flags, timestamp, true);
 }
示例#5
0
        /// <summary>
        /// Gets the event information based on its handle.
        /// </summary>
        /// <param name="eventHandle">Event handle.</param>
        /// <param name="type">[OUT] Event type.</param>
        /// <param name="flags">[OUT] Event flags.</param>
        /// <param name="data">[OUT] Event data.</param>
        /// <param name="owningModule">[OUT] Event related module.</param>
        /// <param name="timestamp">[OUT] Event timestamp.</param>
        public static void FromHandle(IntPtr eventHandle, out DatabaseEventType type, out DatabaseEventProperties flags, out string data, out Protocol owningModule, out DateTime timestamp)
        {
            InteropBuffer buffer = null;

            try
            {
                unsafe
                {
                    DBEVENTINFO dbEventInfo;
                    PrepareDbEventInfo(eventHandle, out dbEventInfo, out buffer);

                    GetEventInfo(ref dbEventInfo, eventHandle, buffer, out type, out flags, out data, out owningModule, out timestamp);
                }
            }
            catch (MirandaException)
            {
                throw;
            }
            catch (Exception e)
            {
                throw new InvalidOperationException(TextResources.ExceptionMsg_CannotFinishMarshaling, e);
            }
            finally
            {
                if (buffer != null)
                {
                    buffer.Unlock();
                    InteropBufferPool.ReleaseBuffer(buffer);
                }
            }
        }