5

Possible Duplicate:
Loading x86 or x64 assembly

I'm trying to compile Any CPU .NET project, but I have to link SQLite library which has different versions for x86 and x64 platforms. Changing only DLL versions to x64 doesn't help, application doesn't start, I need to recompile code using x64 reference. When I add both x86 and x64 references it fails to compile, because of conflicts. I can't compile application using x86, because one of the system COM libraries I'm using doesn't work under WOW64.

All 32-bit VSS applications (requesters, providers, and writers) must run as native 32-bit or 64-bit applications. Running them under WOW64 is not supported

http://social.msdn.microsoft.com/Forums/en-US/windowsgeneraldevelopmentissues/thread/eadf5dcd-fbd1-4224-9a56-b5843efebb15/

so I need to build Any CPU project, but the only solution to this problem I see at the moment is having duplicate projects for x86 and x64. Is there anything better?

UPDATE

When I reference x64 libraries in project, but try to load x86 libraries I get the following exception.

The located assembly's manifest definition does not match the assembly reference.

Community
  • 1
  • 1
axe
  • 2,331
  • 4
  • 31
  • 53
  • Thanks. I've seen posts like that, but the problem is changing DLL only doesn't help much. It still doesn't work when I copy 64 bit DLLs into my folder, I still need to recompile application referencing those 64 bit libraries. Am I doing something wrong? And I also don't understand which version should I reference? – axe Jul 24 '12 at 11:45
  • 1
    Yes. The native libraries must have identical names, but must be located in different folders(such as subfolders of your app). At runtime you call `SetDllDirectory` to make the OS to look in the correct folder depending on the bitness. Or use the `AppDomain.CurrentDomain.AssemblyResolve` event as [suggested by Rover](http://stackoverflow.com/questions/3787428/loading-x86-or-x64-assembly#comment7029800_6060754). You don't reference those libraries directly, the SQLite managed wrapper does. Your code will make the wrapper load the correct version of the underlying native library. – GSerg Jul 24 '12 at 11:49
  • I tried it once again, it doesn't work for me. Application crashes on start. The problem is I'm referencing x64 libraries in project, but I select x86 libraries at runtime. If I build my application using x86 libraries it perfectly works on x86 windows, otherwise it doesn't. – axe Jul 24 '12 at 12:04
  • Oops. Sorry, it was my fault. Actually I downloaded 64 bit version from internet just now, but 32 bit version was a bit older. They managed to upload new version and I didn't notice that. Thanks, the question is closed. – axe Jul 24 '12 at 12:35
  • 1
    @axe Then post an answer describing what you did, and accept it. – CodesInChaos Jul 24 '12 at 12:59

1 Answers1

6

The main problem was the fact that I was using different versions of SQLite for x86 and x64. I added method

static private Assembly SQLitePlatformSpecificResolve(object sender, ResolveEventArgs args)
{
    string platform = Environment.Is64BitProcess ? "x64" : "x86";
    string assemblyName = new AssemblyName(args.Name).Name;
    string assemblyPath = Path.Combine(
        Environment.CurrentDirectory, "SQLite", platform, assemblyName + ".dll");

    return !File.Exists(assemblyPath) ? null : Assembly.LoadFrom(assemblyPath);
}

And set event handler

AppDomain.CurrentDomain.AssemblyResolve += SQLitePlatformSpecificResolve;

in main application entry point. Now it loads x86 assembly for x86 platforms and x64 on 64 bit platforms correspondingly.

Thanks.

axe
  • 2,331
  • 4
  • 31
  • 53