aboutsummaryrefslogtreecommitdiffstats
path: root/src/native/addrbook/msoutlook/MsOutlookAddrBookContactSourceService.cxx
blob: e65f02a3f9a0b956cb816ff7544f929f1b2a25c8 (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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
/*
 * Jitsi, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

#include "MsOutlookAddrBookContactSourceService.h"

#include "com/ComClient.h"
#include "MAPINotification.h"
#include "MAPISession.h"
#include "MAPIBitness.h"
#include "MsOutlookUtils.h"
#include <Tchar.h>
#include "StringUtils.h"

typedef BOOL (STDAPICALLTYPE *LPFBINFROMHEX)(LPSTR, LPBYTE);
typedef void (STDAPICALLTYPE *LPFREEPROWS)(LPSRowSet);
typedef void (STDAPICALLTYPE *LPHEXFROMBIN)(LPBYTE, int, LPSTR);
typedef HRESULT (STDAPICALLTYPE *LPHRALLOCADVISESINK)(LPNOTIFCALLBACK, LPVOID, LPMAPIADVISESINK FAR *);
typedef HRESULT (STDAPICALLTYPE *LPHRQUERYALLROWS)(LPMAPITABLE, LPSPropTagArray,
LPSRestriction, LPSSortOrderSet, LONG, LPSRowSet FAR *);

static HANDLE MsOutlookAddrBookContactSourceService_comServerHandle = NULL;
static LPFBINFROMHEX MsOutlookAddrBookContactSourceService_fBinFromHex;
static LPFREEPROWS MsOutlookAddrBookContactSourceService_freeProws;
static LPHEXFROMBIN MsOutlookAddrBookContactSourceService_hexFromBin;
static LPHRALLOCADVISESINK MsOutlookAddrBookContactSourceService_hrAllocAdviseSink;
static LPHRQUERYALLROWS MsOutlookAddrBookContactSourceService_hrQueryAllRows;
static LPMAPIALLOCATEBUFFER
    MsOutlookAddrBookContactSourceService_mapiAllocateBuffer;
static LPMAPIFREEBUFFER MsOutlookAddrBookContactSourceService_mapiFreeBuffer;
static LPMAPIINITIALIZE MsOutlookAddrBookContactSourceService_mapiInitialize;
static LPMAPILOGONEX MsOutlookAddrBookContactSourceService_mapiLogonEx;
static LPMAPIUNINITIALIZE
    MsOutlookAddrBookContactSourceService_mapiUninitialize;
static HMODULE MsOutlookAddrBookContactSourceService_hMapiLib = NULL;

HRESULT MsOutlookAddrBookContactSourceService_startComServer(void);

HRESULT MsOutlookAddrBookContactSourceService_MAPIInitialize
    (jlong version, jlong flags)
{
    HKEY regKey;
    HRESULT hResult = MAPI_E_NO_SUPPORT;

    /*
     * In the absence of a default e-mail program, MAPIInitialize may show a
     * dialog to notify of the fact. The dialog is undesirable here. Because we
     * implement ContactSourceService for Microsoft Outlook, we will try to
     * mitigate the problem by implementing an ad-hoc check whether Microsoft
     * Outlook is installed.
     */
    if (ERROR_SUCCESS
            == RegOpenKeyEx(
                    HKEY_LOCAL_MACHINE,
                    _T("Software\\Microsoft\\Office"),
                    0,
                    KEY_ENUMERATE_SUB_KEYS,
                    &regKey))
    {
        DWORD i = 0;
        TCHAR installRootKeyName[
                255 // The size limit of key name as documented in MSDN
                    + 20 // \Outlook\InstallRoot
                    + 1]; // The terminating null character
        MsOutlookUtils_logInfo("Searching for outlook InstallRoot.");
        while (1)
        {
            LONG regEnumKeyEx;
            DWORD subkeyNameLength = 255 + 1;
            LPTSTR str;
            HKEY installRootKey;
            DWORD pathValueType;
            DWORD pathValueSize;

            regEnumKeyEx = RegEnumKeyEx(
                        regKey,
                        i,
                        installRootKeyName,
                        &subkeyNameLength,
                        NULL,
                        NULL,
                        NULL,
                        NULL);
            if (ERROR_NO_MORE_ITEMS == regEnumKeyEx)
			{
            	MsOutlookUtils_logInfo("No more Software\\Microsoft\\Office items.");
                break;
			}

            i++;
            if (ERROR_SUCCESS != regEnumKeyEx)
			{
            	MsOutlookUtils_logInfo("Error quering the next Software\\Microsoft\\Office item.");
                continue;
			}

            str = installRootKeyName + subkeyNameLength;
            memcpy(str, _T("\\Outlook\\InstallRoot"), 20 * sizeof(TCHAR));
            *(str + 20) = 0;
			MsOutlookUtils_log("Trying to open the following key:");
			MsOutlookUtils_log(installRootKeyName);
            if (ERROR_SUCCESS
                    == RegOpenKeyEx(
                            regKey,
                            installRootKeyName,
                            0,
                            KEY_QUERY_VALUE,
                            &installRootKey))
            {
            	MsOutlookUtils_logInfo("The key is opened successfully.");
                if ((ERROR_SUCCESS
                        == RegQueryValueEx(
                                installRootKey,
                                _T("Path"),
                                NULL,
                                &pathValueType,
                                NULL,
                                &pathValueSize))
                    && (REG_SZ == pathValueType)
                    && pathValueSize)
                {
                	MsOutlookUtils_logInfo("Path value found.");
                    LPTSTR pathValue;

                    // MSDN says "the string may not have been stored with the
                    // proper terminating null characters."
                    pathValueSize
                        += sizeof(TCHAR)
                            * (12 // \Outlook.exe
                                + 1); // The terminating null character

                    if (pathValueSize <= sizeof(installRootKeyName))
                        pathValue = installRootKeyName;
                    else
                    {
                        pathValue = (LPTSTR)::malloc(pathValueSize);
                        if (!pathValue)
						{
                        	MsOutlookUtils_logInfo("Error with memory allocation for the pathValue.");
                            continue;
						}
                    }

                    if (ERROR_SUCCESS
                            == RegQueryValueEx(
                                    installRootKey,
                                    _T("Path"),
                                    NULL,
                                    NULL,
                                    (LPBYTE) pathValue, &pathValueSize))
                    {
                    	MsOutlookUtils_logInfo("The path value is retrieved");
                        DWORD pathValueLength = pathValueSize / sizeof(TCHAR);

                        if (pathValueLength)
                        {
                        	MsOutlookUtils_logInfo("The path value is retrieved successfully. The length is not 0.");
                            DWORD fileAttributes;

                            str = pathValue + (pathValueLength - 1);
                            if (*str)
                                str++;
                            memcpy(str, _T("\\Outlook.exe"), 12 * sizeof(TCHAR));
                            *(str + 12) = 0;
                            MsOutlookUtils_logInfo("Trying to retrieve atributes for:");
                            MsOutlookUtils_logInfo(pathValue);
                            fileAttributes = GetFileAttributes(pathValue);
                            if (INVALID_FILE_ATTRIBUTES != fileAttributes)
							{
                            	MsOutlookUtils_logInfo("The file exists.");
                                hResult = S_OK;
							}
							else
							{
								MsOutlookUtils_logInfo("The file doesn't exists");
							}
                        }
						else
						{
							MsOutlookUtils_logInfo("Error - the length of the path value is 0.");
						}
                    }
					else
					{
						MsOutlookUtils_logInfo("Error retrieving the pathValue.");
					}

                    if (pathValue != installRootKeyName)
                        free(pathValue);
                }
				else
				{
					MsOutlookUtils_logInfo("Error Path value not found.");
				}
                RegCloseKey(installRootKey);
            }
			else
			{
				MsOutlookUtils_logInfo("Error openning the key.");
			}
        }
        RegCloseKey(regKey);

        // Make sure that Microsoft Outlook is the default mail client in order
        // to prevent its dialog in the case of it not being the default mail
        // client.
        if (HR_SUCCEEDED(hResult))
        {
        	if(MsOutlookUtils_isOutlookDefaultMailClient())
        	{
        		hResult = S_OK;
        	}
        	else
        	{
        		hResult = MAPI_E_NO_SUPPORT;
        	}
        }
		else
		{
			MsOutlookUtils_logInfo("Outlook is not installed.");
		}
    }
	else
	{
		MsOutlookUtils_logInfo("Error opening HKLM\\Software\\Microsoft\\Office registry.");
	}

    // If we've determined that we'd like to go on with MAPI, try to load it.
    if (HR_SUCCEEDED(hResult))
    {
    	MsOutlookUtils_logInfo("Loading MAPI.");
        MsOutlookAddrBookContactSourceService_hMapiLib
            = ::LoadLibrary(_T("mapi32.dll"));

        hResult = MAPI_E_NO_SUPPORT;
        if(MsOutlookAddrBookContactSourceService_hMapiLib)
        {
        	MsOutlookUtils_logInfo("Loading MAPI functions");
            // get and check function pointers
            MsOutlookAddrBookContactSourceService_mapiInitialize
                = (LPMAPIINITIALIZE) GetProcAddress(
                        MsOutlookAddrBookContactSourceService_hMapiLib,
                        "MAPIInitialize");
            MsOutlookAddrBookContactSourceService_mapiUninitialize
                = (LPMAPIUNINITIALIZE) GetProcAddress(
                            MsOutlookAddrBookContactSourceService_hMapiLib,
                            "MAPIUninitialize");
            MsOutlookAddrBookContactSourceService_mapiAllocateBuffer
                = (LPMAPIALLOCATEBUFFER) GetProcAddress(
                        MsOutlookAddrBookContactSourceService_hMapiLib,
                        "MAPIAllocateBuffer");
            MsOutlookAddrBookContactSourceService_mapiFreeBuffer
                = (LPMAPIFREEBUFFER) GetProcAddress(
                        MsOutlookAddrBookContactSourceService_hMapiLib,
                        "MAPIFreeBuffer");
            MsOutlookAddrBookContactSourceService_mapiLogonEx
                = (LPMAPILOGONEX) GetProcAddress(
                        MsOutlookAddrBookContactSourceService_hMapiLib,
                        "MAPILogonEx");

            // Depending on mapi32.dll version the following functions must be
            // loaded with or without "...@#".
            MsOutlookAddrBookContactSourceService_fBinFromHex
                = (LPFBINFROMHEX) GetProcAddress(
                        MsOutlookAddrBookContactSourceService_hMapiLib,
                        "FBinFromHex");
            if(MsOutlookAddrBookContactSourceService_fBinFromHex == NULL)
            {
                MsOutlookAddrBookContactSourceService_fBinFromHex
                    = (LPFBINFROMHEX) GetProcAddress(
                            MsOutlookAddrBookContactSourceService_hMapiLib,
                            "FBinFromHex@8");
            }
            MsOutlookAddrBookContactSourceService_freeProws
                = (LPFREEPROWS) GetProcAddress(
                        MsOutlookAddrBookContactSourceService_hMapiLib,
                        "FreeProws");
            if(MsOutlookAddrBookContactSourceService_freeProws == NULL)
            {
                MsOutlookAddrBookContactSourceService_freeProws
                    = (LPFREEPROWS) GetProcAddress(
                            MsOutlookAddrBookContactSourceService_hMapiLib,
                            "FreeProws@4");
            }
            MsOutlookAddrBookContactSourceService_hexFromBin
                = (LPHEXFROMBIN) GetProcAddress(
                        MsOutlookAddrBookContactSourceService_hMapiLib,
                        "HexFromBin");
            if(MsOutlookAddrBookContactSourceService_hexFromBin == NULL)
            {
                MsOutlookAddrBookContactSourceService_hexFromBin
                    = (LPHEXFROMBIN) GetProcAddress(
                            MsOutlookAddrBookContactSourceService_hMapiLib,
                            "HexFromBin@12");
            }
            MsOutlookAddrBookContactSourceService_hrAllocAdviseSink
                = (LPHRALLOCADVISESINK)
                    GetProcAddress(
                            MsOutlookAddrBookContactSourceService_hMapiLib,
                            "HrAllocAdviseSink");
            if(MsOutlookAddrBookContactSourceService_hrAllocAdviseSink == NULL)
            {
                MsOutlookAddrBookContactSourceService_hrAllocAdviseSink
                    = (LPHRALLOCADVISESINK)
                    GetProcAddress(
                            MsOutlookAddrBookContactSourceService_hMapiLib,
                            "HrAllocAdviseSink@12");
            }
            MsOutlookAddrBookContactSourceService_hrQueryAllRows
                = (LPHRQUERYALLROWS) GetProcAddress(
                            MsOutlookAddrBookContactSourceService_hMapiLib,
                            "HrQueryAllRows");
            if(MsOutlookAddrBookContactSourceService_hrQueryAllRows == NULL)
            {
                MsOutlookAddrBookContactSourceService_hrQueryAllRows
                    = (LPHRQUERYALLROWS) GetProcAddress(
                            MsOutlookAddrBookContactSourceService_hMapiLib,
                            "HrQueryAllRows@24");
            }

            if (MsOutlookAddrBookContactSourceService_mapiInitialize
                && MsOutlookAddrBookContactSourceService_mapiUninitialize
                && MsOutlookAddrBookContactSourceService_mapiAllocateBuffer
                && MsOutlookAddrBookContactSourceService_mapiFreeBuffer
                && MsOutlookAddrBookContactSourceService_mapiLogonEx
                && MsOutlookAddrBookContactSourceService_fBinFromHex
                && MsOutlookAddrBookContactSourceService_freeProws
                && MsOutlookAddrBookContactSourceService_hexFromBin
                && MsOutlookAddrBookContactSourceService_hrAllocAdviseSink
                && MsOutlookAddrBookContactSourceService_hrQueryAllRows)
            {
                MAPIINIT_0 mapiInit = { (ULONG) version, (ULONG) flags };

                // Opening MAPI changes the working directory. Make a backup of
                // the current directory, login to MAPI and restore it
                DWORD dwSize = ::GetCurrentDirectory(0, NULL);
                if (dwSize > 0)
                {
                    LPTSTR lpszWorkingDir
                        = (LPTSTR)::malloc(dwSize*sizeof(TCHAR));
                    DWORD dwResult
                        = ::GetCurrentDirectory(dwSize, lpszWorkingDir);
                    if (dwResult != 0)
                    {
                        MAPISession_lock();
                        hResult
                            = MsOutlookAddrBookContactSourceService_mapiInitialize(
                                    &mapiInit);

                        if(HR_SUCCEEDED(hResult)
                                && MAPISession_getMapiSession() == NULL)
                        {
                        	MsOutlookUtils_logInfo("MAPI logon.");
                            LPMAPISESSION mapiSession = NULL;
                            hResult = MsOutlookAddrBook_mapiLogonEx(
                                    0,
                                    NULL, NULL,
                                    MAPI_EXTENDED
                                        | MAPI_NO_MAIL
                                        | MAPI_USE_DEFAULT,
                                    &mapiSession);
                            if(HR_SUCCEEDED(hResult))
                            {
                            	MsOutlookUtils_logInfo("MAPI logon success.");
                                // Register the notification of contact changed,
                                // created and deleted.
                                MAPINotification_registerNotifyAllMsgStores(
                                        mapiSession);
                            }
                            else
                            {
                            	MsOutlookUtils_logInfo("MAPI logon error.");
                            }
                        }
                        else
                        {
                        	MsOutlookUtils_logInfo("Error calling MAPI init from MAPI library.");
                        }
                        ::SetCurrentDirectory(lpszWorkingDir);
                        MAPISession_unlock();
                    }
                    else
                    {
                        hResult = HRESULT_FROM_WIN32(::GetLastError());
                        MsOutlookUtils_logInfo("Error getting current directory.[1]");
                    }

                    ::free(lpszWorkingDir);
                }
                else
                {
                    hResult = HRESULT_FROM_WIN32(::GetLastError());
                    MsOutlookUtils_logInfo("Error getting current directory.[2]");
                }
            }
            else
            {
            	MsOutlookUtils_logInfo("Cannot get MAPI functions.");
            }
        }
        else
        {
        	MsOutlookUtils_logInfo("Error while loading MAPI library.");
        }
    }
    else
    {
    	MsOutlookUtils_logInfo("ERROR - we won't load MAPI.");
    }

    if (HR_FAILED(hResult))
    {
    	MsOutlookUtils_logInfo("ERROR - in MAPI native init.");
        if(MsOutlookAddrBookContactSourceService_hMapiLib)
        {
        	MsOutlookUtils_logInfo("ERROR - free MAPI library.");
            FreeLibrary(MsOutlookAddrBookContactSourceService_hMapiLib);
            MsOutlookAddrBookContactSourceService_hMapiLib = NULL;
        }
    }

    return hResult;
}

