函数类型是否为“extern __declspec(dllimport) INT __cdecl"?在 C/C++ 或 SWIG 中有意义吗? [英] Does a function type "extern __declspec(dllimport) INT __cdecl" makes sense in C/C++ or SWIG?

查看:31
本文介绍了函数类型是否为“extern __declspec(dllimport) INT __cdecl"?在 C/C++ 或 SWIG 中有意义吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 SWIG 使用其头文件包装一个 DLL.使用 SWIG 处理接口 .i 文件时出现语法错误.在追踪有问题的行是什么(SWIG 错误消息打印的行号与真正的违规行不匹配)后,我发现最小的不可处理的 SWIG 接口文件是:

I am trying to wrap a DLL, using its header file, through SWIG. I got a syntax error while processing my interface .i file using SWIG. After tracking down what was the offending line (line number of the printed by SWIG error message did not match the true offending line), I found that the minimum non-processable SWIG interface file is:

/* example.i */
%module example
%{
extern  __declspec(dllimport) INT __cdecl function(int argument);
%}
extern  __declspec(dllimport) INT __cdecl function(int argument);

原型 tern extern __declspec(dllimport) INT __cdecl function(int argument); 是从我试图包装的头文件中获得的.

Where the prototype tern extern __declspec(dllimport) INT __cdecl function(int argument); was obtained from the header file I was trying to wrap.

用这种类型声明了几十个函数.如您所见,我没有足够的 C 经验来判断这种类型是否有意义.头文件来自供应商,它使用 Visual C++ 6 编译正常.有人可以澄清我吗?

There are tens of functions declared with this type. As you can see I dont have enough C experience to tell whether this type makes sense. The header file came from the vendor and it compiles OK using Visual C++ 6. Could somebody clarify me?

推荐答案

好的...最终了解了什么是调用约定.问题是应该如何编写 SWIG 接口文件.

OK... ended up learning what calling conventions are. The problem was how the SWIG interface file should be written.

/* example.i */
%module example
%{
int __cdecl foo(void);      // function declarations for the function 
int __stdcall foo2(void);   // you intend to wrap goes in here
%}
int foo(void);          // function you want to wrap goes in here
int foo2(void);         // without the calling convention

在您要包装的函数所在的区域中,不应包含调用约定修饰符(__cdecl、__stdcall 等).但是我不想手动删除调用约定修饰符,特别是因为我不想修改我试图包装的头文件(想象一下供应商发布了新的和不兼容的头文件版本,我将不得不修改头文件一切从头再来).一个解决方案是#define 调用约定修饰符,SWIG 已经提供了一个包含这个#define'tions 的文件,即windows.i".您只需要在包含包含要包装的函数的标头之前包含它.像这样:

In the region where the function you want to wrap goes, should not contain the calling convention modifier (__cdecl,__stdcall,etc). But I did not want to remove the calling convention modifiers by hand, specially because I did not want to modify the header file I was trying to wrap (Imagine the vendor releasing new and incompatible version of header file, I would have to modify the header all over again). A solution is to #define the calling convention modifiers away, SWIG already provides a file that contain this #define'tions, the "windows.i". You just have to include it before including the header containing the functions you want to wrap. Like this:

/* example.i */
%module example
%{
#include "example.h"
%}
%include "windows.i"
%include "example.h"

哪里

/* example.h */
int __cdecl foo(void);
int __stdcall foo2(void);

我只是不明白为什么它默认不这样做.但我对我所发现的感到满意.谢谢各位指点...

I just don't understand why it does not do this by default. But I am happy with what I have found. Thank you guys for the pointers...

这篇关于函数类型是否为“extern __declspec(dllimport) INT __cdecl"?在 C/C++ 或 SWIG 中有意义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