5

A bit of searching returns this result: Which processes are running managed code and which version?

However I am wondering if there is a 'better' way then simply iterating though the loaded modules? It seems a little quirky to look for the string "mscorwks.dll". Reading though the Process Class on MSDN doesn't seem to point out an obvious solution.

Assumptions Made

  1. .NET 4.0
  2. I have a "Process" in hand

Thank you

Dirk Vollmar
  • 172,527
  • 53
  • 255
  • 316
aolszowka
  • 1,300
  • 12
  • 36
  • Define "managed code". Java is managed, interpreted languages are managed as well. Are you interested specifically in .NET assemblies or any type of managed code? – Ed S. Feb 14 '11 at 22:29
  • I don't see it as quirky. That's the filename of the core library dll. –  Feb 14 '11 at 22:30
  • @Ed S. Apologies, by 'Managed Code' I mean code running on top of the CLR. @yodaj007 Is that guaranteed to be the same across all versions of the .NET Framework and for all languages? – aolszowka Feb 14 '11 at 22:31
  • 2
    Are you interested in whether the main executable is a .NET assembly, or just whether the process in question hosts a CLR? The latter is the case for managed add-ins in e.g. Windows Explorer or Office applications. And do you need to do this programmatically? Otherwise Process Explorer will highlight all processes hosting a .NET CLR. – Dirk Vollmar Feb 14 '11 at 22:49
  • @0xA3 Interesting question, and one I had forgotten all about. If possible I'd like to detect both scenarios. In essence if I could programmatically duplicate what ProcessExplorer does to determine if the process is hosting the .NET CLR that would be ideal. – aolszowka Feb 16 '11 at 20:07
  • 1
    Meanwhile you might want to check out this question: http://stackoverflow.com/questions/2080046/how-to-check-if-a-program-is-using-net – Dirk Vollmar Feb 17 '11 at 19:41

2 Answers2

6

For any future Googlers: I ended up using the suggested answer posted here How to check if a program is using .NET? (thanks 0xA3!)

Process mProcess = //Get Your Process Here
foreach (ProcessModule pm in mProcess.Modules)
{
    if (pm.ModuleName.StartsWith("mscor", StringComparison.InvariantCultureIgnoreCase))
    {
        return true;
    }
}

As an aside looking for "mscorwks.dll" as mentioned in my original post does not work for .NET 4.0.

Community
  • 1
  • 1
aolszowka
  • 1,300
  • 12
  • 36
  • 1
    Or in a single LINQ expression: `return process.Modules.Cast().Any(module => module.ModuleName.StartsWith("mscor", StringComparison.InvariantCultureIgnoreCase));`. – MasterMastic Jul 28 '13 at 12:10
1

In code, get the full path of the executing process. Try to use Assembly.Load on the process. If it works, it's a .Net assembly :)

Steve B
  • 36,818
  • 21
  • 101
  • 174