/**
 * Starts the COM server.
 *
 * @return S_OK if eveything was fine. E_FAIL otherwise.
 */
HRESULT MsOutlookAddrBookContactSourceService_MAPIInitializeCOMServer(void)
{
    HRESULT hr = E_FAIL;

    MAPISession_lock();
    MsOutlookUtils_log("Init com server.");
    // Start COM service
    if((hr = MsOutlookAddrBookContactSourceService_startComServer()) == S_OK)
    {
    	MsOutlookUtils_log("COM Server started.");
        // Start COM client
        ComClient_start();
    }
    else
    {
    	MsOutlookUtils_log("Failed to start COM Server.");
    }

    MAPISession_unlock();

    return hr;
}

void MsOutlookAddrBookContactSourceService_MAPIUninitialize(void)
{
    MAPISession_lock();

    LPMAPISESSION mapiSession = MAPISession_getMapiSession();
    if(mapiSession != NULL)
    {
        MAPINotification_unregisterNotifyAllMsgStores();
        mapiSession->Logoff(0, 0, 0);
        mapiSession->Release();
        MAPISession_setMapiSession(NULL);
    }

    if(MsOutlookAddrBookContactSourceService_hMapiLib)
    {
        MsOutlookAddrBookContactSourceService_mapiUninitialize();

        MsOutlookAddrBookContactSourceService_mapiInitialize = NULL;
        MsOutlookAddrBookContactSourceService_mapiUninitialize = NULL;
        MsOutlookAddrBookContactSourceService_mapiAllocateBuffer = NULL;
        MsOutlookAddrBookContactSourceService_mapiFreeBuffer = NULL;
        MsOutlookAddrBookContactSourceService_mapiLogonEx = NULL;
        MsOutlookAddrBookContactSourceService_fBinFromHex = NULL;
        MsOutlookAddrBookContactSourceService_freeProws = NULL;
        MsOutlookAddrBookContactSourceService_hexFromBin = NULL;
        MsOutlookAddrBookContactSourceService_hrAllocAdviseSink = NULL;
        MsOutlookAddrBookContactSourceService_hrQueryAllRows = NULL;
        ::FreeLibrary(MsOutlookAddrBookContactSourceService_hMapiLib);
        MsOutlookAddrBookContactSourceService_hMapiLib = NULL;
    }

    MAPISession_unlock();
}

