私は C# は初めてですが、C++ を幅広く使用してきました。C# から呼び出す必要がある C++ 関数があります。SO からのいくつかの回答といくつかのグーグルを読んだ後、関数への純粋な C インターフェイスを作成する必要があると結論付けました。私はこれを行いましたが、C# から呼び出す方法についてはまだ混乱しています。
C++ の関数は次のようになります。
int processImages(
    std::string& inputFilePath,                      // An input file
    const std::vector<std::string>& inputListOfDirs, // Input list of dirs
    std::vector<InternalStruct>& vecInternalStruct,  // Input/Output struct
    std::vector<std::vector< int > >& OutputIntsForEachFile,
    std::vector< std::vector<SmallStruct> >& vecVecSmallStruct, // Output
    int verboseLevel
    );
同じ関数を C に変換すると、次のようになります。
int processImagesC(
    char* p_inputFilePath,               // An input file
    char** p_inputListOfDirs,            // Input list of dirs
    size_t* p_numInputDirs,              // Indicating number of elements
    InternalStruct* p_vecInternalStruct, // Input/Output struct
    size_t* p_numInternalStructs, 
    int** p_OutputIntsForEachFile,       // a 2d array each row ending with -1
    size_t* p_numOutputIntsForEachFile //one number indicating its number of rows
    SmallStruct** p_vecVecSmallStruct,   // Output
    size_t* p_numInVecSmallStruct,
    int verboseLevel
    );
これは、このアドバイスに基づいています。
ここで、これを C# から呼び出す必要がありますが、これが混乱の原因です。私は構造を変換するために最善を尽くしました。
C# コードは次のようになります。
[DllImport(
    @"C:\path\to\cppdll.dll", CallingConvention=CallingConvention.Cdecl, 
    EntryPoint="processImagesC", SetLastError=true)]
[return: MarshalAs(UnmanagedType.I4)]
unsafe public static extern int processImagesC(
    String inputFilePath,
    String[] inputListOfDirs,
    ref uint numInputListOfDirs,
    // Should I use ref InternalStruct * vecInternalStruct?
    ref InternalStruct[] vecInternalStruct, 
    ref uint numInternalStruct,
    // Or ref int[] or ref int[][] or int[][]?
    ref int[][] OutputIntsForEachFile, 
    ref uint numOutputIntsForEachFile,
    // again, ref ..[], [][], or ref [][]?
    ref SmallStruct[][] vecVecSmallStruct, 
    int verboseLevel
);
C/C++ コード内で行われるすべての出力変数 (ポインター) のメモリ割り当てがあります。これはおそらく、コードを安全でないと宣言する必要があることを意味しますね。
メモリの解放をどのように処理しますか? C/C++ によって割り当てられたオブジェクト/配列の割り当てを解除する別の API (関数) を作成する必要がありますか?
C++ コードは標準に準拠し、プラットフォームに依存しない必要があるため、Windows 固有のものを挿入することはできません。
誰かがこれを理解して答えを提供するか、少なくとも私を正しい方向に向けてくれることを願っています.