函数类型是否为“extern __declspec(dllimport) INT __cdecl”?在 C/C++ 中有意义还是斯威格?

发布于 2024-09-10 09:04:55 字数 489 浏览 2 评论 0原文

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

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

: dllimport) INT __cdecl function(int argument); 是从我试图包装的头文件中获得的。

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

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);

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鹤仙姿 2024-09-17 09:04:55

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

/* 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”。您只需在包含包含要包装的函数的标头之前包含它即可。就像这样:

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

我只是不

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

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

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

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"

where

/* 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...

梦里梦着梦中梦 2024-09-17 09:04:55

只有 INT 会成为麻烦。这是一个宏,您需要像 #include 来定义它。除了常规 int 之外,它非常不可能是其他任何东西。 “extern”是多余的,任何编译器都会忽略它。

Just the INT jumps out as trouble. That's a macro, you'd need like #include <windows.h> to define it. It is very unlikely to be anything else but a regular int. The "extern" is superfluous, any compiler would ignore it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文