来自C DLL / lib的C ++访问函数

时间:2013-04-15 10:35:09

标签: c++ c dll static-libraries

我拼命想要访问一些硬件提供的DLL。我有DLL,LIB文件和DLL的标题。

我首先尝试使用C#但由于传递的大型数据结构而失败。我得到了调用的函数,但修改后的结构中的值不正确。

然后我考虑用C ++编写一个包装类,并将其用作C#的库。但在这里我可以调用该函数但是如果我测试了一个已经传递的有符号长整数,则它是“1072955392l”而不是“-1l”。

然后我将cpp文件重命名为“.c”并再次编译。现在我得到了正确的价值。

从C到C ++的数据类型是否存在一些差异?

提供的include文件中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头

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

我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)