Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Is there any accurate way to determine if an assembly (actually, the current process/assembly) is running GACed/MSIL or Native ?

I have successfully tested a few assemblies with the fuslogvw.exe utility, and I can determine manually if the running program was loaded from a native image or not, but I want to be able to determine that at runtine.

So far, I have tested a few approaches, but none of them seems to be valid.

1) Existence of the clrjit.dll module Loaded... wrong, it's ALWAYS loaded, native or gaced.

2) Existence of the mscorjit.dll module loaded... wrong, its NEVER loaded, or at least, not as a part of the process modules

3) If Reflection.Assembly.GetExecutingAssembly.GlobalAssemblyCache , wrong, as it tells if the assemly is in the GAC, but not if its native or not... or at least, its always returning false on me.


Has anyone else got any approach with success on this?
Posted
Updated 7-Feb-11 23:45pm
v2
Comments
Dalek Dave 8-Feb-11 5:45am    
Edited for Grammar.

1 solution

Looks like no one knows of an elegant or accurate solution, but i have come with an approach, that might not be bright, but at least seems to be accurate, somehow. and i decided to share it with everyone.

According to my researches, .NET native images are identified, in a partial way, by the .ni. name, so, System.Forms.dll has a System.Forms.ni.dll native image, and then, MyApplication.exe has an MyApplication.ni.exe native image, if exist.

Also, to give us another hint, we know that all native images are located on the assembly directory under the %windir%.

Now, we can just query all the modules loaded by our application, and see if one of them is "our native file" application name.

VB
Dim ImNative As Boolean = False
Dim MyFile As String = IO.Path.GetFileName(Application.ExecutablePath).ToLower.Replace(".exe", ".ni.exe").Trim
For Each m As ProcessModule In Process.GetCurrentProcess.Modules
  If MyFile.ToLower = IO.Path.GetFileName(m.FileName).ToLower AndAlso m.FileName.ToLower.Contains("\assembly\nativeimages") Then
    ImNative = True : Exit For
  End If
Next

If ImNative Then Msgbox("Yes, you are running a native image of this assembly.")
 
Share this answer
 
Comments
Dalek Dave 8-Feb-11 5:45am    
Good Call.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900