P/Invoke 声明似乎不正确
我得到了一个内部开发的库,作为 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全部兼容的方法)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你不需要做任何 x64/x86 技巧,这是结构体的 pinvoke
You don't need todo any x64/x86 tricks, here is the pinvoke of the struct