简体   繁体   English

MAPI链接静态库(LNK2019:未解析的外部符号)

[英]MAPI Linking Static Library (LNK2019: Unresolved External Symbol)

I try to make use of Microsoft's MAPI (Extended MAPI) using Visual Studio 2017. 我尝试使用Visual Studio 2017来利用Microsoft的MAPI(扩展MAPI)。

So first I created a solution with 3 projects: 所以首先我创建了一个包含3个项目的解决方案:

  1. Visual C++ - Windows Desktop - Static Library Visual C ++-Windows桌面-静态库
  2. Visual C++ - CLR - Class Library Visual C ++-CLR-类库
  3. Visual C# - Classic Windows Desktop - WPF-App Visual C#-经典Windows桌面-WPF应用

(1) is set up as follows: (1)设置如下:

stdafx.h stdafx.h中

#pragma once

#include "targetver.h"

#define WIN32_LEAN_AND_MEAN

// Not auto generated
#define DLLEXPORT __declspec(dllexport)

InstanceManager.h InstanceManager.h

#pragma once

namespace NativeWrapper
{
    class DLLEXPORT InstanceManager
    {
    public:
        InstanceManager();
        int Init();
        void UnInit();
        ~InstanceManager();
    private:
        bool _Initialized;
    };
}

InstanceManager.cpp InstanceManager.cpp

#pragma once
#include "stdafx.h"
#include "InstanceManager.h"

namespace NativeWrapper
{
    InstanceManager::InstanceManager()
    {
        _Initialized = false;
    }

    int InstanceManager::Init()
    {
        if (!_Initialized)
        {
            MAPIINIT init = {
                MAPI_INIT_VERSION,
                MAPI_MULTITHREAD_NOTIFICATIONS
            };
            return MAPIInitialize(&init);
        }
    }

    void InstanceManager::UnInit()
    {
        if (_Initialized)
        {
            MAPIUninitialize();
        }
    }

    InstanceManager::~InstanceManager()
    {
        UnInit();
    }
}

I also downloaded the required headers for MAPI Development and referenced them by adding the corresponding path ( C:\\Office 2010 Developer Resources\\Outlook 2010 MAPI Headers ) to the additional include directories. 我还下载了MAPI开发所需的标头,并通过将相应的路径( C:\\Office 2010 Developer Resources\\Outlook 2010 MAPI Headers )添加到其他包含目录中来引用它们。

(2) is setup as follows: (2)设置如下:

InstanceManager.h InstanceManager.h

#pragma once

#include "..\StaticLib1\NativeInstanceManager.h"

namespace MAPIManaged
{
    ref class InstanceManager
    {
    public:
        InstanceManager();
        ~InstanceManager();
        int Init();
        void UnInit();
    private:
        NativeWrapper::InstanceManager* _NativeObject;
    };
}

InstanceManager.cpp InstanceManager.cpp

#include "stdafx.h"
#include "InstanceManager.h"    

namespace MAPIManaged
{
    InstanceManager::InstanceManager()
    {
        _NativeObject = new NativeWrapper::InstanceManager();
    }

    InstanceManager::~InstanceManager()
    {
        delete _NativeObject;
    }

    int InstanceManager::Init()
    {
        return _NativeObject->Init();
    }

    void InstanceManager::UnInit()
    {
        _NativeObject->UnInit();
    }
}

Also there is a Project-Reference on (1). 在(1)上也有一个Project-Reference。 I did it by Right-Clicking the Project 2 and added Project 1. 我是通过右键单击Project 2并添加Project 1来完成的。

(3) is set up as follows: (3)设置如下:

using System.Windows;

namespace WpfApp1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

           var obj = new MAPIManaged.InstanceManager();
           obj.Init();
        }
    }
}

Also there is a Project-Reference on (2). 在(2)上也有一个Project-Reference。 I did it by Right-Clicking the Project 3 and added Project 2. 我是通过右键单击Project 3并添加Project 2来实现的。

Error 错误

Although I referenced the Static Library I get the following errors: 尽管我引用了静态库,但出现以下错误:

Error 1: MapUninitialize Linking Error 错误1:MapUninitialize链接错误

Error LNK2019 unresolved external symbol "_MAPIUninitialize@0"
    in function ""public: void __thiscall NativeWrapper::InstanceManager::UnInit(void)" (?UnInit@InstanceManager@NativeWrapper@@QAEXXZ)".
ClassLibrary1   PATH\WrapNative\ClassLibrary1\StaticLib1.lib(InstanceManager.obj)   1   

Error 2: MapInitialize Linking Error 错误2:MapInitialize链接错误

    Error LNK2019 unresolved external symbol "_MAPIInitialize@4"
in function ""public: int __thiscall NativeWrapper::InstanceManager::Init(void)" (?Init@InstanceManager@NativeWrapper@@QAEHXZ)".
ClassLibrary1   PATH\WrapNative\ClassLibrary1\StaticLib1.lib(InstanceManager.obj)   1   

You should never statically link to any MAPI functions - you must first find the right MAPI dll and use LoadLibrary / GetProcAddress. 您永远不应静态链接到任何MAPI函数-您必须首先找到正确的MAPI dll并使用LoadLibrary / GetProcAddress。

Look at the MFCMAPI source code for an example on how this is done. 查看MFCMAPI源代码 ,以获取有关如何完成此操作的示例。

Expanding on Dmitry's answer - the only version of mapi32.lib out there is buggy (missing exports, wrong number and types of parameters, etc.) and unsupported by Microsoft for over a decade. 扩大Dmitry的答案-唯一的mapi32.lib版本存在错误(缺少导出,错误的参数数量和类型等),并且十年来一直没有Microsoft的支持。 The MSDN article Link to MAPI functions discusses this in depth and offers a number of options for linking to MAPI, all of which boil down to: 1 - Locate the correct MAPI DLL to load, which is non-trivial, involving calls to FGetComponentPath to locate the proper implementation of MAPI. MSDN文章“ 链接到MAPI函数”对此进行了深入讨论,并提供了许多链接到MAPI的选项,所有这些选项归结为:1-找到正确的MAPI DLL加载,这很简单,涉及到对FGetComponentPath的调用。找到正确的MAPI实现。 In practice, you can simplify this step a bit by just linking to the system mapi32.dll (itself a stub), but you will miss out on exports offered by Outlook's MAPI which are not exposed in the system stub. 实际上,您可以通过仅链接到系统mapi32.dll(本身是存根)来稍微简化此步骤,但是您会错过Outlook MAPI提供的未在系统存根中公开的导出。 2 - Once located, use LoadLibrary and GetProcAddress to link to the exports. 2-找到后,使用LoadLibrary和GetProcAddress链接到导出。

This article also links to the Microsoft recommended MAPIStubLibrary , which encapsulates the logic described above. 本文还链接到Microsoft推荐的MAPIStubLibrary ,其中封装了上述逻辑。 It is preferred that this stub library be used instead of hand rolling location and linking logic. 最好使用此存根库代替手动滚动位置和链接逻辑。

Disclaimer: I am a Microsoft employee, author of MFCMAPI , and co-author of the MAPI Stub Library along with the Outlook development team. 免责声明:我是Microsoft员工,是MFCMAPI的作者,并且与Outlook开发团队一起是MAPI Stub库的合著者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM