tags:

views:

41

answers:

1

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

+4  A: 

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;
}
Shay Erlichmen
Yup, that will do it. Even the non-standard Pack is unnecessary.
Hans Passant