0

Cで作成されたプログラムでいくつかの関数を使用する必要があります。テストするために、次を定義しました。

これは私の .h ファイルです:

namespace amt
{
    class AMT_EXPORT FaceRecognition
    {   
        public:
            std::string amt_test_string(std::string in);
    };  
};

これは私の .cpp ファイルです:

#include <memory.h>
#include <string>
#include <iostream>
#include <fstream>
#include "api_shared.h"
#include <sys/stat.h>

using namespace std;

std::string amt::FaceRecognition::amt_test_string (std::string in)
{
    std::string s="in: "+in;
    std::cout<<s<<std::endl;

    return s;
}

私はこのようなメソッドを呼び出そうとしています:

 const string str = "C:\\minimal.dll";
[DllImport(str)]
public static extern string amt_test_string(string input);
static void Main(string[] args)
{
    string myinput = "12";
    string myoutput = "";
    myoutput = amt_test_string(myinput);
    Console.WriteLine(myoutput);
    Console.Read();

}

しかし、amt_test_string という名前のエントリ ポイントが見つからないというエラーが表示されます。私はCところで初心者です

4

1 に答える 1

3

これは C DLL ではなく、C++ DLL です。C と C++ は同じ言語ではありません。特に、C++ には名前マングリングがあるため、DLL にエクスポートされる関数名は装飾されます。

そのため、DLL に C++ エクスポートを含めないようにすることを強くお勧めします。C エクスポートのみを使用する場合、シンボル名は予測可能 (つまり、C++ コンパイラが名前を装飾する方法の特定の詳細に依存しない) であり、C++ 標準ライブラリの実装方法など、実行時の違いについて心配する必要はありません。std::string.

DLL エクスポートは次のようにすることをお勧めします。

extern "C"  // This says that any functions within the block have C linkage
{

// Input is 'in', output gets stored in the 'out' buffer, which must be 'outSize'
// bytes long
void DLLEXPORT amt_FaceRecogniztion_amt_test_string(const char *in, char *out, size_t outSize)
{
    ...
}

}

このインターフェイスは、特定のライブラリの実装に依存していませんstd::string。C# は、char*パラメーターを C 文字列として変換する方法を知っています。ただし、出力の大きさの上限を把握し、適切なサイズのバッファを渡す必要があるため、メモリ管理はより複雑です。

于 2013-04-10T20:36:50.097 に答える