首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >__stdcall类型胡枝子结构

__stdcall类型胡枝子结构
EN

Stack Overflow用户
提问于 2018-02-26 10:02:10
回答 1查看 332关注 0票数 1

我正在用C++编写我的第一个DLL。使用__declspec(dll_export),我可以使用C包装器在Python和C++上读取它。但是我现在也想在C上阅读它,所以我必须现在添加__stdcall惯例。但我不知道如何将其应用于typedef struct。例如:

Projet.h

代码语言:javascript
复制
#pragma once
#include "Projet_inc.h"

class Projet // before, class _declspec(dll_export) Projet
{
public:
    Projet();
    ~Projet();

    int multiply(int arg1, int arg2);
    int result;
};

Projet_inc.h

代码语言:javascript
复制
#ifdef PROJET_EXPORTS
#  define EXPORT __declspec(dllexport)
#else
#  define EXPORT __declspec(dllimport)
#endif

#define CALLCONV_API __stdcall // before, this line didn't exist

extern "C" // C wrapper
{
    typedef struct Projet Projet; // make the class opaque to the wrapper

    Projet* EXPORT CALLCONV_API cCreateObject(void);
    int EXPORT CALLCONV_API cMultiply(Projet* pDLLobject, int arg1, int arg2);
}

Projet.cpp

代码语言:javascript
复制
#include "stdafx.h"
#include "Projet.h"

Projet::Projet() {}
Projet::~Projet() {}

int Projet::multiply(int arg1, int arg2) {
    result = arg1 * arg2;
    return result;
}

Projet* EXPORT CALLCONV_API  cCreateObject(void)
{
    return new Projet();
}

int EXPORT CALLCONV_API  cMultiply(Projet* pDLLtest, int arg1, int arg2)
{
    if (!pDLLtest)
        return 0;
    return pDLLtest->multiply(arg1, arg2);
}

在Visual 2017上,编译返回(第一行):

代码语言:javascript
复制
dir\projet_inc.h(11) : warning C4229: anachronisme utilisé : modificateurs de données ignorés
dir\projet_inc.h(13) :error C2059: erreur de syntaxe : '__declspec(dllimport)'

MSDN告诉我们,对于C2059错误,我必须首先检查。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-02-26 10:18:07

导出说明符仅适用于函数和变量。调用约定说明符仅适用于函数。所以类型别名(C样式)应该如下所示:

代码语言:javascript
复制
typedef struct Projet_I2M Projet_I2M;

出口规格应列在申报前:

代码语言:javascript
复制
EXPORT Projet * CALLCONV_API cCreateObject(void);

您似乎有意导出C接口,因此您应该防止C++异常跨越语言边界。

extern "C"应有条件地包括在内:

代码语言:javascript
复制
#ifdef __cplusplus
extern "C"
{
#endif

#ifdef __cplusplus
}
#endif
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48985778

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档