summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/mapi/mapiDll/MapiDll.cpp
blob: 027257a1117d1a7bf003be2f9bebfe7311ade84b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#pragma warning(disable : 4996)  // MAPILogoff is deprecated

#include <windows.h>
#include <mapidefs.h>
#include <mapi.h>
#include "msgMapi.h"

// Ensure that our COM structs match MS MAPI structs - thoroughly check each
// struct layout This needs that MAPI.h is not used from
// https://www.microsoft.com/en-us/download/details.aspx?id=12905 because the
// newer MAPI.h is the part of Windows SDK, and it includes MapiFileDescW and
// friends.

static_assert(sizeof(nsMapiFileDesc) == sizeof(MapiFileDesc), "Size mismatch!");
static_assert(offsetof(nsMapiFileDesc, ulReserved) ==
                  offsetof(MapiFileDesc, ulReserved),
              "Member offset mismatch!");
static_assert(offsetof(nsMapiFileDesc, flFlags) ==
                  offsetof(MapiFileDesc, flFlags),
              "Member offset mismatch!");
static_assert(offsetof(nsMapiFileDesc, nPosition_NotUsed) ==
                  offsetof(MapiFileDesc, nPosition),
              "Member offset mismatch!");
static_assert(offsetof(nsMapiFileDesc, lpszPathName) ==
                  offsetof(MapiFileDesc, lpszPathName),
              "Member offset mismatch!");
static_assert(offsetof(nsMapiFileDesc, lpszFileName) ==
                  offsetof(MapiFileDesc, lpszFileName),
              "Member offset mismatch!");
static_assert(offsetof(nsMapiFileDesc, lpFileType_NotUsed) ==
                  offsetof(MapiFileDesc, lpFileType),
              "Member offset mismatch!");

static_assert(sizeof(nsMapiRecipDesc) == sizeof(MapiRecipDesc),
              "Size mismatch!");
static_assert(offsetof(nsMapiRecipDesc, ulReserved) ==
                  offsetof(MapiRecipDesc, ulReserved),
              "Member offset mismatch!");
static_assert(offsetof(nsMapiRecipDesc, ulRecipClass) ==
                  offsetof(MapiRecipDesc, ulRecipClass),
              "Member offset mismatch!");
static_assert(offsetof(nsMapiRecipDesc, lpszName) ==
                  offsetof(MapiRecipDesc, lpszName),
              "Member offset mismatch!");
static_assert(offsetof(nsMapiRecipDesc, lpszAddress) ==
                  offsetof(MapiRecipDesc, lpszAddress),
              "Member offset mismatch!");
static_assert(offsetof(nsMapiRecipDesc, ulEIDSize_NotUsed) ==
                  offsetof(MapiRecipDesc, ulEIDSize),
              "Member offset mismatch!");
static_assert(offsetof(nsMapiRecipDesc, lpEntryID_NotUsed) ==
                  offsetof(MapiRecipDesc, lpEntryID),
              "Member offset mismatch!");

static_assert(sizeof(nsMapiMessage) == sizeof(MapiMessage), "Size mismatch!");
static_assert(offsetof(nsMapiMessage, ulReserved) ==
                  offsetof(MapiMessage, ulReserved),
              "Member offset mismatch!");
static_assert(offsetof(nsMapiMessage, lpszSubject) ==
                  offsetof(MapiMessage, lpszSubject),
              "Member offset mismatch!");
static_assert(offsetof(nsMapiMessage, lpszNoteText) ==
                  offsetof(MapiMessage, lpszNoteText),
              "Member offset mismatch!");
static_assert(offsetof(nsMapiMessage, lpszMessageType) ==
                  offsetof(MapiMessage, lpszMessageType),
              "Member offset mismatch!");
static_assert(offsetof(nsMapiMessage, lpszDateReceived) ==
                  offsetof(MapiMessage, lpszDateReceived),
              "Member offset mismatch!");
static_assert(offsetof(nsMapiMessage, lpszConversationID_NotUsed) ==
                  offsetof(MapiMessage, lpszConversationID),
              "Member offset mismatch!");
static_assert(offsetof(nsMapiMessage, flFlags) ==
                  offsetof(MapiMessage, flFlags),
              "Member offset mismatch!");
static_assert(offsetof(nsMapiMessage, lpOriginator) ==
                  offsetof(MapiMessage, lpOriginator),
              "Member offset mismatch!");
