我在从 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;
}
//P调用
[DllImportAttribute("SystemMISO,dll", CallingConvention = CallingConvention.Cdecl, EntryPoint="CallStackSearch")]
[return: MarshalAs(UnmanagedType.LPStruct)]
static private extern IntPtr CallStackSeach(IntPtr pObejct, userCredential item);
//函数包装器
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)."