How can I tell (from C# code) if a .DLL is native or a .NET assemb

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

How can I tell (from C# code) whether a .DLL contains native code or is
infact a .NET assembly?

I know that I could use Assembly.LoadFile() and see if it throws a
BadImageFormatException but I really don't want any exceptions in my normal
program flow...



sincerely,
martin
 
Hello, martin!

m> How can I tell (from C# code) whether a .DLL contains native code or is
m> infact a .NET assembly?

m> I know that I could use Assembly.LoadFile() and see if it throws a
m> BadImageFormatException but I really don't want any exceptions in my
m> normal program flow...

You will have to analyze file PE format
( http://www.samspublishing.com/articles/article.asp?p=25350&rl=1 )
links in the article are broken, here valid ones
( http://msdn.microsoft.com/msdnmag/issues/02/02/PE/ )

Here is the sample how to do this
( http://geekswithblogs.net/rupreet/archive/2005/11/02/58873.aspx )
--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
 
One more way to check PE for managed code is "COM Descriptor Directory"
section. If it contains non zero value - file contains managed code

Vadym Stetsyak said:
Hello, martin!

m> How can I tell (from C# code) whether a .DLL contains native code or is
m> infact a .NET assembly?

m> I know that I could use Assembly.LoadFile() and see if it throws a
m> BadImageFormatException but I really don't want any exceptions in my
m> normal program flow...

You will have to analyze file PE format
( http://www.samspublishing.com/articles/article.asp?p=25350&rl=1 )
links in the article are broken, here valid ones
( http://msdn.microsoft.com/msdnmag/issues/02/02/PE/ )

Here is the sample how to do this
( http://geekswithblogs.net/rupreet/archive/2005/11/02/58873.aspx )

--
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
I know that I could use Assembly.LoadFile() and see if it throws a
BadImageFormatException but I really don't want any exceptions in my normal
program flow...

That's why you catch it.

And yet that's the only way to be sure. You can reinvent the wheel, but it
wouldn't be as good/accurate/fast as Microsoft's, and it may need to be
updated in the (unlikely?) event that new DLL formats are created.

Microsoft gave you a tool, use it.
 
Back
Top