2

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

lsalamon
  • 7,998
  • 6
  • 50
  • 63
Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431

1 Answers1

4

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;
}
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
Shay Erlichmen
  • 31,691
  • 7
  • 68
  • 87