C# - PInvokeStackImbalance - 编组指向C结构的指针

时间:2017-03-02 11:05:13

标签: c# c dll marshalling

我有以下C代码:

#define NAG_CALL __stdcall
#define NAG_DLL_EXPIMP __declspec(dllexport) 
#define MAX_TABLE_LEN 4096

struct section_for_csharp {      
  unsigned char new_data; 
  unsigned char end;    
  unsigned char stop;    
  unsigned char section[MAX_TABLE_LEN];
  unsigned int section_len;
  unsigned char table_id;
  unsigned int pid;
};
struct section_for_csharp data_to_ext;

NAG_DLL_EXPIMP void NAG_DLL_EXPIMP return_section_for_csharp (struct section_for_csharp *temp) {
  temp = &data_to_ext;
}

我有以下C#代码:

    [DllImport("../../utils/libs_win/ts2sec.dll")]        
    public static extern void return_section_for_csharp(ref section_for_csharp temp);

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    public struct section_for_c_sharp
    {      
        public byte new_data;
        public byte end;   
        public byte stop;  

        //unsigned char section[MAX_TABLE_LEN];
        [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = MAX_TABLE_LEN)]
        public byte[] section;

        public uint section_len;
        public byte table_id;
        public uint pid;          
    };

当我运行这样的代码时:

section_for_c_sharp temp = new section_for_c_sharp();          
return_section_for_csharp(ref temp);
Console.WriteLine(temp.new_data);  

我得到return_section_for_csharp函数执行:
托管调试助手&PInvokeStackImbalance'已在C:\ TSMuxer_CSharp \ TSMuxer \ bin \ Debug \ TSMuxer.vshost.exe'中检测到问题。

附加信息:调用PInvoke函数' TSMuxer!TSMuxer.TSMuxer_MainForm :: return_section_for_csharp'堆栈不平衡。这很可能是因为托管PInvoke签名与非托管目标签名不匹配。检查PInvoke签名的调用约定和参数是否与目标非托管签名匹配。

0 个答案:

没有答案
相关问题