0

私は関数がこのように見えるネイティブDLLに関数を持っています

int GetActiveNames(char* names[]);

文字列/文字の配列を返す上記の関数に対して C# で PInvoke ラッパー メソッドを作成するにはどうすればよいですか?


もっと分かりやすく..

P/Invoke の方法を試してみたところ、次のエラーAttempted to read or write protected memory が表示されました。これは多くの場合、他のメモリが破損していることを示しています。.

簡単にするために、実際の C 関数のシグネチャを取り除いただけです。ただし、完全な C 関数の署名と関数のパラメーターの説明は次のとおりです。

 int OpalGetActiveProjects(unsigned short allocatedProjects,
unsigned short *numProjects,
unsigned short allocatedModels,
unsigned short *numModels,
unsigned short allocatedProjectsPathLen,
unsigned short *maxProjectPathLen,
unsigned short allocatedModelPathLen,
unsigned short *maxModelPathLen,
unsigned short allocatedMachineNameLen,
unsigned short *maxMachineNameLen,
unsigned short allocatedIpLen,
unsigned short *maxIpLen,
char *projectPaths[],
unsigned short instanceIDs[],
char *machineNames[],
char *controllerIPs[],
char *modelPaths[],
unsigned short numModelsPerProject[]
unsigned short modelIDs[],
unsigned short modelStates[]);

この関数は、アクティブなプロジェクトのリストを返します。この関数は、プロジェクトごとに、構成ファイルのフル パス、このプロジェクトを開いたマシンの名前と IP アドレス、現在のプロジェクト セッションのインスタンス ID、およびこのプロジェクトに属するモデルのリストを返します。モデルごとに、パス、ID、および状態が返されます。割り当てられたストレージがいずれかの引数に対して小さすぎる場合、リストは返されませんが、numProjects、numModels、maxProjectPathLen、maxModelPathLen、maxMachineNameLen、および maxIpLen の値が設定されます。戻り値は E2BIG です。アプリケーションはこの事実を利用して、最初の呼び出しで長さ 0 を指定し、必要なストレージを割り当て、2 番目の呼び出しを発行して情報を取得できます。


入力パラメータ


allocatedProjects : 呼び出し元によってパス、マシン名、IP アドレス、およびインスタンス ID 用にストレージが割り当てられたプロジェクトの数。 allocatedModels : 名前、パス、およびインスタンス ID 用に呼び出し元によってストレージが割り当てられたモデルの数。 allocatedProjectPathLen : プロジェクト パスを受け取るために 割り当てられたストレージの長さ。 allocatedModelPathLen : モデル パスを受け取るために 割り当てられたストレージの長さ


出力パラメータ


numProjects : API がアクティブなプロジェクトの実際の数を格納する場所へのポインター。 numModels: API がすべてのアクティブなプロジェクトのモデルの実際の合計数を格納する場所へのポインター。 maxProjectPathLen: API が最長のプロジェクト パスの長さを格納する場所へのポインター。 maxModelPathLen: API が最長のモデル パスの長さを格納する場所へのポインター。 maxMachineNameLen: API が最長のマシン名の長さを格納する場所へのポインター。 maxIpLen: API が最長の IP アドレスの長さを格納する場所へのポインター。 projectPaths: API がアクティブなプロジェクトの構成ファイルのパスを格納する文字列の配列。 instanceIDs: API がアクティブなプロジェクトのインスタンス ID を格納する配列。 machineNames: API がマシン名を格納する配列。 controllerIPs: API が IP アドレスを格納する配列。 modelPaths: API がすべてのアクティブなプロジェクトのモデル パスを格納する配列。 numModelsPerProject: API が各プロジェクトのモデル数を格納する配列。これらの値を使用して、どのモデルがどのプロジェクトに属しているかを判断します。 modelIDs: API がすべてのアクティブなプロジェクトのモデル ID を格納する配列。 modelStates: API がすべてのアクティブなプロジェクトのモデルの状態を格納する配列。

また、OpalGetActiveProjects() 関数を使用して projectPaths、machineNames などを取得する C プログラムもあります。

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>

// Header for getcwd() is platform-dependent
#if defined(WIN32)
#include <direct.h>
#include <windows.h>
#elif defined(__linux__)
#include <unistd.h>
#define _MAX_PATH 256
 #endif
 #include "OpalApi.h"

