1

C#で使用するためにC++DLLを変換するのに少し問題があります。

それは機能しています..DLLの最初のC++関数は、次のとおりですint subtractInts(int x, int y)。その典型的な本体は問題なく機能します。他のすべての機能は同様に単純であり、テストされています。ただし、私はチュートリアルに従い、C#でそのコードをC ++ DLLとして使用するためにいくつかのファンキーなことを行っています(移植性のため)。

私の手順は次のとおりです。

•C++クラスを作成し、テストして保存します–「class.cpp」ファイルと「class.h」ファイルのみを使用します•Visual Studio 2010でWin32ライブラリプロジェクトを作成し、起動時にDLLを選択します。 C#に公開します。以下のコード

extern "C" __declspec(dllexport) int addInts(int x, int y)
extern "C" __declspec(dllexport) int multiplyInts(int x, int y)
extern "C" __declspec(dllexport) int subtractInts(int x, int y)
extern "C" __declspec(dllexport) string returnTestString()

かなり重要なポイントです。それは、DLLでそれらを外部化した順序です。

次に、以前にこの問題が発生したため、テストとして.. C#プロジェクトで別の方法でそれらを参照しました

   [DllImport("C:\\cppdll\\test1\\testDLL1_medium.dll", CallingConvention = CallingConvention.Cdecl)]

    public static extern int subtractInts(int x, int y);
    public static extern int multiplyints(int x, int y);
    public static extern int addints(int x, int y);
    public static extern string returnteststring();

C#から呼び出されたときに機能する唯一の関数はsubtractIntsです。これは、明らかに最初に参照される関数です。他のすべては、コンパイル時にエラー(以下を参照)を引き起こします。

上記のコードをコメントアウトせずに、これらのすべての関数を外部から参照する場合。multipyInts(int x、int y)で次のエラーが発生します。

Could not load type 'test1DLL_highest.Form1' from assembly 'test1DLL_highest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' because the method 'multiplyints' has no implementation (no RVA).

私はそれがすべてをソートするソートを想像するでしょう。

乾杯。

4

1 に答える 1

5

4つのメソッドすべてにを追加しDllImportAttribute、パスを削除して、ケーシングを修正する必要があります。

[DllImport("testDLL1_medium.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int subtractInts(int x, int y);
[DllImport("testDLL1_medium.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int multiplyInts(int x, int y);
[DllImport("testDLL1_medium.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int addInts(int x, int y);
[DllImport("testDLL1_medium.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern string returnTestString();

また、ネイティブDLLが管理対象アセンブリと同じ場所にある(または通常のDLL検出方法で検出可能)ことを確認してください。

于 2013-03-14T15:45:56.093 に答える