<p>I've got a problem similar to,but subtly different from, that described <a href="https://stackoverflow.com/questions/22012/loading-assemblies-and-its-dependencies">here</a> (Loading assemblies and their dependencies).</p>

<p>I have a C++ DLL for 3D rendering that is what we sell to customers. For .NET users we will have a CLR wrapper around it. The C++ DLL can be built in both 32 and 64bit versions, but I think this means we need to have two CLR wrappers since the CLR binds to a specific DLL? </p>

<p>Say now our customer has a .NET app that can be either 32 or 64bit, and that it being a pure .NET app it leaves the CLR to work it out from a single set of assemblies. The question is how can the app code dynamically choose between our 32 and 64bit CLR/DLL combinations at run-time?</p>

<p>Even more specifically, is the suggested answer to the aforementioned question applicable here too (i.e. create a ResolveEvent handler)?</p>

[{'answer_id': 22347, 'author': 'Ishmaeel', 'author_id': 227, 'author_profile': 'https://Stackoverflow.com/users/227', 'pm_score': 1, 'selected': False, 'text': "<p>I encountered a similar scenario a while back. A toolkit I was using did not behave well in a 64-bit environment and I wasn't able to find a way to dynamically force the assemblies to bind as 32 bit.</p>\n\n<p>It is possible to force your assemblies to work in 32 bit mode, but this requires patching the CLR header, (there is a tool that does that in the Framework) and if your assemblies are strongly-named, this does not work out.</p>\n\n<p>I'm afraid you'll need to build and publish two sets of binaries for 32 and 64 bit platforms.</p>\n"}, {'answer_id': 22414, 'author': 'Curt Hagenlocher', 'author_id': 533, 'author_profile': 'https://Stackoverflow.com/users/533', 'pm_score': 2, 'selected': False, 'text': "<p>I was able to do this about a year ago, but I no longer remember all of the details.  Basically, you can use IntPtr.Size to determine which DLL to load, then perform the actual LoadLibrary through p/Invoke.  At that point, you've got the module in memory and you ought to be able to just p/Invoke functions from inside of it -- the same module name shouldn't get reloaded again.</p>\n\n<p>I think, though, that in my application I actually had the C++ DLL register itself as a COM server and then accessed its functionality through a generated .NET wrapper -- so I don't know if I ever tested p/Invoking directly.</p>\n"}, {'answer_id': 479664, 'author': 'Greg Whitfield', 'author_id': 2102, 'author_profile': 'https://Stackoverflow.com/users/2102', 'pm_score': 4, 'selected': True, 'text': '<p>I finally have an answer for this that appears to work.</p>\n\n<p>Compile both 32 &amp; 64 bit versions - both managed &amp; unmanaged - into separate folders. Then have the .NET app choose at run time which directory to load the assemblies from.</p>\n\n<p>The problem with using the ResolveEvent is that it only gets called if assemblies aren\'t found, so it is all to easy to accidentally end up with 32 bit versions. Instead use a second AppDomain object where we can change the ApplicationBase property to point at the right folder. So you end up with code like:</p>\n\n<pre><code>static void Main(String[] argv)\n  {\n     // Create a new AppDomain, but with the base directory set to either the 32-bit or 64-bit\n     // sub-directories.\n\n     AppDomainSetup objADS = new AppDomainSetup();\n\n     System.String assemblyDir = System.IO.Path.GetDirectoryName(Application.ExecutablePath);\n     switch (System.IntPtr.Size)\n     {\n        case (4): assemblyDir += "\\\\win32\\\\";\n           break;\n        case (8): assemblyDir += "\\\\x64\\\\";\n           break;\n     }\n\n     objADS.ApplicationBase = assemblyDir;\n\n     // We set the PrivateBinPath to the application directory, so that we can still\n     // load the platform neutral assemblies from the app directory.\n     objADS.PrivateBinPath = System.IO.Path.GetDirectoryName(Application.ExecutablePath);\n\n     AppDomain objAD = AppDomain.CreateDomain("", null, objADS);\n     if (argv.Length &gt; 0)\n        objAD.ExecuteAssembly(argv[0]);\n     else\n        objAD.ExecuteAssembly("MyApplication.exe");\n\n     AppDomain.Unload(objAD);\n\n  }\n</code></pre>\n\n<p>You end up with 2 exes - your normal app and a second switching app that chooses which bits to load.\nNote - I can\'t take credit for the details of this myself. One of my colleagues sussed that out given my initial pointer. If and when he signs up to StackOverflow I\'ll assign the answer to him</p>\n'}]
['https://Stackoverflow.com/questions/22322', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2102/']