Saturday, December 25, 2010

Identifying target machine (32bit or 64bit) with ClickOnce deployment

The title is from a question at Stack Overflow, and the problem is how do you check what environment you are running under when you have third party libraries which are compiled explicit to x86/x64, in order to instantiate the correct version?
My test project for the scenario contains three parts. “test” my third party library, “ClickOnceTest”, the main application, and “Loader”, the application which will start “ClickOnceTest” with the correct version of the “test” library.
image

“ClickOnceTest” has a reference to the third party library with “Copy Local” set to false. This is because the loader app will copy in the correct version of the library. In the SO question both libraries have the same version information, they are just built for different targets, so referencing either on in the project, and copy in a substitute later on will work.
image
The loader application includes a reference to the “ClickOnceTest” application, and a copy of the needed third party libraries with “Copy to Output Directory” set to “Copy always”. This ensures that the files are included when we build the loader application.
image
The loader code itself is fairly simple. We retrieve the location of where the assembly is residing, copy either test32.dll or test64.dll over to test.dll, and start Form1 which is the form in the “ClickOnceTest” application.
Checking the size of IntPtr is a normal way to decide if your application is running under either 32bit or 64bit mode. In 32bit mode the size will be 4, and in 64bit it will be 8 due to the extended memory space you can address in 64bit.
image
A zip file of the test solution can be downloaded below.