20 December, 2012

An attempt was made to load a program with an incorrect format

On 64 bit versions of windows you can recive an anoying error stating that "An attempt was made to load a program with an incorrect format".

This can sometimes be caused by the fact that you are trying to reference a 64 bit dll in a 32 bit project, but it can strangely enough also be caused by a bug in VS 2010 involving resx files.

I gathered a few reference links, about the issue:
stackoverflow.com/.../an-attempt-was-made-to-load-a-program-with-an-incorrect-format
connect.microsoft.com/VisualStudio/feedback/details/532584/....
msdn.com/b/visualstudio/.../resgen-exe-error-...-program-with-an-incorrect-format
stackoverflow.com/..../could-not-load-file-or-assembly-or-one-of-its-dependencies

I ran into this problem a few times, after any resource (resx) files was modified, and decided to find a solution that would work, and could be put inside the project/solution build cycle.

Add to the project "pre-build" event
FORFILES /P $(ProjectDir) /M *.resx /S /C "CMD /C RegexConsole.exe @path \"AAEAAAD/////AQAAAAAAAAAMAgAAAFdTeXN0ZW0uV2luZG93cy5Gb3JtcywgVmVyc2lvbj00LjAuMC4w\" \"AAEAAAD/////AQAAAAAAAAAMAgAAAFdTeXN0ZW0uV2luZG93cy5Gb3JtcywgVmVyc2lvbj0yLjAuMC4w\" I"

It's important the the line of code is on a single line and that the RegexConsole.exe file is placed in a accesable path, put it in the system32 folder or another global %PATH% registered location.

Downloads:
VS-Fix-resx-files-pre-build-event.txt
RegexConsole.exe

No comments:

Post a Comment