2

ハードウェアに付属している DLL に必死にアクセスしようとしています。DLL、LIB ファイル、および DLL のヘッダーがあります。

最初に C# を使用してみましたが、渡されるデータ構造が大きいために失敗しました。関数を呼び出しましたが、変更された構造体の値が正しくありません。

次に、C++ でラッパー クラスを作成し、これを C# のライブラリとして使用することを考えました。しかし、ここでも関数を呼び出すことができますが、渡された符号付き long をテストすると、「-1l」ではなく「1072955392l」です。

次に、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 に #include をインクルードして dll インクルード ヘッダーをラップする必要がない限り、cpp ファイルと c ファイルは同じです。

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

ここで何が間違っていますか?

4

1 に答える 1

0

DLL のクライアントをコンパイルする際に 4 バイトにアラインする問題があることをどこかで見ました。

ただし、これらのページをご覧ください。

アレクサンドル

于 2014-03-14T12:20:10.307 に答える