簡體   English   中英

PInvoke將來自ANSI C的結構指針編組為C#中的IntPtr

[英]PInvoke marshaling a struct pointer from ANSI C as an IntPtr in C#

所以我試圖從iniParser庫PInvoke C#中的非托管DLL。 我目前正在絆倒正確的方法來編組非托管庫中的函數返回和獲取的結構指針。

在C:

__declspec(dllexport) dictionary * iniparser_load(const char * ininame);

在C#中:

[DllImport("iniParser.dll")]
private static extern IntPtr iniparser_load(string filename);

C中的字典結構:

typedef struct _dictionary_ {
    int             n ;     /** Number of entries in dictionary */
    int             size ;  /** Storage size */
    char        **  val ;   /** List of string values */
    char        **  key ;   /** List of string keys */
    unsigned     *  hash ;  /** List of hash values for keys */
} dictionary ;

我理解為了實際訪問C#中的結構,我需要為C結構創建一個對應物,但我不需要在C#中訪問該結構。

在C#中調用該函數時,我收到以下錯誤:

A call to PInvoke function 'CacheExplorer!CacheExplorer.iniParser::iniparser_load'
has unbalanced the stack. This is likely because the managed PInvoke signature 
does not match the unmanaged target signature. Check that the calling convention 
and parameters of the PInvoke signature match the target unmanaged signature.

但是托管簽名與非托管簽名不匹配? PInvoke是否要求我為C結構制作C#對應物? 我所需要的只是C#中字典的句柄,訪問成員是完全沒必要的,我寧願不將結構轉換為C#

iniParser庫的調用約定可能是cdecl,這意味着您需要更新[DllImport]屬性用法:

[DllImport("iniParser.dll", CallingConvention = CallingConvention.Cdecl)]

暫無
暫無

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

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