Fixing ApplicationBlocks.Updater Exception in x64

I recently inherited a project that uses Microsoft Enterprise Library, albeit that it uses source code instead of binaries. Yuck. I spent a lot of time trying to fix compatibility issues with x86 vs. x64.

In the end, I did manage to get it working. The major issue I ran into was an exception in the Updater project:


Could not load type 'Microsoft.ApplicationBlocks.Updater.Downloaders.BG_BASIC_CREDENTIALS' from assembly 'Microsoft.ApplicationBlocks.Updater.Downloaders, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null' because it contains an object field at offset 4 that is incorrectly aligned or overlapped by a non-object field.

As it turns out, this is a unique x64 problem. Essentially, the struct in question uses a space of 4 bytes, which is correct for 32-bit machines. On 64-bit machines, it needs to be eight bytes.

We can easily update the relevant class, like so:


/// [StructLayout(LayoutKind.Explicit, Size=8, Pack=4)]
internal struct BG_BASIC_CREDENTIALS
{
///

[FieldOffset(0)]
[MarshalAs(UnmanagedType.LPWStr)]
public string UserName;

///

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

Tada, instant success! The exception disappears, and everything proceeds on as expected.

About Ashiq Alibhai, PMP

Ashiq has been coding C# since 2005. A desktop, web, and RIA application developer, he's touched ASP.NET MVC, ActiveRecord, Silverlight, NUnit, and all kinds of exciting .NET technologies. He started C# City in order to accelerate his .NET learning.
This entry was posted in Libraries, Silverlight, Web, Wndows Forms and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *