Exemple #1
0
        /// <summary>
        /// This method is called on contact status change.
        /// </summary>
        /// <param name="wParam">Handle of chagned contact.</param>
        /// <param name="lParam">
        /// Pointer to DBContactWriteSetting structure.
        /// </param>
        /// <returns>Always zero.</returns>
        private int ContactSettingChanged(IntPtr wParam, IntPtr lParam)
        {
            IntPtr changedHContact = wParam;
            if (changedHContact == hContact)
            {
                IntPtr pDBContactWriteSetting = lParam;
                var setting = new DBContactWriteSetting();
                Marshal.PtrToStructure(pDBContactWriteSetting, setting);

                // TODO: Check this setting name
                if (setting.szSetting == "Status" &&
                    setting.szModule == Protocol)
                {
                    var newStatus = (Status)setting.value.Value.wVal;
                    if (StatusChanged != null)
                        StatusChanged(this, newStatus);
                }
            }
            return 0;
        }
        /// <summary>
        /// Writes setting to database.
        /// </summary>
        /// <param name="hContact">
        /// Handle of contact whose setting must be gotten.
        /// </param>
        /// <param name="moduleName">
        /// Name of module that wrote the setting to get.
        /// </param>
        /// <param name="settingName">
        /// Name of setting to get.
        /// <param name="value">
        /// Value of one of specified types: byte, ushort, short, uint, int,
        /// string, byte[].
        /// </param>
        private void SetSetting(IntPtr hContact, string moduleName,
            string settingName, object value)
        {
            using (var pDBContactWriteSetting =
                new AutoPtr(Marshal.AllocHGlobal(
                    Marshal.SizeOf(typeof (DBContactWriteSetting)))))
            {
                var writeSetting = new DBContactWriteSetting();
                writeSetting.szModule = moduleName;
                writeSetting.szSetting = settingName;

                var variant = new DBVariant();

                // This pointer will be set if we allocate unmanaged memory to
                // store the value:
                AutoPtr valuePtr = null;
                if (value is byte)
                {
                    variant.type = DBVariant.DBVT_BYTE;
                    variant.Value.bVal = (byte)value;
                }
                else if (value is ushort)
                {
                    variant.type = DBVariant.DBVT_WORD;
                    variant.Value.wVal = (ushort)value;
                }
                else if (value is short)
                {
                    variant.type = DBVariant.DBVT_WORD;
                    variant.Value.sVal = (short)value;
                }
                else if (value is uint)
                {
                    variant.type = DBVariant.DBVT_DWORD;
                    variant.Value.dVal = (uint)value;
                }
                else if (value is int)
                {
                    variant.type = DBVariant.DBVT_DWORD;
                    variant.Value.lVal = (int)value;
                }
                else if (value is string)
                {
                    variant.type = DBVariant.DBVT_WCHAR;
                    valuePtr = new AutoPtr(
                        Marshal.StringToHGlobalUni(value as string));
                    variant.Value.pszVal = valuePtr;
                }
                else if (value is byte[])
                {
                    var blob = value as byte[];
                    valuePtr = new AutoPtr(Marshal.AllocHGlobal(blob.Length));
                    for (int i = 0; i < blob.Length; ++i)
                        Marshal.WriteByte(valuePtr, i, blob[i]);

                    variant.Value.ByteArrayValue.cpbVal = (ushort)blob.Length;
                    variant.Value.ByteArrayValue.pbVal = valuePtr;
                }
                else
                    throw new ArgumentException("Type of argument not " +
                                                "supported.", "value");

                writeSetting.value = variant;

                Marshal.StructureToPtr(writeSetting, pDBContactWriteSetting,
                    false);

                IntPtr result = Plugin.m_CallService(
                    "DB/Contact/WriteSetting",
                    hContact,
                    pDBContactWriteSetting);

                // Free allocated memory:
                if (valuePtr != null)
                    valuePtr.Dispose();

                if (result != IntPtr.Zero)
                    throw new DatabaseException();
            }
        }