Search code examples
.netx86project64-bitanycpu

.NET AnyCPU project links platform specific library


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.


Solution

  • 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.