/**
 * Stops the COM server.
 */
void MsOutlookAddrBookContactSourceService_MAPIUninitializeCOMServer(void)
{
    if(MsOutlookAddrBookContactSourceService_comServerHandle != NULL)
    {
        TerminateProcess(
                MsOutlookAddrBookContactSourceService_comServerHandle,
                1);

        CloseHandle(MsOutlookAddrBookContactSourceService_comServerHandle);
        MsOutlookAddrBookContactSourceService_comServerHandle = NULL;
    }
    ComClient_stop();
}

/**
 * Initializes the plugin but from the COM server point of view: natif side, no
 * java available here.
 *
 * @param version The version of MAPI to load.
 * @param flags The option choosen to load the MAPI to lib.
 * @param deletedMethod A function pointer used as a callback on notification
 * from outlook when a contact has been removed.
 * @param insertedMethod A function pointer used as a callback on notification
 * from outlook when a contact has been added.
 * @param updatedMethod A function pointer used as a callback on notification
 * from outlook when a contact has been modified.
 *
 * @return  S_OK if everything was alright.
 */
HRESULT MsOutlookAddrBookContactSourceService_NativeMAPIInitialize
    (jlong version, jlong flags,
     void * deletedMethod, void * insertedMethod, void * updatedMethod)
{
	MsOutlookUtils_logInfo("MAPI native init.");
    MAPINotification_registerNativeNotificationsDelegate(
            deletedMethod, insertedMethod, updatedMethod);

    return MsOutlookAddrBookContactSourceService_MAPIInitialize(version, flags);
}

