c# - 使 StructLayout 在类上工作,就像它在结构上工作一样

标签 c# class struct unmanaged structlayout

在处理非托管代码时,我想更好地理解结构/类的映射。

我定义了以下结构:

   [StructLayout(LayoutKind.Sequential)]
   public struct ProfileInfo
   {
      public int dwSize;
      public int dwFlags;
      [MarshalAs(UnmanagedType.LPTStr)] public string lpUserName;
      [MarshalAs(UnmanagedType.LPTStr)] public string lpProfilePath;
      [MarshalAs(UnmanagedType.LPTStr)] public string lpDefaultPath;
      [MarshalAs(UnmanagedType.LPTStr)] public string lpServerName;
      [MarshalAs(UnmanagedType.LPTStr)] public string lpPolicyPath;
      public IntPtr hProfile;

      public ProfileInfo(string userName, string profilepath)
      {
         dwFlags = 1;    
         dwSize = Marshal.SizeOf<ProfileInfo>();
         lpUserName = userName;
         lpServerName = null;
         lpProfilePath = string.IsNullOrWhiteSpace(profilepath) ? null : profilepath;
         lpPolicyPath = null;
         lpDefaultPath = null;
         hProfile = IntPtr.Zero;
      }
   }

与以下方法一起使用:

      [DllImport("userenv.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "LoadUserProfileW")]
      public static extern bool LoadUserProfile(IntPtr hToken, ref ProfileInfo lpProfileInfo);

虽然只要 ProfileInfo 是一个结构它就可以很好地工作,但是当我将 ProfileInfo 设为一个类时 LoadUserProfile 开始失败。

我只是想知道为什么?
对我来说,StructLayout 以相同的方式应用于类或结构。

当我将 ProfileInfo 的内存表示形式从 struct 更改为 class 时,导致 LoadUserProfile 失败的可能差异是什么?

最佳答案

在 C++ 中,类包含一个隐藏的“this”指针。我猜 C# 做同样的事情。

在下图中,pi 是一个结构体,p2 是一个类。从内存转储(左边是 pi,右边是 p2)可以看出,p2 有一些额外的字节,这是隐藏指针。

enter image description here

关于c# - 使 StructLayout 在类上工作,就像它在结构上工作一样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32054267/

相关文章:

c# - 存储库有更改事件是一种代码味道吗?

c - 结构体首地址

复制结构成员

c# - 无法在 app.config 上写入设置(或未显示任何更改)

C#:将int[]转换为字符串的最有效方法

c# - 将可空整数初始化为默认值的最佳实践?

c++ - 用层次结构和类替换数组是否可以?

ruby-on-rails - 在 Ruby on Rails 应用程序中使用 RSpec 时,对 Mock 对象的 #class 方法进行 stub 是否合法?

c++ - 如何读取位和将位写入字节数组

c++ - 我可以用结构 vector 构建一个结构 vector vector 吗? (对真的)