P/Invoke 声明似乎不正确

发布于 2024-09-26 12:30:58 字数 972 浏览 8 评论 0原文

我得到了一个内部开发的库,作为 BITS 的包装。有人告诉我,如果您想在 64 位和 32 位版本之间进行更改,则需要交换这两行注释。

[StructLayout(LayoutKind.Explicit, Size = 8, Pack = 4)]  //32 bit address
internal struct BG_BASIC_CREDENTIALS
{
    [FieldOffset(0)]
    [MarshalAs(UnmanagedType.LPWStr)]
    public string UserName;

    [FieldOffset(4)]
    [MarshalAs(UnmanagedType.LPWStr)]
    public string Password;
}


//[StructLayout(LayoutKind.Explicit, Size = 16, Pack = 8)]  //64 bit address
//internal struct BG_BASIC_CREDENTIALS
//{
//    [FieldOffset(0)]
//    [MarshalAs(UnmanagedType.LPWStr)]
//    public string UserName;

//    [FieldOffset(8)]
//    [MarshalAs(UnmanagedType.LPWStr)]
//    public string Password;
//}

这对我来说是不合适的,是我通过做正确的事情得到这个的人(此代码使用交换注释技巧部署在 32 和 64 机器上,所以我知道它是有效的)。如果这是需要做的,有什么方法可以做到这一点,这样注释就不需要在每次 32 或 64 位构建完成时手动调整? (或者使该dll目标CPU全部兼容的方法)

链接到该数据类型的 MSDN

I was handed down a library that was developed in house as a wrapper for BITS. I was told that if you wanted to change between the 64bit and 32bit build you would need to swap out these two commented lines.

[StructLayout(LayoutKind.Explicit, Size = 8, Pack = 4)]  //32 bit address
internal struct BG_BASIC_CREDENTIALS
{
    [FieldOffset(0)]
    [MarshalAs(UnmanagedType.LPWStr)]
    public string UserName;

    [FieldOffset(4)]
    [MarshalAs(UnmanagedType.LPWStr)]
    public string Password;
}


//[StructLayout(LayoutKind.Explicit, Size = 16, Pack = 8)]  //64 bit address
//internal struct BG_BASIC_CREDENTIALS
//{
//    [FieldOffset(0)]
//    [MarshalAs(UnmanagedType.LPWStr)]
//    public string UserName;

//    [FieldOffset(8)]
//    [MarshalAs(UnmanagedType.LPWStr)]
//    public string Password;
//}

This just does not sit right with me, was the person who I got this from doing the right thing (this code is deployed on both 32 and 64 machines using the swapped comment trick so I know it works). If this is what needs to be done is there any way to make it so the comment does not need to be manually adjusted every time a 32 or 64 bit build is done? (or a way to make this dll target cpu all compatible)

Link to the MSDN of the datatype

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

氛圍 2024-10-03 12:30:58

你不需要做任何 x64/x86 技巧,这是结构体的 pinvoke

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
struct BG_BASIC_CREDENTIALS
{
  public string UserName;
  public string Pssword;
}

You don't need todo any x64/x86 tricks, here is the pinvoke of the struct

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
struct BG_BASIC_CREDENTIALS
{
  public string UserName;
  public string Pssword;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文