开发者

P/Invoke declaration does not seem correct

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 UserNa开发者_StackOverflow社区me;

    [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


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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