Skip to main content

outlook_mapi/
mapi_initialize.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT license.
3
4//! Define [`Initialize`] and [`InitializeFlags`].
5
6use crate::sys;
7use core::ptr;
8use std::sync::Arc;
9use windows_core::*;
10
11/// Set of flags that can be passed to [`sys::MAPIInitialize`] through the
12/// [`sys::MAPIINIT::ulFlags`] member.
13#[derive(Default)]
14pub struct InitializeFlags {
15    /// Pass [`sys::MAPI_MULTITHREAD_NOTIFICATIONS`].
16    pub multithread_notifications: bool,
17
18    /// Pass [`sys::MAPI_NT_SERVICE`].
19    pub nt_service: bool,
20
21    /// Pass [`sys::MAPI_NO_COINIT`].
22    pub no_coinit: bool,
23}
24
25impl From<InitializeFlags> for u32 {
26    fn from(value: InitializeFlags) -> Self {
27        let multithread_notifications = if value.multithread_notifications {
28            sys::MAPI_MULTITHREAD_NOTIFICATIONS
29        } else {
30            0
31        };
32        let nt_service = if value.nt_service {
33            sys::MAPI_NT_SERVICE
34        } else {
35            0
36        };
37        let no_coinit = if value.no_coinit {
38            sys::MAPI_NO_COINIT
39        } else {
40            0
41        };
42
43        multithread_notifications | nt_service | no_coinit
44    }
45}
46
47/// Call [`sys::MAPIInitialize`] in the constructor, and balance it with a call to
48/// [`sys::MAPIUninitialize`] in the destructor.
49pub struct Initialize();
50
51impl Initialize {
52    /// Call [`sys::MAPIInitialize`] with the specified flags in [`InitializeFlags`].
53    pub fn new(flags: InitializeFlags) -> Result<Arc<Self>> {
54        unsafe {
55            sys::MAPIInitialize(ptr::from_mut(&mut sys::MAPIINIT {
56                ulVersion: sys::MAPI_INIT_VERSION,
57                ulFlags: flags.into(),
58            }) as *mut _)?;
59        }
60
61        Ok(Arc::new(Self()))
62    }
63}
64
65impl Drop for Initialize {
66    /// Call [`sys::MAPIUninitialize`].
67    fn drop(&mut self) {
68        unsafe {
69            sys::MAPIUninitialize();
70        }
71    }
72}