static_assert(offsetof(nsMapiMessage, nRecipCount) ==
                  offsetof(MapiMessage, nRecipCount),
              "Member offset mismatch!");
static_assert(offsetof(nsMapiMessage, lpRecips) ==
                  offsetof(MapiMessage, lpRecips),
              "Member offset mismatch!");
static_assert(offsetof(nsMapiMessage, nFileCount) ==
                  offsetof(MapiMessage, nFileCount),
              "Member offset mismatch!");
static_assert(offsetof(nsMapiMessage, lpFiles) ==
                  offsetof(MapiMessage, lpFiles),
              "Member offset mismatch!");

static_assert(sizeof(nsMapiFileDescW) == sizeof(MapiFileDescW),
              "Size mismatch!");
static_assert(offsetof(nsMapiFileDescW, ulReserved) ==
                  offsetof(MapiFileDescW, ulReserved),
              "Member offset mismatch!");
static_assert(offsetof(nsMapiFileDescW, flFlags) ==
                  offsetof(MapiFileDescW, flFlags),
              "Member offset mismatch!");
static_assert(offsetof(nsMapiFileDescW, nPosition_NotUsed) ==
                  offsetof(MapiFileDescW, nPosition),
              "Member offset mismatch!");
static_assert(offsetof(nsMapiFileDescW, lpszPathName) ==
                  offsetof(MapiFileDescW, lpszPathName),
              "Member offset mismatch!");
static_assert(offsetof(nsMapiFileDescW, lpszFileName) ==
                  offsetof(MapiFileDescW, lpszFileName),
              "Member offset mismatch!");
static_assert(offsetof(nsMapiFileDescW, lpFileType_NotUsed) ==
                  offsetof(MapiFileDescW, lpFileType),
              "Member offset mismatch!");

static_assert(sizeof(nsMapiRecipDescW) == sizeof(MapiRecipDescW),
              "Size mismatch!");
static_assert(offsetof(nsMapiRecipDescW, ulReserved) ==
                  offsetof(MapiRecipDescW, ulReserved),
              "Member offset mismatch!");
static_assert(offsetof(nsMapiRecipDescW, ulRecipClass) ==
                  offsetof(MapiRecipDescW, ulRecipClass),
              "Member offset mismatch!");
static_assert(offsetof(nsMapiRecipDescW, lpszName) ==
                  offsetof(MapiRecipDescW, lpszName),
              "Member offset mismatch!");
static_assert(offsetof(nsMapiRecipDescW, lpszAddress) ==
                  offsetof(MapiRecipDescW, lpszAddress),
              "Member offset mismatch!");
static_assert(offsetof(nsMapiRecipDescW, ulEIDSize_NotUsed) ==
                  offsetof(MapiRecipDescW, ulEIDSize),
              "Member offset mismatch!");
static_assert(offsetof(nsMapiRecipDescW, lpEntryID_NotUsed) ==
                  offsetof(MapiRecipDescW, lpEntryID),
              "Member offset mismatch!");

static_assert(sizeof(nsMapiMessageW) == sizeof(MapiMessageW), "Size mismatch!");
static_assert(offsetof(nsMapiMessageW, ulReserved) ==
                  offsetof(MapiMessageW, ulReserved),
              "Member offset mismatch!");
static_assert(offsetof(nsMapiMessageW, lpszSubject) ==
                  offsetof(MapiMessageW, lpszSubject),
              "Member offset mismatch!");
static_assert(offsetof(nsMapiMessageW, ulReserved) ==
                  offsetof(MapiMessageW, ulReserved),
              "Member offset mismatch!");
static_assert(offsetof(nsMapiMessageW, lpszSubject) ==
                  offsetof(MapiMessageW, lpszSubject),
              "Member offset mismatch!");
static_assert(offsetof(nsMapiMessageW, lpszNoteText) ==
                  offsetof(MapiMessageW, lpszNoteText),
              "Member offset mismatch!");
static_assert(offsetof(nsMapiMessageW, lpszMessageType) ==
                  offsetof(MapiMessageW, lpszMessageType),
              "Member offset mismatch!");
static_assert(offsetof(nsMapiMessageW, lpszDateReceived) ==
                  offsetof(MapiMessageW, lpszDateReceived),
              "Member offset mismatch!");
