While trying to run the sample project ShellObjectWatcherSampleWPF from the latest version of Windows API Code Pack, I came across the following problem, both in Visual Studio 2008 and 2010:

image

Or in a search-engine friendly way:

“Cannot create instance of ‘MainWindow’ defined in assembly, ‘ShellObjectWatcherSampleWPF, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null’. Exception has been thrown by the target of an invocation.  Error in markup file ‘MainWindow.xaml’ Line 1 Position 9.”

Checking the internal exception, revealed the following:

"Could not load file or assembly 'Microsoft.WindowsAPICodePack.Shell, Version=1.1.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format."

Now, when we get an error of “incorrect format”, it usually means there is a matching problem between the bitness of the project and the bitness of the machine.

Since my system happens to be x64 based, and the project was compiled as 32bit, we got ourselves a problem.

Usually, this doesn’t matter, since .NET is bit-agnostic (yet another advantage of JIT compilation), but since the project depends heavily on P/Invoke, it does indeed make a difference.

The solution is simply to go to the Project Properties | Build and change the Platform target to x64.

image

That’s it for now,
Arik Poznanski.

推荐.NET配套的通用数据层ORM框架:CYQ.Data 通用数据层框架
新浪微博粉丝精灵,刷粉丝、刷评论、刷转发、企业商家微博营销必备工具"