Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / December 2005

Tip: Looking for answers? Try searching our database.

Assembly.Load() cause BadImageFormatException

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
V&G - 29 Dec 2005 11:27 GMT
           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 - 29 Dec 2005 13:11 GMT
If CLR finds a file to load and , but the file is not a managed
assembly , a BadImageFormatException is thrown.
V&G - 29 Dec 2005 13:20 GMT
I speeking about managed assembly exactly
V&G - 29 Dec 2005 13:22 GMT
I'm speeking about managed assembly exactly
Mattias Sjögren - 29 Dec 2005 13:32 GMT
>            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

Signature

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 - 29 Dec 2005 13:52 GMT
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 - 29 Dec 2005 17:49 GMT
> 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
Signature

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

V&G - 29 Dec 2005 20:16 GMT
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/t
hread/6dff95d4ea9b6671
)...
So I wanted to serialize the object loaded in correct way.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.