Mapi.cs
author Thomas
Mon, 03 Sep 2018 14:04:38 +0200
branchOUT-283
changeset 2353 81975e284b07
child 2354 e5b26ee3c384
permissions -rw-r--r--
Add MAPI methods to set properties via p/invoke
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2353
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
     1
´╗┐using System;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
     2
using System.Runtime.InteropServices;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
     3
using Outlook = Microsoft.Office.Interop.Outlook;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
     4
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
     5
namespace pEp
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
     6
{
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
     7
    internal class Mapi
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
     8
    {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
     9
        #region Definitions
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    10
        /// <summary>
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    11
        /// A collection of MAPI error or warning codes.
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    12
        /// </summary>
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    13
        internal static class HResult
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    14
        {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    15
            public const uint S_FALSE                           = 0x00000001;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    16
            public const uint S_OK                              = 0x00000000;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    17
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    18
            public const uint E_NOTIMPL                         = 0x80004001;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    19
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    20
            public const uint MAPI_E_CALL_FAILED                = 0x80004005;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    21
            public const uint MAPI_E_NOT_ENOUGH_MEMORY          = 0x8007000E;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    22
            public const uint MAPI_E_INVALID_PARAMETER          = 0x80000003;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    23
            public const uint MAPI_E_INTERFACE_NOT_SUPPORTED    = 0x80000004;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    24
            public const uint MAPI_E_NO_ACCESS                  = 0x80000009;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    25
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    26
            public const uint E_INVALIDARG                      = 0x80070057;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    27
            public const uint E_OUTOFMEMORY                     = 0x80000002;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    28
            public const uint E_UNEXPECTED                      = 0x8000FFFF;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    29
            public const uint E_FAIL                            = 0x80000008;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    30
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    31
            public const uint MAPI_E_NO_SUPPORT                 = 0x80040000 | 0x102;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    32
            public const uint MAPI_E_BAD_CHARWIDTH              = 0x80040000 | 0x103;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    33
            public const uint MAPI_E_STRING_TOO_LONG            = 0x80040000 | 0x105;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    34
            public const uint MAPI_E_UNKNOWN_FLAGS              = 0x80040000 | 0x106;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    35
            public const uint MAPI_E_INVALID_ENTRYID            = 0x80040000 | 0x107;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    36
            public const uint MAPI_E_INVALID_OBJECT             = 0x80040000 | 0x108;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    37
            public const uint MAPI_E_OBJECT_CHANGED             = 0x80040000 | 0x109;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    38
            public const uint MAPI_E_OBJECT_DELETED             = 0x80040000 | 0x10A;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    39
            public const uint MAPI_E_BUSY                       = 0x80040000 | 0x10B;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    40
            public const uint MAPI_E_NOT_ENOUGH_DISK            = 0x80040000 | 0x10D;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    41
            public const uint MAPI_E_NOT_ENOUGH_RESOURCES       = 0x80040000 | 0x10E;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    42
            public const uint MAPI_E_NOT_FOUND                  = 0x80040000 | 0x10F;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    43
            public const uint MAPI_E_VERSION                    = 0x80040000 | 0x110;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    44
            public const uint MAPI_E_LOGON_FAILED               = 0x80040000 | 0x111;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    45
            public const uint MAPI_E_SESSION_LIMIT              = 0x80040000 | 0x112;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    46
            public const uint MAPI_E_USER_CANCEL                = 0x80040000 | 0x113;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    47
            public const uint MAPI_E_UNABLE_TO_ABORT            = 0x80040000 | 0x114;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    48
            public const uint MAPI_E_NETWORK_ERROR              = 0x80040000 | 0x115;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    49
            public const uint MAPI_E_DISK_ERROR                 = 0x80040000 | 0x116;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    50
            public const uint MAPI_E_TOO_COMPLEX                = 0x80040000 | 0x117;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    51
            public const uint MAPI_E_BAD_COLUMN                 = 0x80040000 | 0x118;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    52
            public const uint MAPI_E_EXTENDED_ERROR             = 0x80040000 | 0x119;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    53
            public const uint MAPI_E_COMPUTED                   = 0x80040000 | 0x11A;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    54
            public const uint MAPI_E_CORRUPT_DATA               = 0x80040000 | 0x11B;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    55
            public const uint MAPI_E_UNCONFIGURED               = 0x80040000 | 0x11C;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    56
            public const uint MAPI_E_FAILONEPROVIDER            = 0x80040000 | 0x11D;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    57
            public const uint MAPI_E_UNKNOWN_CPID               = 0x80040000 | 0x11E;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    58
            public const uint MAPI_E_UNKNOWN_LCID               = 0x80040000 | 0x11F;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    59
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    60
            public const uint MAPI_E_CORRUPT_STORE              = 0x80040000 | 0x600;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    61
            public const uint MAPI_E_NOT_IN_QUEUE               = 0x80040000 | 0x601;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    62
            public const uint MAPI_E_NO_SUPPRESS                = 0x80040000 | 0x602;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    63
            public const uint MAPI_E_COLLISION                  = 0x80040000 | 0x604;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    64
            public const uint MAPI_E_NOT_INITIALIZED            = 0x80040000 | 0x605;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    65
            public const uint MAPI_E_NON_STANDARD               = 0x80040000 | 0x606;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    66
            public const uint MAPI_E_NO_RECIPIENTS              = 0x80040000 | 0x607;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    67
            public const uint MAPI_E_SUBMITTED                  = 0x80040000 | 0x608;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    68
            public const uint MAPI_E_HAS_FOLDERS                = 0x80040000 | 0x609;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    69
            public const uint MAPI_E_HAS_MESSAGES               = 0x80040000 | 0x60A;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    70
            public const uint MAPI_E_FOLDER_CYCLE               = 0x80040000 | 0x60B;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    71
        };
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    72
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    73
        /// <summary>
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    74
        /// Interface IDs used to retrieve the specific MAPI Interfaces from the IUnknown object.
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    75
        /// </summary>
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    76
        internal static class MAPIInterfaceIds
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    77
        {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    78
            public const string IMAPISession        = "00020300-0000-0000-C000-000000000046";
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    79
            public const string IMAPIProp           = "00020303-0000-0000-C000-000000000046";
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    80
            public const string IMAPITable          = "00020301-0000-0000-C000-000000000046";
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    81
            public const string IMAPIMsgStore       = "00020306-0000-0000-C000-000000000046";
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    82
            public const string IMAPIFolder         = "0002030C-0000-0000-C000-000000000046";
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    83
            public const string IMAPISpoolerService = "0002031E-0000-0000-C000-000000000046";
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    84
            public const string IMAPIStatus         = "0002031E-0000-0000-C000-000000000046";
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    85
            public const string IMessage            = "00020307-0000-0000-C000-000000000046";
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    86
            public const string IAddrBook           = "00020309-0000-0000-C000-000000000046";
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    87
            public const string IProfSect           = "00020304-0000-0000-C000-000000000046";
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    88
            public const string IMAPIContainer      = "0002030B-0000-0000-C000-000000000046";
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    89
            public const string IABContainer        = "0002030D-0000-0000-C000-000000000046";
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    90
            public const string IMsgServiceAdmin    = "0002031D-0000-0000-C000-000000000046";
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    91
            public const string IProfAdmin          = "0002031C-0000-0000-C000-000000000046";
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    92
            public const string IMailUser           = "0002030A-0000-0000-C000-000000000046";
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    93
            public const string IDistList           = "0002030E-0000-0000-C000-000000000046";
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    94
            public const string IAttachment         = "00020308-0000-0000-C000-000000000046";
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    95
            public const string IMAPIControl        = "0002031B-0000-0000-C000-000000000046";
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    96
            public const string IMAPILogonRemote    = "00020346-0000-0000-C000-000000000046";
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    97
            public const string IMAPIForm           = "00020327-0000-0000-C000-000000000046";
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    98
        }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
    99
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   100
        /// <summary>
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   101
        /// Save options for the IMAPIProp.SaveChanges method.
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   102
        /// </summary>
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   103
        internal static class SaveOptions
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   104
        {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   105
            public const int KEEP_OPEN_READONLY     = 0x00000001;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   106
            public const int KEEP_OPEN_READWRITE    = 0x00000002;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   107
            public const int FORCE_SAVE             = 0x00000004;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   108
        }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   109
        #endregion
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   110
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   111
        #region Interfaces
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   112
        /// <summary>
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   113
        /// Enables clients, service providers, and MAPI to work with properties. All objects that 
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   114
        /// support properties implement this interface.
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   115
        /// </summary>
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   116
        [
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   117
            ComImport,
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   118
            ComVisible(false),
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   119
            InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   120
            Guid(Mapi.MAPIInterfaceIds.IMAPIProp)
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   121
        ]
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   122
        internal interface IMAPIProp
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   123
        {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   124
            [return: MarshalAs(UnmanagedType.I4)]
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   125
            [PreserveSig]
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   126
            int GetLastError();
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   127
            [return: MarshalAs(UnmanagedType.I4)]
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   128
            [PreserveSig]
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   129
            int SaveChanges(uint uFlags);
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   130
            [return: MarshalAs(UnmanagedType.I4)]
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   131
            [PreserveSig]
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   132
            int GetProps();
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   133
            [return: MarshalAs(UnmanagedType.I4)]
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   134
            [PreserveSig]
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   135
            int GetPropList();
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   136
            [return: MarshalAs(UnmanagedType.I4)]
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   137
            [PreserveSig]
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   138
            int OpenProperty();
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   139
            [return: MarshalAs(UnmanagedType.I4)]
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   140
            [PreserveSig]
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   141
            int SetProps(uint values, IntPtr propArray, IntPtr problems);
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   142
            [return: MarshalAs(UnmanagedType.I4)]
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   143
            [PreserveSig]
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   144
            int DeleteProps();
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   145
            [return: MarshalAs(UnmanagedType.I4)]
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   146
            [PreserveSig]
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   147
            int CopyTo();
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   148
            [return: MarshalAs(UnmanagedType.I4)]
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   149
            [PreserveSig]
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   150
            int CopyProps();
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   151
            [return: MarshalAs(UnmanagedType.I4)]
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   152
            [PreserveSig]
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   153
            int GetNamesFromIDs();
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   154
            [return: MarshalAs(UnmanagedType.I4)]
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   155
            [PreserveSig]
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   156
            int GetIDsFromNames();
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   157
        }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   158
        #endregion
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   159
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   160
        #region Structures
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   161
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   162
        /// <summary>
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   163
        /// The SPropValue structure describes a MAPI property.
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   164
        /// </summary>
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   165
        public struct SPropValue
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   166
        {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   167
            /// <summary>
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   168
            /// Property tag for the property. Property tags are 32-bit unsigned integers consisting of the property's unique identifier in the high-order 16 bits and the property's type in the low-order 16 bits.
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   169
            /// </summary>
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   170
            public uint PropTag;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   171
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   172
            /// <summary>
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   173
            /// Reserved for MAPI; do not use.
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   174
            /// </summary>
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   175
            public uint DwAlignPad;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   176
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   177
            /// <summary>
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   178
            /// Union of data values, the specific value dictated by the property type.
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   179
            /// </summary>
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   180
            public PV Value;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   181
        }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   182
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   183
        /// <summary>
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   184
        /// Union of data values for the SPropValue.value property.
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   185
        /// </summary>
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   186
        [StructLayout(LayoutKind.Explicit, Size = 8)]
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   187
        public struct PV
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   188
        {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   189
            [FieldOffset(0)]
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   190
            public short i;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   191
            [FieldOffset(0)]
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   192
            public int l;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   193
            [FieldOffset(0)]
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   194
            public uint ul;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   195
            [FieldOffset(0)]
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   196
            public float flt;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   197
            [FieldOffset(0)]
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   198
            public double dbl;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   199
            [FieldOffset(0)]
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   200
            public ushort b;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   201
            [FieldOffset(0)]
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   202
            public double at;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   203
            [FieldOffset(0)]
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   204
            public IntPtr lpszA;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   205
            [FieldOffset(0)]
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   206
            public IntPtr lpszW;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   207
            [FieldOffset(0)]
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   208
            public IntPtr lpguid;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   209
            /*[FieldOffset(0)]
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   210
            public IntPtr bin;*/
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   211
            [FieldOffset(0)]
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   212
            public ulong li;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   213
            [FieldOffset(0)]
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   214
            public SRowSet bin;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   215
        }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   216
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   217
        /// <summary>
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   218
        /// Contains an array of SRow structures. Each SRow structure describes a row from a table.
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   219
        /// </summary>
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   220
        [StructLayout(LayoutKind.Sequential)]
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   221
        public struct SRowSet
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   222
        {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   223
            public uint cRows;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   224
            public IntPtr aRow; // pSRow
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   225
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   226
            public byte[] AsBytes
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   227
            {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   228
                get
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   229
                {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   230
                    byte[] b = new byte[this.cRows];
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   231
                    for (int i = 0; i < this.cRows; i++)
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   232
                        b[i] = Marshal.ReadByte(aRow, i);
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   233
                    return b;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   234
                }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   235
            }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   236
        }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   237
        #endregion
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   238
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   239
        #region Methods
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   240
        /// <summary>
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   241
        /// Sets the given MAPI properties on the Outlook mail item.
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   242
        /// Note: the caller has to make sure that the count of MAPI properties
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   243
        /// and respective values matches and that the given values match the
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   244
        /// required data type for each MAPI property.
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   245
        /// </summary>
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   246
        /// <param name="omi">The Outlook mail item to set its properties.</param>
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   247
        /// <param name="mapiProperties">The MAPI properties to set.</param>
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   248
        /// <param name="values">The values to set.</param>
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   249
        /// <returns>True if the method succeeds, otherwise false.</returns>
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   250
        public static bool SetMAPIProperties(Outlook.MailItem omi, MapiProperty.MapiProp[] mapiProperties, object[] values)
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   251
        {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   252
            // The return status of this method
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   253
            bool success = false;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   254
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   255
            // Pointer to IUnknown interface
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   256
            IntPtr IUnknown = IntPtr.Zero;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   257
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   258
            // Pointer to the MAPI properties array
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   259
            IntPtr propArray = IntPtr.Zero;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   260
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   261
            // Pointer to the value
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   262
            IntPtr valuePtr = IntPtr.Zero;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   263
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   264
            // Get MAPI object from mail item
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   265
            object mapiObject = omi?.MAPIOBJECT;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   266
            if (mapiObject == null)
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   267
            {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   268
                Log.Error("SetMAPIProperties: MAPI object is null. Property could not be set.");
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   269
                return success;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   270
            }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   271
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   272
            // Make sure the properties count matches the values count
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   273
            int propCount = mapiProperties.Length;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   274
            if (propCount != values.Length)
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   275
            {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   276
                Log.Error("SetMAPIProperties: Mismatch between tag count and value count.");
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   277
                return success;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   278
            }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   279
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   280
            try
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   281
            {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   282
                // Initialize MAPI
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   283
                Mapi.MAPIInitialize(IntPtr.Zero);
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   284
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   285
                // Get the IUnknown interface from the MAPI object
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   286
                IUnknown = Marshal.GetIUnknownForObject(mapiObject);
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   287
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   288
                // Get the IMAPIProp interface
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   289
                Mapi.IMAPIProp IMAPIProp = (Mapi.IMAPIProp)Marshal.GetTypedObjectForIUnknown(IUnknown, typeof(Mapi.IMAPIProp));
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   290
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   291
                // Create the SPropValue structure
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   292
                Mapi.SPropValue sPropValue = new Mapi.SPropValue();
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   293
                var propValueSize = Marshal.SizeOf(sPropValue);
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   294
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   295
                // Allocate memory for the array of SPropValues to set
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   296
                propArray = Marshal.AllocHGlobal(propValueSize * propCount);
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   297
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   298
                // Create the property values array
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   299
                for (int i = 0; i < propCount; i++)
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   300
                {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   301
                    // Get the current property/value pair
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   302
                    MapiProperty.MapiProp mapiProperty = mapiProperties[i];
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   303
                    uint tag = mapiProperty.Tag;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   304
                    object value = values[i];
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   305
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   306
                    // Set the property value's property tag
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   307
                    sPropValue.PropTag = (uint)tag;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   308
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   309
                    // Set the property value's value
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   310
                    switch (mapiProperty.DataType)
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   311
                    {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   312
                        case MapiProperty.MapiDataType.PtypBoolean:
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   313
                            {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   314
                                try
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   315
                                {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   316
                                    sPropValue.Value.b = (ushort)Convert.ToInt16((bool)value);
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   317
                                }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   318
                                catch (Exception ex)
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   319
                                {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   320
                                    throw new Exception(string.Format("Error converting to Boolean. Property tag: {0}. Value: {1}. Exception: {2}.", mapiProperty.DaslName, value?.ToString(), ex.ToString()));
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   321
                                }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   322
                            }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   323
                            break;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   324
                        case MapiProperty.MapiDataType.PtypString:
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   325
                            {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   326
                                try
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   327
                                {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   328
                                    valuePtr = Marshal.StringToHGlobalUni(value as string);
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   329
                                    sPropValue.Value.lpszW = valuePtr;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   330
                                }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   331
                                catch (Exception ex)
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   332
                                {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   333
                                    throw new Exception(string.Format("Error converting to String. Property tag: {0}. Value: {1}. Exception: {2}.", mapiProperty.DaslName, value?.ToString(), ex.ToString()));
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   334
                                }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   335
                            }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   336
                            break;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   337
                        case MapiProperty.MapiDataType.PtypString8:
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   338
                            {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   339
                                try
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   340
                                {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   341
                                    valuePtr = Marshal.StringToHGlobalAnsi(value as string);
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   342
                                    sPropValue.Value.lpszA = valuePtr;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   343
                                }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   344
                                catch (Exception ex)
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   345
                                {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   346
                                    throw new Exception(string.Format("Error converting to String8. Property tag: {0}. Value: {1}. Exception: {2}.", mapiProperty.DaslName, value?.ToString(), ex.ToString()));
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   347
                                }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   348
                            }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   349
                            break;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   350
                        case MapiProperty.MapiDataType.PtypInteger16:
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   351
                            {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   352
                                try
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   353
                                {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   354
                                    sPropValue.Value.i = (short)value;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   355
                                }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   356
                                catch (Exception ex)
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   357
                                {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   358
                                    throw new Exception(string.Format("Error converting to Short. Property tag: {0}. Value: {1}. Exception: {2}.", mapiProperty.DaslName, value?.ToString(), ex.ToString()));
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   359
                                }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   360
                            }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   361
                            break;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   362
                        case MapiProperty.MapiDataType.PtypInteger32:
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   363
                            {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   364
                                try
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   365
                                {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   366
                                    sPropValue.Value.l = (int)value;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   367
                                }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   368
                                catch (Exception ex)
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   369
                                {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   370
                                    throw new Exception(string.Format("Error converting to Integer. Property tag: {0}. Value: {1}. Exception: {2}.", mapiProperty.DaslName, value?.ToString(), ex.ToString()));
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   371
                                }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   372
                            }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   373
                            break;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   374
                        case MapiProperty.MapiDataType.PtypFloating32:
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   375
                            {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   376
                                try
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   377
                                {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   378
                                    sPropValue.Value.flt = (float)value;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   379
                                }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   380
                                catch (Exception ex)
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   381
                                {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   382
                                    throw new Exception(string.Format("Error converting to Float. Property tag: {0}. Value: {1}. Exception: {2}.", mapiProperty.DaslName, value?.ToString(), ex.ToString()));
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   383
                                }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   384
                            }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   385
                            break;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   386
                        case MapiProperty.MapiDataType.PtypFloating64:
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   387
                        case MapiProperty.MapiDataType.PtypFloatingTime:
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   388
                            {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   389
                                try
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   390
                                {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   391
                                    sPropValue.Value.at = (double)value;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   392
                                }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   393
                                catch (Exception ex)
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   394
                                {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   395
                                    throw new Exception(string.Format("Error converting to Double. Property tag: {0}. Value: {1}. Exception: {2}.", mapiProperty.DaslName, value?.ToString(), ex.ToString()));
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   396
                                }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   397
                            }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   398
                            break;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   399
                        case MapiProperty.MapiDataType.PtypInteger64:
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   400
                            {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   401
                                try
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   402
                                {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   403
                                    sPropValue.Value.li = (ulong)value;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   404
                                }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   405
                                catch (Exception ex)
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   406
                                {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   407
                                    throw new Exception(string.Format("Error converting to Ulong. Property tag: {0}. Value: {1}. Exception: {2}.", mapiProperty.DaslName, value?.ToString(), ex.ToString()));
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   408
                                }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   409
                            }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   410
                            break;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   411
                        case MapiProperty.MapiDataType.PtypGuid:
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   412
                            {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   413
                                try
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   414
                                {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   415
                                    IntPtr guidPtr = Marshal.AllocHGlobal(Marshal.SizeOf(value));
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   416
                                    Marshal.StructureToPtr((Guid)value, guidPtr, false);
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   417
                                    sPropValue.Value.lpguid = guidPtr;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   418
                                }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   419
                                catch (Exception ex)
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   420
                                {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   421
                                    throw new Exception(string.Format("Error converting to Guid. Property tag: {0}. Value: {1}. Exception: {2}.", mapiProperty.DaslName, value?.ToString(), ex.ToString()));
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   422
                                }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   423
                            }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   424
                            break;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   425
                        case MapiProperty.MapiDataType.PtypBinary:
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   426
                            {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   427
                                try
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   428
                                {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   429
                                    sPropValue.Value.bin = (Mapi.SRowSet)value;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   430
                                }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   431
                                catch (Exception ex)
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   432
                                {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   433
                                    throw new Exception(string.Format("Error converting to Binary. Property tag: {0}. Value: {1}. Exception: {2}.", mapiProperty.DaslName, value?.ToString(), ex.ToString()));
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   434
                                }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   435
                            }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   436
                            break;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   437
                        default:
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   438
                            {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   439
                                throw new Exception(string.Format("Error creating SPropValue. Data type {0} not supported.", Enum.GetName(typeof(MapiProperty.MapiDataType), mapiProperty.DataType)));
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   440
                            }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   441
                    }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   442
                    Marshal.StructureToPtr(sPropValue, (IntPtr)((uint)propArray + (propValueSize * i)), false);
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   443
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   444
                    if (valuePtr != IntPtr.Zero)
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   445
                    {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   446
                        Marshal.FreeHGlobal(valuePtr);
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   447
                    }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   448
                }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   449
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   450
                // Set properties and save
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   451
                IntPtr problems = IntPtr.Zero;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   452
                if (IMAPIProp.SetProps((uint)propCount, propArray, problems) == Mapi.HResult.S_OK)
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   453
                {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   454
                    success = (IMAPIProp.SaveChanges(Mapi.SaveOptions.KEEP_OPEN_READWRITE) == Mapi.HResult.S_OK);
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   455
                }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   456
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   457
                // Log problems
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   458
                if (problems != IntPtr.Zero)
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   459
                {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   460
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   461
                }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   462
            }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   463
            catch (Exception ex)
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   464
            {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   465
                Log.Error("SetMAPIProperties: Error occured. " + ex.ToString());
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   466
                success = false;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   467
            }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   468
            finally
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   469
            {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   470
                if (IUnknown != IntPtr.Zero)
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   471
                {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   472
                    Marshal.Release(IUnknown);
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   473
                }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   474
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   475
                if (propArray != IntPtr.Zero)
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   476
                {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   477
                    Marshal.FreeHGlobal(propArray);
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   478
                }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   479
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   480
                if (valuePtr != IntPtr.Zero)
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   481
                {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   482
                    Marshal.FreeHGlobal(valuePtr);
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   483
                }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   484
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   485
                Mapi.MAPIUninitialize();
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   486
            }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   487
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   488
            return success;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   489
        }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   490
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   491
        /// <summary>
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   492
        /// Sets the given MAPI property on the Outlook mail item.
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   493
        /// Note: the caller has to make sure that the given value matches the
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   494
        /// required data type for the MAPI property.
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   495
        /// </summary>
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   496
        /// <param name="omi">The Outlook mail item to set the property for.</param>
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   497
        /// <param name="mapiProperties">The MAPI property to set.</param>
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   498
        /// <param name="values">The value to set.</param>
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   499
        /// <returns>True if the method succeeds, otherwise false.</returns>
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   500
        public static bool SetMAPIProperty(Outlook.MailItem omi, MapiProperty.MapiProp mapiProperty, object value)
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   501
        {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   502
            // The return status of this method
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   503
            bool success = false;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   504
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   505
            // Pointer to IUnknown interface
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   506
            IntPtr IUnknown = IntPtr.Zero;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   507
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   508
            // Pointer to IMessage interface
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   509
            IntPtr IMessage = IntPtr.Zero;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   510
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   511
            // Pointer to IMAPIProp interface
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   512
            IntPtr IMAPIProp = IntPtr.Zero;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   513
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   514
            // Structure that will hold the property value
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   515
            Mapi.SPropValue propValue;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   516
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   517
            // A pointer that points to the SPropValue structure 
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   518
            IntPtr ptrPropValue = IntPtr.Zero;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   519
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   520
            // Get MAPI object from mail item
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   521
            object mapiObject = omi?.MAPIOBJECT;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   522
            if (mapiObject == null)
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   523
            {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   524
                Log.Error("SetMAPIProperty: MAPI object is null. Property could not be set.");
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   525
                return success;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   526
            }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   527
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   528
            try
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   529
            {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   530
                // Initialize MAPI
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   531
                Mapi.MAPIInitialize(IntPtr.Zero);
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   532
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   533
                // Get the IUnknown interface from the MAPI object
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   534
                IUnknown = Marshal.GetIUnknownForObject(mapiObject);
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   535
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   536
                // Set the IMessage interface GUID that we pass to retrieve the IMessage interface.
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   537
                Guid guidIMessage = new Guid(Mapi.MAPIInterfaceIds.IMessage);
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   538
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   539
                // Try to retrieve the IMessage interface
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   540
                if (Marshal.QueryInterface(IUnknown, ref guidIMessage, out IMessage) != Mapi.HResult.S_OK)
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   541
                {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   542
                    Log.Error("SetMAPIProperty: Could not retrieve IMessage interface. Property could not be set.");
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   543
                    return success;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   544
                }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   545
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   546
                // Set the IMAPIProp interface GUID that we pass to retrieve the IMAPIProp Interface.
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   547
                Guid guidIMAPIProp = new Guid(Mapi.MAPIInterfaceIds.IMAPIProp);
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   548
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   549
                // Try to retrieve the IMAPIProp interface
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   550
                if ((Marshal.QueryInterface(IMessage, ref guidIMAPIProp, out IMAPIProp) != Mapi.HResult.S_OK) ||
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   551
                    (IMAPIProp == IntPtr.Zero))
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   552
                {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   553
                    Log.Error("SetMAPIProperty: Could not retrieve IMAPIProp interface. Property could not be set.");
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   554
                    return success;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   555
                }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   556
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   557
                // Create the SPropValue structure
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   558
                propValue = new Mapi.SPropValue
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   559
                {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   560
                    PropTag = mapiProperty.Tag
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   561
                };
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   562
                propValue.Value.li = (bool)value ? 1UL : 0UL;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   563
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   564
                // Get pointer to property value structure
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   565
                ptrPropValue = Marshal.AllocHGlobal(Marshal.SizeOf(propValue));
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   566
                Marshal.StructureToPtr(propValue, ptrPropValue, false);
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   567
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   568
                // Try to set the property
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   569
                Mapi.HrSetOneProp(IMAPIProp, ptrPropValue);
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   570
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   571
                // Save changes
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   572
                Mapi.IMAPIProp mapiProp = (Mapi.IMAPIProp)Marshal.GetTypedObjectForIUnknown(IUnknown, typeof(Mapi.IMAPIProp));
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   573
                success = (mapiProp.SaveChanges(Mapi.SaveOptions.KEEP_OPEN_READWRITE) == Mapi.HResult.S_OK);
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   574
            }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   575
            catch (Exception ex)
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   576
            {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   577
                Log.Error("SetMAPIProperty: Error occured. " + ex.ToString());
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   578
                success = false;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   579
            }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   580
            finally
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   581
            {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   582
                // Free used memory structures
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   583
                if (ptrPropValue != IntPtr.Zero)
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   584
                {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   585
                    Mapi.MAPIFreeBuffer(ptrPropValue);
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   586
                }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   587
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   588
                // Clean up all references to COM Objects
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   589
                if (IMAPIProp != IntPtr.Zero)
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   590
                {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   591
                    Marshal.Release(IMAPIProp);
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   592
                }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   593
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   594
                if (IMessage != IntPtr.Zero)
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   595
                {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   596
                    Marshal.Release(IMessage);
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   597
                }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   598
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   599
                if (IUnknown != IntPtr.Zero)
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   600
                {
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   601
                    Marshal.Release(IUnknown);
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   602
                }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   603
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   604
                Mapi.MAPIUninitialize();
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   605
            }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   606
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   607
            return success;
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   608
        }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   609
        #endregion
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   610
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   611
        #region MAPI DLL Imports
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   612
        /// <summary>
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   613
        /// The MAPIInitialize function increments the MAPI subsystem reference count and initializes global data for the MAPI DLL.
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   614
        /// </summary>
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   615
        /// <param name="lpMapiInit">[in] Pointer to a MAPIINIT_0 structure. The lpMapiInit parameter can be set to NULL.</param>
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   616
        /// <returns>
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   617
        /// S_OK
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   618
        /// The MAPI subsystem was initialized successfully.
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   619
        /// </returns>
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   620
        [DllImport("mapi32.DLL", CharSet = CharSet.Ansi)]
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   621
        internal static extern int MAPIInitialize(IntPtr lpMapiInit);
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   622
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   623
        /// <summary>
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   624
        /// The MAPIUninitialize function decrements the reference count, cleans up, and deletes per-instance global data for the MAPI DLL.
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   625
        /// </summary>
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   626
        [DllImport("mapi32.DLL", CharSet = CharSet.Ansi)]
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   627
        internal static extern void MAPIUninitialize();
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   628
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   629
        /// <summary>
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   630
        /// The HrGetOneProp function retrieves the value of a single property from a property interface, that is, an interface derived from IMAPIProp.
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   631
        /// </summary>
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   632
        /// <param name="pmp">[in] Pointer to the IMAPIProp interface from which the property value is to be retrieved.</param>
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   633
        /// <param name="ulPropTag">[in] Property tag of the property to be retrieved.</param>
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   634
        /// <param name="ppprop">[out] Pointer to a pointer to the returned SPropValue structure defining the retrieved property value.</param>
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   635
        /// <remarks>
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   636
        /// Unlike the IMAPIProp::GetProps method, the HrGetOneProp function never returns any warning.
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   637
        /// Because it retrieves only one property, it simply either succeeds or fails. For retrieving multiple properties,
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   638
        /// GetProps is faster. 
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   639
        ///
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   640
        /// You can set or change a single property with the HrSetOneProp function.
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   641
        /// </remarks>
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   642
        [DllImport("mapi32.DLL", CharSet = CharSet.Ansi, EntryPoint = "HrGetOneProp@12")]
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   643
        internal static extern void HrGetOneProp(IntPtr pmp, uint ulPropTag, out IntPtr ppprop);
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   644
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   645
        /// <summary>
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   646
        /// The HrSetOneProp function sets or changes the value of a single property on a property interface, that is, an interface derived from IMAPIProp.
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   647
        /// </summary>
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   648
        /// <param name="pmp">[in] Pointer to an IMAPIProp interface on which the property value is to be set or changed.</param>
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   649
        /// <param name="pprop">[in] Pointer to the SPropValue structure defining the property to be set or changed.</param>
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   650
        /// <remarks>
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   651
        /// Unlike the IMAPIProp::SetProps method, the HrSetOneProp function never returns any warning.
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   652
        /// Because it sets only one property, it simply either succeeds or fails.
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   653
        /// For setting or changing multiple properties, SetProps is faster. 
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   654
        /// </remarks>
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   655
        [DllImport("mapi32.DLL", CharSet = CharSet.Ansi, EntryPoint = "HrSetOneProp@8")]
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   656
        internal static extern void HrSetOneProp(IntPtr pmp, IntPtr pprop);
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   657
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   658
        /// <summary>
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   659
        /// The MAPIFreeBuffer function frees a memory buffer allocated with a call to the MAPIAllocateBuffer function or the MAPIAllocateMore function.
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   660
        /// </summary>
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   661
        /// <param name="lpBuffer">[in] Pointer to a previously allocated memory buffer. If NULL is passed in the lpBuffer parameter, MAPIFreeBuffer does nothing.</param>
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   662
        [DllImport("mapi32.DLL", CharSet = CharSet.Ansi, EntryPoint = "MAPIFreeBuffer@4")]
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   663
        internal static extern void MAPIFreeBuffer(IntPtr lpBuffer);
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   664
        #endregion
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   665
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   666
    }
81975e284b07 Add MAPI methods to set properties via p/invoke
Thomas
parents:
diff changeset
   667
}