C# 怎么封装含有指针的结构体

youqingyike 2014-08-18 01:22:12
结构体里面含有 char*,int*,char[],int[],
比如:C 或C++ 动态库中,函数接口参数如下,
全是输入参数,
typedef struct MyStruct
{
char* pChar;
int* pInt;
char arrChar[20];
int arrInt[10];
}

C# 中如何封装对应结构体,求指教,主要是char* pChar 和 int* pInt 怎么获取地址
...全文
506 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
youqingyike 2014-08-18
  • 打赏
  • 举报
回复
C的Dll 结构体定义如下: typedef struct structPointer { void* pvVoid; bool* pBool; char* pChar; int* pInt; short* pShort; long* lpLong; unsigned char* pucChar; unsigned int* puiInt; unsigned short* pusShort; unsigned long* pulLong; float* pFloat; double* pDouble; }stPointer,*pstPointer; C# 中对应结构体定义如下: [StructLayout(LayoutKind.Sequential)] public struct stPointer { [MarshalAs(UnmanagedType.SysUInt)] public IntPtr pvVoid; [MarshalAs(UnmanagedType.SysUInt)] public IntPtr pBool; public string pChar; [MarshalAs(UnmanagedType.SysUInt)] public IntPtr pInt; [MarshalAs(UnmanagedType.SysUInt)] public IntPtr pShort; [MarshalAs(UnmanagedType.SysUInt)] public IntPtr lpLong; public string pucChar; [MarshalAs(UnmanagedType.SysUInt)] public IntPtr puiInt; [MarshalAs(UnmanagedType.SysUInt)] public IntPtr pusShort; [MarshalAs(UnmanagedType.SysUInt)] public IntPtr pulLong; [MarshalAs(UnmanagedType.SysUInt)] public IntPtr pFloat; [MarshalAs(UnmanagedType.SysUInt)] public IntPtr pDouble; }; C中函数 : _declspec(dllexport) void pointStruct(stPointer stTest); C#中对应定义: [DllImport("DataTypeTest_Dll.dll", EntryPoint = "pointStruct")] public static extern void pointStruct(stPointer stTest); 调用后,地址传值是正确,除字符串 public string pChar; 和 public string pucChar; 正确, 其他值都不对,有的是非常大的负值,不知什么原因
youqingyike 2014-08-18
  • 打赏
  • 举报
回复
谢谢,我看下
相思梦 2014-08-18
  • 打赏
  • 举报
回复
typedef struct MyStruct { char* pChar; int* pInt; char arrChar[20]; int arrInt[10]; } [StructLayout(LayoutKind.Sequential)] public unsafe struct MyStruct{ public char* pChar; char* pChar; int* pInt; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)] string arrChar; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)] int[] arrInt; }
宝_爸 2014-08-18
  • 打赏
  • 举报
回复

110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