void    PrintError(int rc, char *funcName);

 #define FALSE 0

 #define TRUE 1

 int main (int argc, char* argv[])
 {
     char                                **projectPath, **modelPath;
     char                                *projectPaths, *modelPaths;
     char                                **loaderMachineName, **controllerIp;
     char                                *loaderMachineNames, *controllerIps;
     int                                         i, rc, projectIdx;
     unsigned short                  *instId;
     int                             *instId2;
     int                                 * modelIds;
     unsigned short             *numModelsPerProject;
     unsigned short             *modelId;
     unsigned short             *modelState;
     unsigned short             allocProjects, numProjects;
     unsigned short             allocModels, numModels;
     unsigned short             allocProjectPathLen, maxProjectPathLen;
     unsigned short             allocModelPathLen, maxModelPathLen;
     unsigned short             allocMachineLen, maxMachineLen;
     unsigned short             allocIpLen, maxIpLen;

     // Obtenir la taille des données.
     allocProjects              =allocModels                 = 0;
     allocProjectPathLen = allocModelPathLen = 0;
     allocMachineLen         = allocIpLen            = 0;

     rc = OpalGetActiveProjects( 0, &allocProjects,
                                                             0, &allocModels,
                                                             0, &allocProjectPathLen,
                                                             0, &allocModelPathLen,
                                                             0, &allocMachineLen,
                                                             0, &allocIpLen,
                                                             NULL, NULL, NULL, NULL,
                                                             NULL, NULL, NULL, NULL );

     if ((rc != 0) && (rc != E2BIG))
     {
         PrintError(rc, "OpalGetActiveProject");
            printf(" -        alGetActiveProjects error output !!!\n");
            printf("\t numProjects - %i \n", allocProjects);
            printf("\t numModels - %i \n", allocModels);
            printf("\t maxProjectPathLen - %i \n", allocProjectPathLen);
            printf("\t maxModelPathLen - %i \n", allocModelPathLen);
            //printf("\t maxMachineNameLen - %i \n", maxMachineNameLen);
            printf("\t maxIpLen - %i \n", allocIpLen);
            return (rc);
             // Erreur
             //return returnInstance;
     }

     projectPath = (char **)_alloca(allocProjects * sizeof(*projectPath));
     projectPaths = (char *)_alloca(allocProjects * (allocProjectPathLen + 1) * sizeof(*projectPaths));

     loaderMachineName = (char **)_alloca(allocProjects * sizeof(*loaderMachineName));
     loaderMachineNames = (char *)_alloca(allocProjects * (allocMachineLen + 1) * sizeof(*loaderMachineNames));

     controllerIp = (char **)_alloca(allocProjects * sizeof(*controllerIp));
     controllerIps = (char *)_alloca(allocProjects * (allocIpLen + 1) * sizeof(*controllerIps));

     numModelsPerProject = (unsigned short*)_alloca(allocProjects * sizeof(*numModelsPerProject));

     modelPath = (char **)_alloca(allocModels * sizeof(*modelPath));
     modelPaths = (char *)_alloca(allocModels * (allocModelPathLen + 1) * sizeof(*modelPaths));

     for (i = 0; i < allocProjects; ++i)
     {
             projectPath[i]                          = &projectPaths[i * (allocProjectPathLen + 1)];
             loaderMachineName[i]        = &loaderMachineNames[i * (allocMachineLen + 1)];
             controllerIp[i]                 = &controllerIps[i * (allocIpLen + 1)];
     }

     for (i = 0; i < allocModels; ++i)
     {
             modelPath[i] = &modelPaths[i * (allocModelPathLen + 1)];
     }

     instId = (unsigned short *)_alloca(allocProjects * sizeof(*instId));
     instId2 = (int *)_alloca(allocProjects * sizeof(*instId2));
     modelId = (unsigned short*)_alloca(allocModels * sizeof(*modelId));
     modelState = (unsigned short *)_alloca(allocModels * sizeof(*modelState));

     rc = OpalGetActiveProjects( allocProjects, &numProjects,
                                                             allocModels, &numModels,
                                                             allocProjectPathLen, &maxProjectPathLen,
                                                             allocModelPathLen, &maxModelPathLen,
                                                             allocMachineLen, &maxMachineLen,
                                                             allocIpLen, &maxIpLen,
                                                             projectPath, instId,
                                                             loaderMachineName, controllerIp, modelPath,
                                                             numModelsPerProject, modelId, modelState);
     printf(" - OpalGetActiveProjects output !!!\n");
            printf("\t numProjects - %i \n", allocProjects);
            printf("\t numModels - %i \n", allocModels);
            printf("\t maxProjectPathLen - %i \n", allocProjectPathLen);
            printf("\t maxModelPathLen - %i \n", allocModelPathLen);
            //printf("\t maxMachineNameLen - %i \n", maxMachineNameLen);
            printf("\t maxIpLen - %i \n", allocIpLen);

    for(i=0; i<numProjects; i++)
    {
        printf("\t \t projectPath[%d] = %s \n", i, *(projectPath +i));
        printf("\t \t instId[%d] = %d \n", i, *(instId + i));
        printf("\t \t loaderMachineName[%d] = %s \n", i, *(loaderMachineName + i));
        printf("\t \t controllerIp[%d] = %s \n", i, *(controllerIp + i));
        printf("\t \t numModelsPerProject[%d] = %d \n", i, *  (numModelsPerProject +i));
    }

    for(i=0; i<numModels; i++)
    {
        printf("\t \t modelPath[%d] = %s \n", i, *(modelPath+i));
        printf("\t \t modelId[%d] = %d \n", i, *(modelId +i));
        printf("\t \t modelState[%d] = %d \n", i, *(modelState+i));
    }
    OpalDisconnect();
    getchar();
 }


 void   PrintError(int rc, char *funcName)
  {
char            buf[512];
unsigned short  len;

OpalGetLastErrMsg(buf, sizeof(buf), &len);
printf("Error !!! \n %s (code %d) from %s\n", buf, rc, funcName);
  }
