首页 新闻 会员 周边

C#调用C++DLL,传入struct

0
悬赏园豆:5 [已解决问题] 解决于 2017-08-15 11:15

现在有一个C++写的DLL,有一个struct:

typedef struct USER_INFO
{
    char* user_id;
    char* user_name;
    char* user_icon;
    unsigned int user_icon_len;
}

一个函数:

int __stdcall get_user_info(void* user_info);

要在C#里调用这个函数,传入USER_INFO结构,来获取用户信息。

C#里我这么写:

    [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    public struct UserInfo
    {
        public IntPtr user_id;
        public IntPtr user_name;
        public IntPtr user_icon;
        public IntPtr user_icon_len;
    }

IntPtr infosIntptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(UserInfo)));
                i = get_user_info(infosIntptr);
UserInfo info = (UserInfo)Marshal.PtrToStructure(infosIntptr, typeof(UserInfo));

 

可以调用成功,但是用Marshal.PtrToStringAuto(info.user_name)来获取struct的属性,总是不正确,全是乱码,user_icon也不对。

后来把struct改成这样:

    [StructLayoutAttribute(LayoutKind.Sequential)]
    public struct UserInfo
    {
        [MarshalAs(UnmanagedType.ByValTStr)]
        public string user_id;
        [MarshalAs(UnmanagedType.ByValTStr)]
        public string user_name;
        [MarshalAs(UnmanagedType.ByValArray)]
        public byte[] user_icon;
        public int user_icon_len;
    }

也是无法正确取到UserInfo的字段属性,请问高手这个struct要怎么声明,怎么传参?

mrhyher的主页 mrhyher | 初学一级 | 园豆:5
提问于:2017-03-13 11:20
< >
分享
最佳答案
0
public struct UserInfo
    {
        public string user_id;
        public string user_name;
        public string user_icon;
        public uint user_icon_len;
    }

结构体这样封应该没什么问题

收获园豆:5
史蒂芬周 | 初学一级 |园豆:76 | 2017-03-14 20:04
其他回答(1)
0
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct USER_INFO {
    
    /// char*
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)]
    public string user_id;
    
    /// char*
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)]
    public string user_name;
    
    /// char*
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)]
    public string user_icon;
    
    /// unsigned int
    public uint user_icon_len;
}
那个函数的话参数改成Inptr类型就行
ぁ丶泛滥的尐青春つ | 园豆:199 (初学一级) | 2017-05-06 17:16
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册