Home All Groups Group Topic Archive Search About

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

Author
19 May 2006 8:17 AM
martin
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

Author
19 May 2006 8:40 AM
Vadym Stetsyak
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
Are all your drivers up to date? click for free checkup

Author
19 May 2006 9:32 AM
Michael Nemtsev
One more way to check PE for managed code is "COM Descriptor Directory"
section. If it contains non zero value - file contains managed code

Show quoteHide quote
"Vadym Stetsyak" wrote:

> 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

--
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
Author
19 May 2006 2:53 PM
PIEBALD
> 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.



Post Thread options