将struct数据类型编组为从C ++ Dll到C#的返回类型

时间:2013-03-10 13:52:58

标签: c# c++ data-structures marshalling dllimport

我在从定义堆栈中获取搜索方法的值时遇到问题 从C ++ DLL到C#程序这里是我的代码

C ++ ---------------------------------------------- ----------------

extern "C" __declspec(dllexport) userCredential CallStackSeach(StackUser* pObject, userCredential item)
{
    return pObject->search(item);
}

结构

typedef struct userInfo
{
    char* firstName;
    char* middleName;
    char* lastName;
    char* address;
    char* email;
};


typedef struct userCredential
{
    char* username;
    userInfo person;
    char* password;
    bool admin;

};

C#---------------------------------------------- --------------

结构

[StructLayout(LayoutKind.Sequential)]
public struct userInfo
{
    [MarshalAs(UnmanagedType.LPStr)]
    public string firstName;
    [MarshalAs(UnmanagedType.LPStr)]
    public string middleName;
    [MarshalAs(UnmanagedType.LPStr)]
    public string lastName;
    [MarshalAs(UnmanagedType.LPStr)]
    public string address;
    [MarshalAs(UnmanagedType.LPStr)]
    public string email;
}
[StructLayout(LayoutKind.Sequential)]
public struct userCredential
{
    [MarshalAs(UnmanagedType.LPStr)]
    public string username;
    [MarshalAs(UnmanagedType.Struct)]
    public userInfo person;
    [MarshalAs(UnmanagedType.LPStr)]
    public string password;
    [MarshalAs(UnmanagedType.Bool)]
    public bool admin;
}

// PInvoke的

[DllImportAttribute("SystemMISO,dll", CallingConvention = CallingConvention.Cdecl, EntryPoint="CallStackSearch")]
[return: MarshalAs(UnmanagedType.LPStruct)]
static private extern IntPtr CallStackSeach(IntPtr pObejct, userCredential item);

// FunctionWrapper

public userCredential StackSeach(userCredential item)
{         
   var ptr = CallStackSeach(this.ptrObject,item);
   return (userCredential)Marshal.PtrToStructure(ptr, typeof(userCredential));
   //occur error
}

我遇到错误"Cannot marshal 'return value': Invalid managed/unmanaged type combination (Int/UInt must be paired with SysInt or SysUInt)."

0 个答案:

没有答案