1

this is the error code i get

System.BadImageFormatException: impossible to load the file or assembly '6632 bytes loaded from quick test 2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. Attempt to load a program in a bad format.
Nome file: '6632 bytes loaded from quick test 2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' ---> System.BadImageFormatException: Format IL not correct.
   in System.Reflection.Assembly.nLoadImage(Byte[] rawAssembly, Byte[] rawSymbolStore, Evidence evidence, StackCrawlMark& stackMark, Boolean fIntrospection)
   in System.Reflection.Assembly.Load(Byte[] rawAssembly)
   in quick_test_2.Form1.button2_Click(Object sender, EventArgs e) in C:\Users\HHH\Documents\Visual Studio 2010\Projects\quick test 2\quick test 2\Form1.cs:riga 175
   in System.Windows.Forms.Control.OnClick(EventArgs e)
   in System.Windows.Forms.Button.OnClick(EventArgs e)
   in System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   in System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   in System.Windows.Forms.Control.WndProc(Message& m)
   in System.Windows.Forms.ButtonBase.WndProc(Message& m)
   in System.Windows.Forms.Button.WndProc(Message& m)
   in System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   in System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   in System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

when i try to load a simple windows form application(.net c#)

the code i use is this

try 
{
    Assembly myAsm = Assembly.Load(File.ReadAllBytes("myFile.exe"));
    MethodInfo myMethod = assm.EntryPoint;
    object myObj = assm.CreateInstance(myMethod.Name);
    myMethod.Invoke(o, null);
}
catch 
{
    //error
}

So far i only managed to make it work for Form applications where i removed the Form and ran the code from the [STAThread] inside Program.cs, it didnt even work for a simple console application.

Any insight on this ?

user1909612
  • 273
  • 5
  • 14
  • It sounds like that EXE file isn't actually an EXE file. – SLaks Mar 12 '13 at 22:11
  • i am quite sure it is an exe, i compiled the test exe myself – user1909612 Mar 12 '13 at 22:20
  • At least get rid of File.ReadAllBytes(), that serves no purpose and just adds another way this can fail. Having mismatches in the target platform and the .NET framework version are other reasons for this exception. – Hans Passant Mar 12 '13 at 23:40

3 Answers3

1

Is your loaded assembly built for the same CPU architecture as the host exe (x86 vs. x64)? If the host assembly was set for "Any CPU" and the loaded assembly is set to x86, this will cause issues on a 64-bit machine.

luksan
  • 7,661
  • 3
  • 36
  • 38
  • Extra info: the problem is somewhere around here Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, in the exception info it says the version is 1.0.0.0 and the publickeytoken is null – user1909612 Mar 12 '13 at 23:20
0

I do believe the problem lies in the calling application being a winform app itself. I manually started the program from a simple [Stathread] and it worked. I don' t know if this can count as solved

user1909612
  • 273
  • 5
  • 14
  • 1
    Winforms apps will by default target .NET Client Profile, which will not be able to load applications using full .NET (console applications by default). Try changing the target framework in the Winforms project properties to full .NET and see if that fixes it. – luksan Mar 15 '13 at 17:32
  • Cool. Can you accept my original answer so I at least get some points? ;-) – luksan Mar 16 '13 at 02:26
  • your original answer is a comment, i dont have the option of accepting it :/ – user1909612 Mar 16 '13 at 16:30
  • No, look at my user name. I also wrote the other answer about x86 vs. x64. – luksan Mar 16 '13 at 19:50
0

It can typically occur when you changed the target framework of .csproj and reverted it back to what you started with. Before you ever get and go crazy trying to google the right solution just make sure :

  1. check if supportedRuntime version="a different runtime from cs project target" under startup tag in app.config.

  2. That also means checking other autogenerated or other files in may be properties folder to see if there is no more runtime mismatch between these files and one that is defined in .csproj file.

These might just save you lot of time before you start trying different things with project properties to overcome the error.

purvin
  • 144
  • 1
  • 3
  • I made a quick test and it turned out you were right, whenever i change the version of the project and than change it again the assembly gives me problems to load. I guess i' ll have to choose the version from the start. – user1909612 Apr 13 '13 at 12:14