Question

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 ?

Was it helpful?

Solution 2

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.

OTHER TIPS

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top