4

3 に答える 3

1

この署名は奇妙すぎて C# に変換できません。TlbImp.exe がそれを何に変換するかを調べてみたところ、次のように表示されました。

TlbImp : 警告 TI3015 : 'Marshal2.IMarshal3.GetActiveNames' の引数の少なくとも 1 つをランタイム マーシャラーでマーシャリングできません。したがって、このような引数はポインターとして渡され、操作には安全でないコードが必要になる場合があります。

可能であれば、BSTR を含む SAFEARRAY を返すように関数を変更することをお勧めします。C# 側では、(Object 型の) 要素を String にキャストできる System.Array が表示されます。

GetActiveNames を COM クラスのメソッドにすることもお勧めします。P/Invoke は原始的すぎて安全ではありません。

于 2012-09-19T13:40:57.557 に答える
0

うわー、それは本当に恐ろしいAPIです。遠くからでも他に何かありませんか...ああ、わかりません...まともな、あなたが使用できるのですか?しかし、あなたが本当にそれを望むなら...

using System;
using System.Runtime.InteropServices;

public class ActiveProjects
{
    public string[] ProjectPaths { get; set; }
    public ushort[] InstanceIds { get; set; }
    public string[] MachineNames { get; set; }
    public string[] ControllerIps { get; set; }
    public string[] ModelPaths { get; set; }
    public ushort[] NumberOfModelsPerProject { get; set; }
    public ushort[] ModelIds { get; set; }
    public ushort[] ModelStates { get; set; }

    [DllImport("<the DLL>")]
    unsafe private static int OpalGetActiveProjects(
        ushort allocatedProjects,
        ushort* numProjects,
        ushort allocatedModels,
        ushort* numModels,
        ushort allocatedProjectsPathLen,
        ushort* maxProjectPathLen,
        ushort allocatedModelPathLen,
        ushort* maxModelPathLen,
        ushort allocatedMachineNameLen,
        ushort* maxMachineNameLen,
        ushort allocatedIpLen,
        ushort* maxIpLen,
        sbyte** projectPaths,
        ushort* instanceIDs,
        sbyte** machineNames,
        sbyte** controllerIPs,
        sbyte** modelPaths,
        ushort* numModelsPerProject,
        ushort* modelIDs,
        ushort* modelStates
        );

