簡體   English   中英

如何將C指針編組為C#的struct數組

[英]How to Marshal C pointer to C# array of struct

我試圖將一個指針從ac dll轉換為其等效的C#結構數組。

C代碼

RECORD       locked[MAX+1]; //MAX is a constant

typedef struct
{
  State state; //enum
  unsigned long allocated;
  unsigned long lastUsed;
  unsigned int  useCount;
} RECORD;

API RECORD* __stdcall GetLocks( char* password )
{
  if(strcmp(password, secret) == 0)
    return locked;
  else
    return 0;
}

C#代碼

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] // Pretty sure CharSet isnt actually needed here
public struct RECORD
{
    public State state;
    public UInt32 allocated;
    public UInt32 lastUsed;
    public UInt16 useCount;
}

    [DllImport("gatewayapi.dll", CharSet = CharSet.Ansi)] // or here
    static extern IntPtr GetLocks(string password);
    public RECORD[] GetLocks(string password)
    {
        RECORD[] recs = new RECORD[MAX+1];

        recs =(RECORD[])Marshal.PtrToStructure( GetLocks(password), typeof(RECORD[]));

        if (recs.Length == 0)
        {
            throw new Exception();
        }
        return recs;
    }

不幸的是,以上方法返回了MissingMethodException->沒有為此對象定義無參數的構造函數。

因此,對於Marshalling來說,這是100%全新的知識,並希望獲得一些有關如何將我從C接收到的指針轉換為它所代表的實際C#結構數組的建議。

謝謝

給定最初發布的C代碼,這是我想出的答案,不需要在不安全的模式下編譯代碼:

    [DllImport("gatewayapi.dll", CharSet = CharSet.Ansi)]
    static extern IntPtr AMTgetLocks(string password);
    public RECORD[] GetLocks(string password)
    {
        var channels = new RECORD[MAXCHANS + 1];
        try
        {
            var c = AMTgetLocks(password);
            var crSize = Marshal.SizeOf(typeof(RECORD));
            for (int i = 0; i < MAXCHANS + 1; i++)
            {
                channels[i] = (CHANNELRECORD)Marshal.PtrToStructure(c, typeof(RECORD));
                c = new IntPtr(c.ToInt64() + crSize);
            }
        }
        catch (Exception)
        {
            throw new Exception();
        }
        if (channels.Length == 0)
        {
            throw new Exception();
        }
        return channels;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM