我拼命尝试访问某些硬件提供的 DLL。我有 DLL、LIB 文件和 DLL 的 header 。
我首先尝试使用 C#,但由于传递的大型数据结构而失败。我调用了函数,但修改的结构中的值不正确。
然后我考虑用 C++ 编写一个包装类并将其用作 C# 的库。但在这里我可以再次调用该函数,但如果我测试了传递的带符号长,则它是“1072955392l”而不是“-1l”。
然后我只是将 cpp 文件重命名为“.c”并再次编译它。现在我得到了正确的值。
从 C 到 C++ 的数据类型是否存在一些差异?
提供的包含文件中的 LIb 函数声明如下:
_declspec (dllimport) long ResetControl(Registers* regs);
我使用 VS2013 编译:
cl test.cpp /link test.lib
cl test.c /link test.lib
cpp 文件和 c 文件是相同的,除非我需要为 cpp 包含 #include 并将 dll include header 包装在
extern "C"
{
#include "test.h"
}
test.c 文件如下所示:
//#include <windows.h>
#include <stdlib.h>
#include <malloc.h>
#include <stdio.h>
#include <math.h>
#include "test.h"
int main (int argc, char **argv)
{
Registers Regs;
Reset (&Regs);
printf ("Value: %dl\n\r", Regs.Product);
return 0;
}
C++ 文件:
#include <windows.h>
#include <stdlib.h>
#include <malloc.h>
#include <stdio.h>
#include <math.h>
extern "C"
{
#include "test.h"
}
int main (int argc, char **argv)
{
Registers Regs;
Reset (&Regs);
printf ("Value: %dl\n\r", Regs.Product);
return 0;
}
都编译通过,只是打印Regs.Products的结果不同:
C: -1l
C++: 1072955392l
我在这里做错了什么?
最佳答案
我在某处看到,在编译DLL的客户端时,有一个对齐到4字节的问题。
但是看看那些页面:
- http://www.codeproject.com/Articles/21/Beginner-s-Tutorial-Calling-Visual-Basic-ActiveX-D
- http://www.codeproject.com/Articles/6242/Step-by-Step-Calling-C-DLLs-from-VC-and-VB-Part
亚历山大
关于从 C DLL/lib 访问 C++ 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16013052/