从 C DLL/lib 访问 C++ 函数

标签 c++ c dll static-libraries

我拼命尝试访问某些硬件提供的 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字节的问题。

但是看看那些页面:

亚历山大

关于从 C DLL/lib 访问 C++ 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16013052/

相关文章:

c++ - 这会导致对象切片吗?

c++ - YUV420p 到 RGB 的转换改变了 U 和 V 值 - iOS Unity3D

c - scanf 在 C 中不是很宽容

visual-studio - 如何将 dll 放入我的项目 - Visual Studio C# 2010

ios - Unity 5.1.1错误: dll is not allowed to be included or could not be found

c++ - 为什么 C++ 中没有 "initialization"关键字,而在 Delphi 中有?

c++ - DirectX 11 中的视频播放

c++ - Redis命令ERR:参数数量不正确hiredis

c - 打印 C 中结构数组中每个结构成员的函数

java - JNA 通过引用 dll 传递字符串但不返回