Can we check if a running application or a program uses .Net framework to execute itself?
Use System.Reflection.Assembly.LoadFrom
function to load the .exe file. This function will throw exception if you try to load binary file that is not .NET assembly.
Programmatically you'd get the starting image name using Win32 API like NtQueryInformationProcess
, or in .Net use System.Diagnostics.Process.GetProcesses()
and read Process.StartInfo.FileName
.
Then open and decode the PE headers of that image using details prescribed in the MSDN article below:
http://msdn.microsoft.com/en-us/magazine/cc301808.aspx
Caveats: will only detect .NET built assemblies e.g. won't detect Win32 EXEs dynamically hosting CLR using CorHost APIs.
There's a trick I once learned from Scott Hanselman's list of interview questions. You can easily list all programs running .NET in command prompt by using:
tasklist /m "mscor*"
It will list all processes that have mscor*
amongst their loaded modules.
We can apply the same method in code:
public static bool IsDotNetProcess(this Process process)
{
var modules = process.Modules.Cast<ProcessModule>().Where(
m => m.ModuleName.StartsWith("mscor", StringComparison.InvariantCultureIgnoreCase));
return modules.Count() > 0;
}
Use the CLR COM interfaces ICorPublish and ICorPublishProcess. The easiest way to do this from C# is to borrow some code from SharpDevelop's debugger, and do the following:
ICorPublish publish = new ICorPublish();
ICorPublishProcess process;
process = publish.GetProcess(PidToCheck);
if (process == null || !process.IsManaged)
{
// Not managed.
}
// Managed.
I suggest downloading the Redgate's DotNetReflector and checking if it can open the application.