static_assert(offsetof(nsMapiMessageW, lpszConversationID_NotUsed) ==
                  offsetof(MapiMessageW, lpszConversationID),
              "Member offset mismatch!");
static_assert(offsetof(nsMapiMessageW, flFlags) ==
                  offsetof(MapiMessageW, flFlags),
              "Member offset mismatch!");
static_assert(offsetof(nsMapiMessageW, lpOriginator) ==
                  offsetof(MapiMessageW, lpOriginator),
              "Member offset mismatch!");
static_assert(offsetof(nsMapiMessageW, nRecipCount) ==
                  offsetof(MapiMessageW, nRecipCount),
              "Member offset mismatch!");
static_assert(offsetof(nsMapiMessageW, lpRecips) ==
                  offsetof(MapiMessageW, lpRecips),
              "Member offset mismatch!");
static_assert(offsetof(nsMapiMessageW, nFileCount) ==
                  offsetof(MapiMessageW, nFileCount),
              "Member offset mismatch!");
static_assert(offsetof(nsMapiMessageW, lpFiles) ==
                  offsetof(MapiMessageW, lpFiles),
              "Member offset mismatch!");

#define MAX_RECIPS 2000
#define MAX_FILES 100

#define MAX_NAME_LEN 256
#define MAX_PW_LEN 256
#define MAX_MSGINFO_LEN 512
#define MAX_POINTERS 32

const CLSID CLSID_CMapiImp = {0x29f458be,
                              0x8866,
                              0x11d5,
                              {0xa3, 0xdd, 0x0, 0xb0, 0xd0, 0xf3, 0xba, 0xa7}};
const IID IID_nsIMapi = {0x6EDCD38E,
                         0x8861,
                         0x11d5,
                         {0xA3, 0xDD, 0x00, 0xB0, 0xD0, 0xF3, 0xBA, 0xA7}};

DWORD tId = 0;

#define MAPI_MESSAGE_TYPE 0
#define MAPI_RECIPIENT_TYPE 1

typedef struct {
  LPVOID lpMem;
  UCHAR memType;
} memTrackerType;

// this can't be right.
memTrackerType memArray[MAX_POINTERS];

//
// For remembering memory...how ironic.
//
void SetPointerArray(LPVOID ptr, BYTE type) {
  int i;

  for (i = 0; i < MAX_POINTERS; i++) {
    if (memArray[i].lpMem == NULL) {
      memArray[i].lpMem = ptr;
      memArray[i].memType = type;
      break;
    }
  }
}

BOOL WINAPI DllMain(HINSTANCE aInstance, DWORD aReason, LPVOID aReserved) {
  switch (aReason) {
    case DLL_PROCESS_ATTACH:
      tId = TlsAlloc();
      if (tId == 0xFFFFFFFF) return FALSE;
      break;

    case DLL_PROCESS_DETACH:
      TlsFree(tId);
      break;
  }
  return TRUE;
}

BOOL InitMozillaReference(nsIMapi** aRetValue) {
  // Check whether this thread has a valid Interface
  // by looking into thread-specific-data variable

  *aRetValue = (nsIMapi*)TlsGetValue(tId);

  // Check whether the pointer actually resolves to
  // a valid method call; otherwise mozilla is not running

  if ((*aRetValue) && (*aRetValue)->IsValid() == S_OK) return TRUE;

  HRESULT hRes = ::CoInitialize(NULL);

  hRes = ::CoCreateInstance(CLSID_CMapiImp, NULL, CLSCTX_LOCAL_SERVER,
                            IID_nsIMapi, (LPVOID*)aRetValue);

  if (hRes == S_OK && (*aRetValue)->Initialize() == S_OK)
    if (TlsSetValue(tId, (LPVOID)(*aRetValue))) return TRUE;

  // Either CoCreate or TlsSetValue failed; so return FALSE

  if ((*aRetValue)) (*aRetValue)->Release();

  ::CoUninitialize();
  return FALSE;
}

/////////////////////////////////////////////////////////////////////////////
// The MAPILogon function begins a Simple MAPI session, loading the default
// message store and address book providers
/////////////////////////////////////////////////////////////////////////////