void MsOutlookAddrBookContactSourceService_NativeMAPIUninitialize(void)
{
    MAPINotification_unregisterNativeNotificationsDelegate();

    MsOutlookAddrBookContactSourceService_MAPIUninitialize();
}


/**
 * Starts the COM server.
 *
 * @param S_OK if the server started correctly. E_FAIL otherwise.
 */
HRESULT MsOutlookAddrBookContactSourceService_startComServer(void)
{
    int bitness = MAPIBitness_getOutlookBitnessVersion();
    if(bitness != -1)
    {
        // Start COM service
        char applicationName32[] = "jmsoutlookaddrbookcomserver32.exe";
        char applicationName64[] = "jmsoutlookaddrbookcomserver64.exe";
        char * applicationName = applicationName32;
        if(bitness == 64)
        {
            applicationName = applicationName64;
        }
        int applicationNameLength = strlen(applicationName);
        char currentDirectory[FILENAME_MAX - applicationNameLength - 8];
        GetCurrentDirectory(
                FILENAME_MAX - applicationNameLength - 8,
                currentDirectory);
        char comServer[FILENAME_MAX];
        sprintf(comServer, "%s/native/%s", currentDirectory, applicationName);

        STARTUPINFO startupInfo;
        PROCESS_INFORMATION processInfo;
        memset(&startupInfo, 0, sizeof(startupInfo));
        memset(&processInfo, 0, sizeof(processInfo));
        startupInfo.dwFlags = STARTF_USESHOWWINDOW;
        startupInfo.wShowWindow = SW_HIDE;
        char* loggerPath = MsOutlookUtils_getLoggerPath();
        int loggerPathLenght = 0;
        char* comServerWithLogger;
        char* appNameWithLogger;
        char* loggerPathEscaped = NULL;
        if(loggerPath != NULL)
        {
        	int loggerLevel = MsOutlookUtils_getLoggerLevel();
        	char* loggerPathEscaped = (char* ) malloc(strlen(loggerPath) *
        			sizeof(char) * 2);
        	int i = 0;
        	while(*loggerPath != '\0')
        	{
        		*(loggerPathEscaped + i) = *loggerPath;
        		i++;
        		if(*loggerPath == '\\')
        		{
        			*(loggerPathEscaped + i) = '\\';
					i++;
        		}
        		loggerPath++;
        	}
        	*(loggerPathEscaped + i) = '\0';
        	loggerPathLenght = strlen(loggerPathEscaped);
			comServerWithLogger
				= (char*) malloc(
						(FILENAME_MAX + loggerPathLenght) * sizeof(char));
			appNameWithLogger
				= (char*) malloc(
						(FILENAME_MAX + loggerPathLenght) * sizeof(char));
        	sprintf(comServerWithLogger, "%s \"%s\" %d", comServer,
        			loggerPathEscaped, loggerLevel);
        	sprintf(appNameWithLogger, "%s \"%s\" %d", applicationName
        			, loggerPathEscaped, loggerLevel);
        }
        else
        {
        	comServerWithLogger = comServer;
        	appNameWithLogger = applicationName;
        }
        // Test 2 files: 0 for the build version, 1 for the git source version.
        char * serverExec[2];
        serverExec[0] = comServerWithLogger;
        serverExec[1] = appNameWithLogger;
        for(int i = 0; i < 2; ++i)
        {
            // Create the COM server
            if(CreateProcess(
                        NULL,
                        serverExec[i],
                        NULL, NULL, false, 0, NULL, NULL,
                        &startupInfo,
                        &processInfo))
            {
                MsOutlookAddrBookContactSourceService_comServerHandle
                    = processInfo.hProcess;
                MsOutlookUtils_logInfo(serverExec[i]);
                MsOutlookUtils_logInfo("COM Server started successful.[1]");
                if(loggerPath != NULL)
				{
                	free(comServerWithLogger);
                	free(appNameWithLogger);
				}
                MsOutlookUtils_logInfo("COM Server started successful.[2]");
                return S_OK;
            }
        }
        if(loggerPath != NULL)
		{
			free(comServerWithLogger);
			free(appNameWithLogger);
			free(loggerPathEscaped);
		}
    }

    return E_FAIL;
}