    public void GetActiveProjects()
    {
        unsafe
        {
            ushort numberOfProjects = 0;
            ushort numberOfModels = 0;
            ushort maxProjectPathLength = 0;
            ushort maxModelPathLength = 0;
            ushort maxMachineNameLength = 0;
            ushort maxIpLength = 0;

            int result = OpalGetActiveProjects(
                0,
                &numberOfProjects,
                0,
                &numberOfModels,
                0,
                &maxProjectPathLength,
                0,
                &maxModelPathLength,
                0,
                &maxMachineNameLength,
                0,
                &maxIpLength,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null
                );

            if (result != 0 && result != 123)
                throw new Exception("Error getting active projects");

            sbyte** projectPaths = null;
            ushort* instanceIds = null;
            sbyte** machineNames = null;
            sbyte** controllerIps = null;
            sbyte** modelPaths = null;
            ushort* numberOfModelsPerProject = null;
            ushort* modelIds = null;
            ushort* modelStates = null;

            try
            {
                projectPaths = (sbyte**)Marshal.AllocHGlobal(numberOfProjects * IntPtr.Size).ToPointer();
                for (int i = 0; i < numberOfProjects; ++i)
                    projectPaths[i] = null;
                for (int i = 0; i < numberOfProjects; ++i)
                    projectPaths[i] = (sbyte*)Marshal.AllocHGlobal(maxProjectPathLength);

                instanceIds = (ushort*)Marshal.AllocHGlobal(numberOfProjects * 2).ToPointer();

                machineNames = (sbyte**)Marshal.AllocHGlobal(numberOfProjects * IntPtr.Size).ToPointer();
                for (int i = 0; i < numberOfProjects; ++i)
                    machineNames[i] = null;
                for (int i = 0; i < numberOfProjects; ++i)
                    machineNames[i] = (sbyte*)Marshal.AllocHGlobal(maxMachineNameLength).ToPointer();

                controllerIps = (sbyte**)Marshal.AllocHGlobal(numberOfProjects * IntPtr.Size).ToPointer();
                for (int i = 0; i < numberOfProjects; ++i)
                    controllerIps[i] = null;
                for (int i = 0; i < numberOfProjects; ++i)
                    controllerIps[i] = (sbyte*)Marshal.AlloHGlobal(maxIpLength).ToPointer();

                modelPaths = (sbyte**)Marshal.AlloHGlobal(numberOfModels * IntPtr.Size).ToPointer();
                for (int i = 0; i < numberOfProjects; ++i)
                    modelPaths = null;
                for (int i = 0; i < numberOfProjects; ++i)
                    modelPaths = (sbyte*)Marshal.AllocHGlobal(maxModelPathLength).ToPointer();

                numberOfModelsPerProject = (ushort*)Marshal.AlloHGlobal(numberOfProjects * 2).ToPointer();
                modelIds = (ushort*)Marshal.AllocHGlobal(numberOfModels * 2).ToPointer();
                modelStates = (ushort*)Marshal.AllocHGlobal(numberOfModels * 2).ToPointer();

                ushort numberOfProjects2 = 0;
                ushort numberOfModels2 = 0;
                ushort maxProjectPathLength2 = 0;
                ushort maxModelPathLength2 = 0;
                ushort maxMachineNameLength2 = 0;
                ushort maxIpLength2 = 0;

                OpalGetActiveProjects(
                    numberOfProjects,
                    &numberOfProjects2,
                    numberOfModels,
                    &numberOfModels2,
                    maxProjectPathLength,
                    &maxProjectPathLength2,
                    maxModelPathLength,
                    &maxModelPathLength2,
                    maxMachineNameLength,
                    &maxMachineNameLength2,
                    maxIpLength,
                    &maxIpLength2,
                    projectPaths,
                    instanceIds,
                    machineNames,
                    controllerIps,
                    modelPaths,
                    numberOfModelsPerProject,
                    modelIds,
                    modelStates
                    );

                ProjectPaths = new string[numberOfProjects2];
                for (int i = 0; i < numberOfProjects2; ++i)
                    ProjectPaths[i] = new string(projectPaths[i]);

                InstanceIds = new ushort[numberOfProjects2];
                for (int i = 0; i < numberOfProjects2; ++i)
                    InstanceIds[i] = instanceIds[i];

                MachineNames = new string[numberOfProjects2];
                for (int i = 0; i < numberOfProjects2; ++i)
                    MachineNames[i] = new string(machineNames[i]);

                ControllerIps = new string[numberOfProjects2];
                for (int i = 0; i < numberOfProjects2; ++i)
                    ControllerIps[i] = new string(controllerIps[i]);

                ModelPaths = new string[numberOfModels2];
                for (int i = 0; i < numberOfModels2; ++i)
                    ModelPaths[i] = new string(modelPaths[i]);

                NumberOfModelsPerProject = new ushort[numberOfProjects2];
                for (int i = 0; i < numberOfProjects2; ++i)
                    NumberOfModelsPerProject[i] = numberOfModelsPerProject[i];

                ModelIds = new ushort[numberOfModels2];
                for (int i = 0; i < numberOfModels2; ++i)
                    ModelIds[i] = modelIds[i];

                ModelStates = new ushort[numberOfModels2];
                for (int i = 0; i < numberOfModels2; ++i)
                    ModelStates[i] = modelStates[i];
            }
            finally
            {
                if (projectPaths != null)
                {
                    for (int i = 0; i < numberOfProjects && projectPaths[i] != null; ++i)
                        Marshal.FreeHGlobal(IntPtr((void*)projectPaths[i]));

                    Marshal.FreeHGlobal(IntPtr((void*)projectPaths));
                }

                if (instanceIds != null)
                    Marshal.FreeHGlobal(IntPtr((void*)instanceIds));

                if (machineNames != null)
                {
                    for (int i = 0; i < numberOfProjects && machineNames[i] != null; ++i)
                        Marshal.FreeHGlobal(IntPtr((void*)machineNames[i]));

                    Marshal.FreeHGlobal(IntPtr((void*)machineIds));
                }

                if (controllerIps != null)
                {
                    for (int i = 0; i < numberOfProjects && controllerIps[i] != null; ++i)
                        Marshal.FreeHGlobal(IntPtr((void*)controllerIps[i]));

                    Marshal.FreeHGlobal(IntPtr((void*)controllerIps));
                }

                if (modelPaths != null)
                {
                    for (int i = 0; i < numberOfModels && modelPaths[i] != null; ++i)
                        Marshal.FreeHGlobal(IntPtr((void*)modelPaths[i]));

                    Marshal.FreeHGlobal(IntPtr((void*)modelPaths));
                }

                if (numberOfModelsPerProject != null)
                    Marshal.FreeHGlobal(IntPtr((void*)numberOfModelsPerProject));

                if (modelIds != null)
                    Marshal.FreeHGlobal(IntPtr((void*)modelIds));

                if (modelStates != null)
                    Marshal.FreeHGlobal(IntPtr((void*)modelStates));
            }
        }
    }
}
于 2012-09-20T01:07:27.137 に答える
0

