Tuesday, 12 May 2009

Fix your file associations

For ages I had a problem where I couldn't associate a .dwg file with anything other than AutoCAD 2006. I have 07, 08, 09 and 10 on my development machine, and every time I tried to use Windows Explorer's Tools > Folder Options > File Types to specifically point to one of these installations, it would always switch back to AutoCAD 2006. So, every time I double-clicked a dwg it would open in 06.

Enter Creative Element Power Tools. My colleague found this after having a similar problem with MicroStation XM and V8i. It's a neat box of tricks that's full of good ideas, one of them being the ability to easily re-associate your file types. Even better is the ability to easily add right-click options to a particular file type, enabling you to specify which version you want to open in. So now when I right-click on a dwg I can select any one of my Acad installations.

And there's way more than this. Take a look yourself as there's 'too many to list' :)

Wednesday, 6 May 2009

Preprocessor directives and conditional compilation

If you want different code to be compiled at build time under different conditions you can use preprocessor directives. Using the hash (#) symbol you can write code that the compiler responds to.

A common example of this is the need to use different code in 64 bit and 32 bit applications*.

#If PLATFORM = "x64" Then
'put your 64 bit specific code here
'you know
#End If

In this example we're poking the project's platform setting at compile time and acting accordingly. If the platform is set to 64 bit, the first set of code will be compiled. If the platform is set to 32 bit then the second set of code will be compiled.

You can define your own conditional compiler constants by using the #Const directive (only private to the file in which it is declared), or in the Project Properties dialog (which then has a public scope to all files in the project).

*Of course in some applications it might be more suitable to identify the bitness of the OS at runtime like this:

Private Shared Function Is64Bit() As [Boolean]
    Return Marshal.Sizeof(GetType(IntPtr)) = 8
End Function

Friday, 1 May 2009

Using pre-build events with SQL CE deployment

In a recent post I referred to the four dlls (sqlceer35EN.dll, sqlcese35.dll, sqlceme35.dll, sqlceqp35.dll) you need to carry around with your app to run a private installation of SQL CE. Drag them onto your project in your Visual Studio's Solution Explorer, set them as 'Content', and set them to copy to your output directory. Hey presto, there you have it.

BUT these four dlls are actually different for the 32 and 64 bit environments, so when you build for each platform you have to make sure you have the right ones. To automate this I used Pre-build events to copy the right ones in prior to build. 

Go to your project properties window and click on the 'Build Events' button. Here you're able to drop in code that gets stuff done prior to or after build. The syntax can include any command that is valid at the command line or in a .bat file, and this includes conditional statements that allow you to query certain project settings.

I'll cut to the chase, here's the code:

if $(PlatformName) == x64 xcopy /Y "C:\MyReferences\SQLServerCE\x64\*" "$(ProjectDir)"
if $(PlatformName) == x86 xcopy /Y "C:\MyReferences\SQLServerCE\x86\*" "$(ProjectDir)"