BOOL MsOutlookAddrBook_fBinFromHex(LPSTR lpsz, LPBYTE lpb)
{
    return MsOutlookAddrBookContactSourceService_fBinFromHex(lpsz, lpb);
}

void MsOutlookAddrBook_freeProws(LPSRowSet lpRows)
{
    MsOutlookAddrBookContactSourceService_freeProws(lpRows);
}

void MsOutlookAddrBook_hexFromBin(LPBYTE pb, int cb, LPSTR sz)
{
    MsOutlookAddrBookContactSourceService_hexFromBin(pb, cb, sz);
}

void
MsOutlookAddrBook_hrAllocAdviseSink
    (LPNOTIFCALLBACK lpfnCallback, LPVOID lpvContext, LPMAPIADVISESINK*
      lppAdviseSink)
{
    MsOutlookAddrBookContactSourceService_hrAllocAdviseSink(
            lpfnCallback,
            lpvContext,
            lppAdviseSink);
}

HRESULT
MsOutlookAddrBook_hrQueryAllRows
    (LPMAPITABLE lpTable, LPSPropTagArray lpPropTags,
     LPSRestriction lpRestriction, LPSSortOrderSet lpSortOrderSet,
     LONG crowsMax, LPSRowSet* lppRows)
{
    return MsOutlookAddrBookContactSourceService_hrQueryAllRows(
            lpTable,
            lpPropTags,
            lpRestriction,
            lpSortOrderSet,
            crowsMax,
            lppRows);
}

