You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#调用含curlpp的C++ DLL时遇System.EntryPointNotFoundException求助

C#调用C++ DLL遇到入口点未找到错误

我尝试在C#中调用自行编写的依赖curlpp库的C++ DLL,最初遇到错误:

System.DllNotFoundException: 'Unable to load DLL 'BLib.dll' or one of its dependencies: The specified module could not be found. (0x8007007E)'

目前已解决DLL加载问题,但出现新错误:

System.EntryPointNotFoundException: Unable to find an entry point named 'testHTTP' in DLL 'BLib.dll'.

C++ DLL代码

#include <sstream>
#include <curlpp/cURLpp.hpp>
#include <curlpp/Easy.hpp>
#include <curlpp/Options.hpp>

#define EXPORT extern "C" __declspec( dllexport )

EXPORT class BLib {
    int testHTTP() {
        try {
            curlpp::Cleanup cleanup;
            {
                curlpp::options::Url authURL(std::string("https://example.com"));
                curlpp::Easy request;
                request.setOpt(authURL);
                request.perform();
                std::ostringstream os;
                curlpp::options::WriteStream ws(&os);
                request.setOpt(ws);
                request.perform();
                os << request;
                return 0;
            }
        }
        catch (curlpp::RuntimeError& e) {
            return 1;
        }
        catch (curlpp::LogicError& e) {
            return 2;
        }
    }
};

C#调用代码

using System;
using System.Runtime.InteropServices;

namespace LibConsole
{
    class Program
    {
        [DllImport("BLib.dll")]
        public static extern int testHTTP();
        static void Main(string[] args)
        {
            Console.WriteLine("Calling cURL");
            Console.WriteLine(testHTTP());
        }
    }
}

问题原因及解决方法

  • 核心问题:extern "C"只能导出全局函数,无法直接导出C++类的成员函数。你把testHTTP定义为类的私有成员函数,既没法被外部访问,也不符合C风格导出的要求。

方案1:将函数改为全局函数导出

修改C++代码,直接导出全局的testHTTP:

#include <sstream>
#include <curlpp/cURLpp.hpp>
#include <curlpp/Easy.hpp>
#include <curlpp/Options.hpp>

#define EXPORT extern "C" __declspec( dllexport )

EXPORT int testHTTP() {
    try {
        curlpp::Cleanup cleanup;
        {
            curlpp::options::Url authURL(std::string("https://example.com"));
            curlpp::Easy request;
            request.setOpt(authURL);
            std::ostringstream os;
            curlpp::options::WriteStream ws(&os);
            request.setOpt(ws);
            request.perform();
            return 0;
        }
    }
    catch (curlpp::RuntimeError& e) {
        return 1;
    }
    catch (curlpp::LogicError& e) {
        return 2;
    }
}

C#代码无需修改,保持原导入即可。

方案2:保留类结构,导出全局调用函数

如果需要保留BLib类的结构,可添加一个全局导出函数来实例化类并调用成员方法:

#include <sstream>
#include <curlpp/cURLpp.hpp>
#include <curlpp/Easy.hpp>
#include <curlpp/Options.hpp>

#define EXPORT extern "C" __declspec( dllexport )

class BLib {
public: // 必须将成员函数设为public,否则外部无法访问
    int testHTTP() {
        try {
            curlpp::Cleanup cleanup;
            {
                curlpp::options::Url authURL(std::string("https://example.com"));
                curlpp::Easy request;
                request.setOpt(authURL);
                std::ostringstream os;
                curlpp::options::WriteStream ws(&os);
                request.setOpt(ws);
                request.perform();
                return 0;
            }
        }
        catch (curlpp::RuntimeError& e) {
            return 1;
        }
        catch (curlpp::LogicError& e) {
            return 2;
        }
    }
};

EXPORT int CallBLibTestHTTP() {
    BLib instance;
    return instance.testHTTP();
}

对应的C#代码需要修改导入的函数名:

[DllImport("BLib.dll")]
public static extern int CallBLibTestHTTP();

// 在Main中调用
Console.WriteLine(CallBLibTestHTTP());

额外注意事项

  • 确保C++ DLL和C#项目的编译平台一致(x86/x64),避免位数不匹配。
  • 确认curlpp的依赖库(如libcurl.dll等)已放置在C#程序的运行目录下,防止依赖缺失。

内容的提问来源于stack exchange,提问作者XTREME

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 19:47:59