Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Assembly.Load() cause BadImageFormatException

2 views
Skip to first unread message

V&G

unread,
Dec 29, 2005, 6:27:17 AM12/29/05
to
FileStream fs = new FileStream(fileName, FileMode.Open,
FileAccess.Read);
Byte[] body = new Byte[(int)fs.Length];
fs.Read(buffer, 0, buffer.Length);
fs.Close();
Assembly assembly = Assembly.Load(body);

This code cause BadImageFormatException. An error message is:
"A BadImageFormatException has been thrown while parsing the signature.
This is likely due to lack of a generic context. Ensure
genericTypeArguments and genericMethodArguments are provided and
contain enough context."
InnerException:
"An attempt was made to load a program with an incorrect format.
(Exception from HRESULT: 0x8007000B)"

My target is to load an assembly and serialize it for future proposes.
So I can not use LoadFrom() directly.

Do anybody have any suggestions?
Thanks for all

Naveen

unread,
Dec 29, 2005, 8:11:03 AM12/29/05
to

If CLR finds a file to load and , but the file is not a managed
assembly , a BadImageFormatException is thrown.

Message has been deleted

V&G

unread,
Dec 29, 2005, 8:22:13 AM12/29/05
to
I'm speeking about managed assembly exactly

Mattias Sjögren

unread,
Dec 29, 2005, 8:32:36 AM12/29/05
to
> FileStream fs = new FileStream(fileName, FileMode.Open,
>FileAccess.Read);
> Byte[] body = new Byte[(int)fs.Length];
> fs.Read(buffer, 0, buffer.Length);

There's no guarantee that a single call to Read will fill the byte
array. You should check the return value and if necessary keep calling
Read until everything has been read or the end of the stream is
reached.


>My target is to load an assembly and serialize it for future proposes.
>So I can not use LoadFrom() directly.

But does it load correctly if you use Assembly.LoadFrom(fileName) so
you know that the file isn't corrupt or anything?


Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

V&G

unread,
Dec 29, 2005, 8:52:08 AM12/29/05
to
I checked it. The whole file was red.
The error occurs on particular assemblies.
May be the reason is than these assemblies contain unmanaged code?

Richard Grimes

unread,
Dec 29, 2005, 12:49:57 PM12/29/05
to
"V&G" <va...@vandg.d2g.com> wrote in message
news:1135855637....@g47g2000cwa.googlegroups.com...

> My target is to load an assembly and serialize it for future proposes.
> So I can not use LoadFrom() directly.
>
> Do anybody have any suggestions?


Read what Gregory Beamer said in another post in this group. It makes no
sense to 'serialize an assembly'. What are you trying to gain from
serializing an assembly as opposed to loading the assembly from disk?

Richard
--
Fusion Tutorial: http://www.grimes.demon.co.uk/workshops/fusionWS.htm
Security Tutorial:
http://www.grimes.demon.co.uk/workshops/securityWS.htm


V&G

unread,
Dec 29, 2005, 3:16:16 PM12/29/05
to
There is a multi-user application - shell for accumulating/initializing
components for future usage. In other words user can choose some
assembly, choose some object type from that assembly, instantiate that
object and initialize the instance with default values. After all, all
of this information should be saved to the database for the future
usage. As you see I need to load all referenced assemblies also.
Because the application is a multi-user application I need to store in
database all required data including locally loaded assemblies, so
other users would be able to recreate my AppDomain assemblies
collection to instantiate the objects. Of cause, the alternative is to
manage some centralized data store on server disk, but in this case
there is a danger to get inconsistent data in two sources:
"database"-"disk storage".
Here is the problems start...
As you see, I have opened three different topics in this subj. As I
understand, there are two general ways to load an assembly:
- by defining assembly path/name
- by reading assembly library as a byte array
And here is the source of my questions. I can load assemblies in first
way. But there is no way to serialize an assembly loaded to the memory.
On the other hand, I can read assembly to byte array and load it from
there. But in this case CLR does not recognizes assemblies loaded from
COFF-file image as signed and creates multiple entries for the same
assembly in AppDomain (see
http://groups.google.com/group/microsoft.public.dotnet.framework/browse_thread/thread/6dff95d4ea9b6671)...
So I wanted to serialize the object loaded in correct way.

0 new messages