C# を使用して関数を P/Invoke します。

using System.Runtime.InteropServices;

public class ActiveNames
{
    public string[] ActiveNames { get; set; }

    [DllImport("GetActiveNames.dll")] // replace with the actual name of the DLL
    unsafe private static int GetActiveNames(sbyte** names):

    public void GetActiveNames()
    {
        unsafe
        {
            // I use 100 here as an artificial number. This may not be a reasonable
            // assumption, but I don't know how the actual GetActiveNames works
            sbyte** names = (sbyte**)Marshal.AllocHGlobal(100).ToPointer();

            try
            {
                GetActiveNames(names);

                // fill the ActiveNames property
                ActiveNames = new string[100];

                for (int i = 0; i < 100; ++i)
                    ActiveNames[i] = new string(names[i]);
            }
            finally
            {
                // deallocate the unmanaged names memory
                Marshal.FreeHGlobal(IntPtr((void*)names));
            }
        }
    }
}

C++/CLI を使用する (P/Invoke は不要):

#include "GetActiveNames.h" // replace with the actual GetActiveNames's header

using namespace System;
using namespace System::Runtime::InteropServices;

public ref class ActiveNames
{
private:
    array<String^>^ m_activeNames;

public:
    property array<String^>^ ActiveNames
    {
        array<String^>^ get()
        {
            return m_activeNames;
        }

        void set(array<String^>^ names)
        {
            m_activeNames = names;
        }
    }

    void GetActiveNames()
    {
        signed char** names = new signed char*[100];

        try
        {
            ::GetActiveNames(reinterpret_cast<char**>(names));

            ActiveNames = gcnew array<String^>(100);

            for (int i = 0; i < 100; ++i)
                ActiveNames[i] = gcnew String(names[i]);
        catch (...)
        {
            delete[] names;
            throw;
        }

        delete[] names;
    }
};

ご覧のとおり、GetActiveNames がどのように機能するかがわからないため、安全でない仮定をいくつか行いました (名前のメモリはどのように割り当てられ、割り当てが解除されますか? 返された名前の数はどのようにわかりますか?)。さらにヘルプが必要な場合は、より具体的にする必要があります。

于 2012-09-19T18:30:03.283 に答える