ULONG FAR PASCAL MAPILogon(ULONG aUIParam, LPSTR aProfileName, LPSTR aPassword,
                           FLAGS aFlags, ULONG aReserved, LPLHANDLE aSession) {
  HRESULT hr = 0;
  ULONG nSessionId = 0;
  nsIMapi* pNsMapi = NULL;

  if (!InitMozillaReference(&pNsMapi)) return MAPI_E_FAILURE;

  hr = pNsMapi->Login(aUIParam, aProfileName, aPassword, aFlags, &nSessionId);
  if (hr == S_OK)
    (*aSession) = (LHANDLE)nSessionId;
  else
    return nSessionId;

  return SUCCESS_SUCCESS;
}

ULONG FAR PASCAL MAPILogoff(LHANDLE aSession, ULONG aUIParam, FLAGS aFlags,
                            ULONG aReserved) {
  nsIMapi* pNsMapi = (nsIMapi*)TlsGetValue(tId);
  if (pNsMapi != NULL) {
    if (pNsMapi->Logoff((ULONG)aSession) == S_OK) pNsMapi->Release();
    pNsMapi = NULL;
  }

  TlsSetValue(tId, NULL);

  ::CoUninitialize();

  return SUCCESS_SUCCESS;
}

ULONG FAR PASCAL MAPISendMail(LHANDLE lhSession, ULONG ulUIParam,
                              nsMapiMessage* lpMessage, FLAGS flFlags,
                              ULONG ulReserved) {
  HRESULT hr = 0;
  BOOL bTempSession = FALSE;
  nsIMapi* pNsMapi = NULL;

  if (!InitMozillaReference(&pNsMapi)) return MAPI_E_FAILURE;

  if (lpMessage->nRecipCount > MAX_RECIPS) return MAPI_E_TOO_MANY_RECIPIENTS;

  if (lpMessage->nFileCount > MAX_FILES) return MAPI_E_TOO_MANY_FILES;

  if ((!(flFlags & MAPI_DIALOG)) && (lpMessage->lpRecips == NULL))
    return MAPI_E_UNKNOWN_RECIPIENT;

  if (!lhSession || pNsMapi->IsValidSession(lhSession) != S_OK) {
    FLAGS LoginFlag = flFlags & (MAPI_LOGON_UI | MAPI_NEW_SESSION);
    hr = MAPILogon(ulUIParam, nullptr, nullptr, LoginFlag, 0, &lhSession);
    if (hr != SUCCESS_SUCCESS) return MAPI_E_LOGIN_FAILURE;
    bTempSession = TRUE;
  }

  hr = pNsMapi->SendMail(lhSession, lpMessage, flFlags, ulReserved);

  // we are seeing a problem when using Word, although we return success from
  // the MAPI support MS COM interface in mozilla, we are getting this error
  // here. This is a temporary hack !!
  if (hr == (HRESULT)0x800703e6) hr = SUCCESS_SUCCESS;

  if (bTempSession) MAPILogoff(lhSession, ulUIParam, 0, 0);

  return hr;
}

ULONG FAR PASCAL MAPISendMailW(LHANDLE lhSession, ULONG ulUIParam,
                               nsMapiMessageW* lpMessage, FLAGS flFlags,
                               ULONG ulReserved) {
  HRESULT hr = 0;
  BOOL bTempSession = FALSE;
  nsIMapi* pNsMapi = nullptr;

  if (!InitMozillaReference(&pNsMapi)) return MAPI_E_FAILURE;

  if (lpMessage->nRecipCount > MAX_RECIPS) return MAPI_E_TOO_MANY_RECIPIENTS;

  if (lpMessage->nFileCount > MAX_FILES) return MAPI_E_TOO_MANY_FILES;

  if ((!(flFlags & MAPI_DIALOG)) && (lpMessage->lpRecips == nullptr))
    return MAPI_E_UNKNOWN_RECIPIENT;

  if (!lhSession || pNsMapi->IsValidSession(lhSession) != S_OK) {
    FLAGS LoginFlag = flFlags & (MAPI_LOGON_UI | MAPI_NEW_SESSION);
    hr = MAPILogon(ulUIParam, nullptr, nullptr, LoginFlag, 0, &lhSession);
    if (hr != SUCCESS_SUCCESS) return MAPI_E_LOGIN_FAILURE;
    bTempSession = TRUE;
  }

  hr = pNsMapi->SendMailW(lhSession, lpMessage, flFlags, ulReserved);

  // we are seeing a problem when using Word, although we return success from
  // the MAPI support MS COM interface in mozilla, we are getting this error
  // here. This is a temporary hack !!
  if (hr == (HRESULT)0x800703e6) hr = SUCCESS_SUCCESS;

  if (bTempSession) MAPILogoff(lhSession, ulUIParam, 0, 0);

  return hr;
}

