[StructLayout(LayoutKind.Explicit,Pack=1,CharSet=CharSet.Unicode,Size=64+2)]
        public unsafe struct Repository
        {
            /// <summary>
            /// 仓库名称
            /// </summary>
            [FieldOffset(0)]
            [MarshalAs(UnmanagedType.LPWStr, SizeConst = 64)]
            public fixed byte Name[32];  //加上fixed就可以使用c语言语法的数组
 
            /// <summary>
            /// 指针
            /// </summary>
            [FieldOffset(64)]
            public ushort location;
 

第二种定义方法

public unsafe struct MemoryInfo
    {
	   public UInt32 ModuleSize;
	   public UInt32 EntryPoint;
	   public UInt64 VirtualAddress;
	   public bool Readable;
	   public bool Writeable;
	   public bool Executeable;
	   public bool Guardable;
	  //如果使用char 占用字节会翻倍  使用bute就是真实大小
	   public fixed byte ModulePath[260];
    };

获取结构体大小

System.Runtime.InteropServices.Marshal.SizeOf(typeof(MemoryInfo))
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