首页 > 解决方案 > 从 C 定义的接口编写 C++ DLL

问题描述

我得到了一个 C 头文件,它充当编写 DLL 的接口。然后,可执行文件动态读取该 DLL 以进行测试。我是 DLL 创建世界的新手,我正在努力让可执行文件将我的 DLL 识别为具有它期望的入口点。不幸的是,可执行文件没有告诉我为什么我的 DLL 无效。我曾经使用工具来确保我已经使用正确的名称导出了我的函数,但我猜测我的函数签名签名使它们不兼容。我还确保按照自述文件中的说明构建 x64。

C 接口头文件

#include <stdint.h>
#pragma pack(push, 1)

struct Point
{
    int8_t id;
    int32_t rank;
    float x;
    float y;
};

struct Rect
{
    float lx;
    float ly;
    float hx;
    float hy;
};
#pragma pack(pop)

struct SearchContext;

typedef SearchContext* (__stdcall* T_create)(const Point* points_begin, const Point* points_end);

typedef int32_t (__stdcall* T_search)(SearchContext* sc, const Rect rect, const int32_t count, Point* out_points);

typedef SearchContext* (__stdcall* T_destroy)(SearchContext* sc);

我的 C++ 头文件

#pragma once
#include <cstdint>
#pragma pack(push, 1)

struct Point
{
    int8_t id;
    int32_t rank;
    float x;
    float y;
};

struct Rect
{
    float lx;
    float ly;
    float hx;
    float hy;
};
#pragma pack(pop)

struct SearchContext
{
    float value;
    float minCoverage, maxCoverage;
    SearchContext* leftTree;
    SearchContext* rightTree;
    SearchContext* yTree;
    Point* point;
};

extern "C" __declspec(dllexport) SearchContext* create(const Point* points_begin, const Point* points_end);
extern "C" __declspec(dllexport) int32_t search(SearchContext * sc, const Rect rect, const int32_t count, Point * out_points);
extern "C" __declspec(dllexport) SearchContext* destroy(SearchContext * sc);

提前感谢您的帮助!

标签: c++cdll

解决方案


推荐阅读