ULONG FAR PASCAL MAPISendDocuments(ULONG ulUIParam, LPSTR lpszDelimChar,
                                   LPSTR lpszFilePaths, LPSTR lpszFileNames,
                                   ULONG ulReserved) {
  LHANDLE lhSession;
  nsIMapi* pNsMapi = NULL;

  if (!InitMozillaReference(&pNsMapi)) return MAPI_E_FAILURE;

  unsigned long result =
      MAPILogon(ulUIParam, nullptr, nullptr, MAPI_LOGON_UI, 0, &lhSession);
  if (result != SUCCESS_SUCCESS) return MAPI_E_LOGIN_FAILURE;

  HRESULT hr;

  hr = pNsMapi->SendDocuments(lhSession, lpszDelimChar, lpszFilePaths,
                              lpszFileNames, ulReserved);

  MAPILogoff(lhSession, ulUIParam, 0, 0);

  return hr;
}

ULONG FAR PASCAL MAPIFindNext(LHANDLE lhSession, ULONG ulUIParam,
                              const LPSTR lpszMessageType,
                              const LPSTR lpszSeedMessageID, FLAGS flFlags,
                              ULONG ulReserved,
                              unsigned char lpszMessageID[64]) {
  nsIMapi* pNsMapi = NULL;

  if (!InitMozillaReference(&pNsMapi)) return MAPI_E_FAILURE;

  if (lhSession == 0) return MAPI_E_INVALID_SESSION;

  return pNsMapi->FindNext(lhSession, ulUIParam, lpszMessageType,
                           lpszSeedMessageID, flFlags, ulReserved,
                           lpszMessageID);
}

ULONG FAR PASCAL MAPIReadMail(LHANDLE lhSession, ULONG ulUIParam,
                              LPSTR lpszMessageID, FLAGS flFlags,
                              ULONG ulReserved, nsMapiMessage** lppMessage) {
  nsIMapi* pNsMapi = NULL;

  if (!InitMozillaReference(&pNsMapi)) return MAPI_E_FAILURE;

  if (lhSession == 0) return MAPI_E_INVALID_SESSION;

  return pNsMapi->ReadMail(lhSession, ulUIParam, lpszMessageID, flFlags,
                           ulReserved, lppMessage);
}

ULONG FAR PASCAL MAPISaveMail(LHANDLE lhSession, ULONG ulUIParam,
                              lpnsMapiMessage lpMessage, FLAGS flFlags,
                              ULONG ulReserved, LPSTR lpszMessageID) {
  nsIMapi* pNsMapi = NULL;

  if (lhSession == 0) return MAPI_E_INVALID_SESSION;

  if (!InitMozillaReference(&pNsMapi)) return MAPI_E_FAILURE;

  return MAPI_E_FAILURE;
}

ULONG FAR PASCAL MAPIDeleteMail(LHANDLE lhSession, ULONG ulUIParam,
                                LPSTR lpszMessageID, FLAGS flFlags,
                                ULONG ulReserved) {
  nsIMapi* pNsMapi = NULL;

  if (lhSession == 0) return MAPI_E_INVALID_SESSION;

  if (!InitMozillaReference(&pNsMapi)) return MAPI_E_FAILURE;

  return pNsMapi->DeleteMail(lhSession, ulUIParam, lpszMessageID, flFlags,
                             ulReserved);
}

ULONG FAR PASCAL MAPIAddress(LHANDLE lhSession, ULONG ulUIParam,
                             LPSTR lpszCaption, ULONG nEditFields,
                             LPSTR lpszLabels, ULONG nRecips,
                             lpMapiRecipDesc lpRecips, FLAGS flFlags,
                             ULONG ulReserved, LPULONG lpnNewRecips,
                             lpMapiRecipDesc FAR* lppNewRecips) {
  return MAPI_E_NOT_SUPPORTED;
}

