2
votes

I'm desperately trying to access a DLL supplied with some hardware. I have the DLL, the LIB file and the headers for the DLL.

I first tried using C# but failed due to the large data structures that are passed around. I got the functions called but the values in the structures that are modified are not correct.

I then thought about writing a wrapper class in C++ and use this as a library for C#. But again here I can call the function but if I tested an signed long that is passed it is "1072955392l" instead of "-1l".

I then just renamed the cpp file to ".c" and compiled it again. Now I get the correct value.

Are there some differences in the datatypes from C to C++?

The functions for the LIb in the supplied include file are declared like that:

_declspec (dllimport) long ResetControl(Registers* regs);

I compile using VS2013:

cl test.cpp /link test.lib
cl test.c /link test.lib

The cpp file and c file are the same unless I needed to include #include for the cpp and wrap the dll include header in

extern "C"
{
    #include "test.h"
}

The test.c file looks like:

//#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;
}

The C++ file:

#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;
}

Both compile, but the result of printing Regs.Products is different:

C: -1l
C++: 1072955392l

What am I doing wrong here?

1
Nothing that I can see, what does the "test.h" file look like? - john
You could check the assembly output (cl /Fa) of both C and C++ versions, then compare them. - Joulukuusi
Also, if you're using long integers, just to be sure you should at least use the %ld format before the l instead of just %d. - Medinoc
checking the assembly output I recognized that sizes for the structure were different when compiling with C or C++. The size of BOOL for the Lib seems to be a 4 byte type. Now I got it working with C++. Using that knowledge I fired up the C# project again and corrected the sizes of the structures. But still no look here :( - thefloe

1 Answers

0
votes

I saw somewhere that there is a matter of aligning to 4 bytes while compiling the client of the DLL.

However have a look to those pages:

Alexandre