Click here to Skip to main content
15,893,588 members
Articles / All Topics

Fix Error: An Attempt Was Made to Load a Program with an Incorrect Format

Rate me:
Please Sign up or sign in to vote.
4.93/5 (6 votes)
13 Jun 2011Ms-PL1 min read 126K   1   4
Post on how to fix an error while trying to run the sample project ShellObjectWatcherSampleWPF from the latest version of Windows API Code Pack

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.

Image 3Image 4

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Software Developer (Senior) Verint
Israel Israel
Arik Poznanski is a senior software developer at Verint. He completed two B.Sc. degrees in Mathematics & Computer Science, summa cum laude, from the Technion in Israel.

Arik has extensive knowledge and experience in many Microsoft technologies, including .NET with C#, WPF, Silverlight, WinForms, Interop, COM/ATL programming, C++ Win32 programming and reverse engineering (assembly, IL).

Comments and Discussions

 
Questionבאמת? Pin
Member 800306317-Dec-15 3:37
Member 800306317-Dec-15 3:37 
SuggestionIIS: An attempt was made to load a program with an incorrect format Pin
tanioskahi10-May-12 20:46
tanioskahi10-May-12 20:46 
GeneralMy vote of 5 Pin
Dhadhan4-Apr-12 5:40
Dhadhan4-Apr-12 5:40 
QuestionSharepoint 2007 vs visual studio 2008 , debug problem Pin
kinny0722-Nov-10 21:18
kinny0722-Nov-10 21:18 
Hello I got a similar problem.

As our company is using Sharepoint 2007 (64bits), but the visual studio 2008 is 32bits

We choose "Any CPU" at the platform target. We construct a webpart project and try to debug. Then the webpart can be shown at http://localhost:51059/Default.aspx , but this webpart need to get or update the list item in sharepoint using this :

SPSite site = new SPSite("http://moss.xxxxxxxxxx.com/group/itd/default.aspx");

to connect our live site at a button. When click the button, we got this error:

The Web application at http://moss.xxxxxxxxxx.com/group/itd/default.aspx could not be found. Verify that you have typed the URL correctly. If the URL should be serving existing content, the system administrator may need to add a new request URL mapping to the intended application.

We cannot continue the debug. However, we can still put the web-part to sharepoint site, this work fine.
We browse to search how to fix, we got this may related to bit difference. So, we try to choose the platform target "x64". But other error occur:

Could not load file or assembly ’InsertEvent, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null’ or one of its dependencies. An attempt was made to load a program with an incorrect format.

Another bit problem, Can any one help me to solve it ?? how i can do ?? Thanks alot !!

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.