C ++ CLR将C ++ DLL与C#应用程序桥接

时间:2018-09-28 14:29:25

标签: c# c++ log4net

我正在尝试遵循以下示例:http://blogs.artinsoft.net/Mrojas/archive/2008/06/19/Log4NET-for-C++.aspx

Log4Net在c ++ dll中运行。

我创建了一个空的CLR项目,将输出类型更改为lib,并添加了以下代码:

// Bridge.cpp

#include <atlstr.h>

using namespace System;

/// <summary>

/// Example of how to simply configure and use log4net

/// </summary>
ref class LoggingExample
{
private:
    // Create a logger for use in this class
    static log4net::ILog^ log = log4net::LogManager::GetLogger("LoggingExample"); static LoggingExample()
    {
        log4net::Config::BasicConfigurator::Configure();
    }


public:static void ReportMessageWarning(char* msg)
{
    String^ data = gcnew String(msg);
    log->Warn(data);
}
       static void ReportMessageError(char* msg)
       {
           String^ data = gcnew String(msg);
           log->Error(data);
       }

       static void ReportMessageInfo(char* msg)
       {
           String^ data = gcnew String(msg);
           log->Info(data);
       }static void ReportMessageDebug(char* msg)
       {
           String^ data = gcnew String(msg);
           log->Debug(data);
       }
};

extern "C"
{

    _declspec(dllexport) void ReportMessageWarning(char* msg)
    {
        LoggingExample::ReportMessageWarning(msg);
    }
    _declspec(dllexport) void ReportMessageError(char* msg)
    {
        LoggingExample::ReportMessageError(msg);
    }

    _declspec(dllexport) void ReportMessageInfo(char* msg)
    {
        LoggingExample::ReportMessageInfo(msg);
    }

    _declspec(dllexport) void ReportMessageDebug(char* msg)
    {
        LoggingExample::ReportMessageDebug(msg);
    }
}

我创建了一个c ++控制台应用程序项目,并添加了以下代码:

#include "stdafx.h"
#include <iostream>
#include <atlstr.h>


extern "C"
{

    //log4net from the wrapper lib
    _declspec(dllimport) void ReportMessageWarning(char* msg);

    __declspec(dllexport) void RunTest() 
    {
        std::string message = "hey it seems it is working";
        ReportMessageWarning(_strdup(message.c_str()));
    }


}

这将是从另一个C#应用程序调用的c ++ dll。 我已经编译了clr lib,并将其链接到c ++应用程序的依赖项中。

但是,c ++ dll无法按原样编译,这给了我

Error LNK2001 unresolved external symbol __imp_ReportMessageWarning

我在这里错过了什么?

0 个答案:

没有答案