1

所以我试图从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#

4

1 回答 1

4

iniParser 库的调用约定可能是 cdecl,这意味着您需要更新[DllImport]属性用法:

[DllImport("iniParser.dll", CallingConvention = CallingConvention.Cdecl)]
于 2013-03-10T22:30:01.040 回答