SCODE
MsOutlookAddrBook_mapiAllocateBuffer(ULONG size, LPVOID FAR *buffer)
{
    return
        MsOutlookAddrBookContactSourceService_mapiAllocateBuffer(size, buffer);
}

ULONG
MsOutlookAddrBook_mapiFreeBuffer(LPVOID buffer)
{
    return MsOutlookAddrBookContactSourceService_mapiFreeBuffer(buffer);
}

HRESULT
MsOutlookAddrBook_mapiLogonEx
    (ULONG_PTR uiParam,
    LPTSTR profileName, LPTSTR password,
    FLAGS flags,
    LPMAPISESSION FAR *mapiSession)
{
    HRESULT hResult;

    MAPISession_lock();
    LPMAPISESSION currentMapiSession = MAPISession_getMapiSession();
    if (currentMapiSession != NULL)
        hResult = S_OK;
    else
    {
        hResult
            = MsOutlookAddrBookContactSourceService_mapiLogonEx(
                    uiParam,
                    profileName, password,
                    flags,
                    &currentMapiSession);

        MAPISession_setMapiSession(currentMapiSession);
    }

    if (HR_SUCCEEDED(hResult))
    {
        *mapiSession = currentMapiSession;
    }

    MAPISession_unlock();
    return hResult;
}