0

私はC#でかなり新しいです。私は自分で C++ で Dll を作成しており、その DLL の関数を C# アプリで使用します。

したがって、C++ プロジェクトで関数を宣言するときは、次のようにします。

public static __declspec(dllexport) int captureCamera(int& captureId);

次に、このメソッドを C# アプリにインポートしようとしています。

[DllImport("MyLib.dll")]
public static extern int captureCamera(ref int captureId);

しかし、私には例外があります:

Unable to find an entry point named 'captureCamera' in DLL 'MyLib.dll'.

タスクは、EntryPoint パラメーターを指定せずに dllimport を実行することです。誰でも私を助けることができますか?

4

3 に答える 3

5

public static __declspec(dllexport) int captureCamera(int& captureId);

その方法ですか?関数の場合、staticdllexportは相互に排他的であるため、静的にすることはできません。

そして、名前がめちゃくちゃです。http://en.wikipedia.org/wiki/Microsoft_Visual_C%2B%2B_Name_Manglingを参照してください。マングルされた名前を取得して、それを提供できるDllImport場合 ( EntryPoint=MANGLED_NAME)、動作するはずです。

エクスポートされた関数の定義を含むファイルをリンカーに提供できます.def。その場合、それらの名前は破損しません。

Project.def:

EXPORTS
    captureCamera @1
于 2012-06-25T11:54:51.873 に答える
2

宣言していますか

extern "C" {
    __declspec(dllexport) int captureCamera(int& captureId);
}

C ++コード内-C#はCにのみアクセスでき、C++関数にはアクセスできません。

于 2012-06-25T11:54:36.140 に答える
2

extern "C" ブロックなしで C++ 関数を定義しています。C++ では関数をオーバーロードできる (つまり、さまざまな引数のセットを持つ多数の captureCamera() 関数を作成する) ことができるため、DLL 内の実際の関数名は異なります。これを確認するには、Visual Studio コマンド プロンプトを開き、バイナリ ディレクトリに移動して次を実行します。

dumpbin /exports YourDll.dll

次のようなものが得られます。

Dump of file debug\dll1.dll

File Type: DLL

  Section contains the following exports for dll1.dll

    00000000 characteristics
    4FE8581B time date stamp Mon Jun 25 14:22:51 2012
        0.00 version
           1 ordinal base
           1 number of functions
           1 number of names

    ordinal hint RVA      name

          1    0 00011087 ?captureCamera@@YAHAAH@Z = @ILT+130(?captureCamera@@YAHAAH@Z)

  Summary

        1000 .data
        1000 .idata
        2000 .rdata
        1000 .reloc
        1000 .rsrc
        4000 .text
       10000 .textbss

?captureCamera@@YAHAAH@Zは、指定した引数を実際にエンコードする修飾名です。

他の回答で述べたように、宣言に extern "C" を追加するだけです。

extern "C" __declspec(dllexport) int captureCamera(int& captureId)
{
    ...
}

dumpbin を再実行して、名前が正しいことを再確認できます。

Dump of file debug\dll1.dll

File Type: DLL

  Section contains the following exports for dll1.dll

    00000000 characteristics
    4FE858FC time date stamp Mon Jun 25 14:26:36 2012
        0.00 version
           1 ordinal base
           1 number of functions
           1 number of names

    ordinal hint RVA      name

          1    0 000110B4 captureCamera = @ILT+175(_captureCamera)

  Summary

        1000 .data
        1000 .idata
        2000 .rdata
        1000 .reloc
        1000 .rsrc
        4000 .text
       10000 .textbss
于 2012-06-25T12:27:57.003 に答える