ULONG FAR PASCAL MAPIDetails(LHANDLE lhSession, ULONG ulUIParam,
                             lpMapiRecipDesc lpRecip, FLAGS flFlags,
                             ULONG ulReserved) {
  return MAPI_E_NOT_SUPPORTED;
}

ULONG FAR PASCAL MAPIResolveName(LHANDLE lhSession, ULONG ulUIParam,
                                 LPSTR lpszName, FLAGS flFlags,
                                 ULONG ulReserved,
                                 lpMapiRecipDesc FAR* lppRecip) {
  char* lpszRecipName = new char[(strlen((const char*)lpszName) + 1)];
  if (lpszRecipName == NULL) return MAPI_E_INSUFFICIENT_MEMORY;
  char* lpszRecipAddress = new char[(strlen((const char*)lpszName) + 6)];
  if (!lpszRecipAddress) {
    delete[] lpszRecipName;
    return MAPI_E_INSUFFICIENT_MEMORY;
  }
  strcpy(lpszRecipName, (const char*)lpszName);
  strcpy(lpszRecipAddress, (const char*)lpszName);
  (*lppRecip) = (lpMapiRecipDesc FAR)malloc(sizeof(MapiRecipDesc));
  if (!(*lppRecip)) {
    delete[] lpszRecipName;
    delete[] lpszRecipAddress;
    return MAPI_E_INSUFFICIENT_MEMORY;
  }
  (*lppRecip)->ulRecipClass = 1;
  (*lppRecip)->lpszName = lpszRecipName;
  (*lppRecip)->lpszAddress = lpszRecipAddress;
  (*lppRecip)->ulEIDSize = 0;
  (*lppRecip)->lpEntryID = 0;
  return SUCCESS_SUCCESS;
}

void FreeMAPIRecipient(lpMapiRecipDesc pv);
void FreeMAPIMessage(lpMapiMessage pv);

ULONG FAR PASCAL MAPIFreeBuffer(LPVOID pv) {
  int i;

  if (!pv) return S_OK;

  for (i = 0; i < MAX_POINTERS; i++) {
    if (pv == memArray[i].lpMem) {
      if (memArray[i].memType == MAPI_MESSAGE_TYPE) {
        FreeMAPIMessage((MapiMessage*)pv);
        memArray[i].lpMem = NULL;
      } else if (memArray[i].memType == MAPI_RECIPIENT_TYPE) {
        FreeMAPIRecipient((MapiRecipDesc*)pv);
        memArray[i].lpMem = NULL;
      }
    }
  }

  pv = NULL;
  return S_OK;
}

ULONG FAR PASCAL GetMapiDllVersion() { return 94; }

void FreeMAPIFile(lpMapiFileDesc pv) {
  if (!pv) return;

  if (pv->lpszPathName != NULL) free(pv->lpszPathName);

  if (pv->lpszFileName != NULL) free(pv->lpszFileName);
}

void FreeMAPIMessage(lpMapiMessage pv) {
  ULONG i;

  if (!pv) return;

  if (pv->lpszSubject != NULL) free(pv->lpszSubject);

  if (pv->lpszNoteText) free(pv->lpszNoteText);

  if (pv->lpszMessageType) free(pv->lpszMessageType);

  if (pv->lpszDateReceived) free(pv->lpszDateReceived);

  if (pv->lpszConversationID) free(pv->lpszConversationID);

  if (pv->lpOriginator) FreeMAPIRecipient(pv->lpOriginator);

  for (i = 0; i < pv->nRecipCount; i++) {
    if (&(pv->lpRecips[i]) != NULL) {
      FreeMAPIRecipient(&(pv->lpRecips[i]));
    }
  }

  if (pv->lpRecips != NULL) {
    free(pv->lpRecips);
  }

  for (i = 0; i < pv->nFileCount; i++) {
    if (&(pv->lpFiles[i]) != NULL) {
      FreeMAPIFile(&(pv->lpFiles[i]));
    }
  }

  if (pv->lpFiles != NULL) {
    free(pv->lpFiles);
  }

  free(pv);
  pv = NULL;
}

void FreeMAPIRecipient(lpMapiRecipDesc pv) {
  if (!pv) return;

  if (pv->lpszName != NULL) free(pv->lpszName);

  if (pv->lpszAddress != NULL) free(pv->lpszAddress);

  if (pv->lpEntryID != NULL) free(pv->lpEntryID);
}