How to know if a ".exe" process was written with C++ or C#?
Solution 1
If you're trying to determine if a process is a .NET process, I can suggest a solution inspired from Dave Van den Eynde's answer in this topic: How do I tell if a win32 application uses the .NET runtime
"An application is a .NET executable if it requires mscoree.dll to run.".
Given that, you check the process' modules to see if mscoree is listed.
foreach (var process in Process.GetProcesses())
{
if (process.Modules.OfType<ProcessModule>().Any(m => m.ModuleName.Equals("mscoree.dll", StringComparison.OrdinalIgnoreCase)))
{
Console.WriteLine("{0} is a .NET process", process.ProcessName);
}
}
Solution 2
Here you can find details how it can be done: Determining Whether a DLL or EXE Is a Managed Component
Solution 3
You can run peverify or ildasm (available from the Visual Studio Command Line environment) on the file and it will give you an error if it's not managed code. Note that this will fail in some cases where they use a protection system that encrypts the IL and hides the CLR bootloader, but most programs don't have this protection.
Solution 4
If it's a .NET .exe (or .dll for that matter), it will have a dependency on mscoree.dll which you can see by examining it with Dependency Walker or something similar.
If you want to do so programatically, you could take this VB project as a starting point.
This won't tell you the language for sure though. You can't tell a C# from a VB.NET (or other .NET language) program or a C++ from a C program (or other non .NET language). There are some things that can rule one or the other out as being the only language used, or make one or the other more likely. There could even be unmanaged code making use of mscoree.dll in some weird way.
Comments
-
Diogo almost 2 years
Possible Duplicate:
How do I tell if a win32 application uses the .NET runtimeThere is a way to manually recognize if a specific ".exe" process was written with C++(unmanaged code) or with C#(managed code)?
-
Kevin Gosse over 12 yearsI can already say that there is a way, since Process Explorer does that. Now we have to find how :p
-
jp2code over 12 yearsTry hitting the exe with [Reflector][1] to see what turns up. [1]: stackoverflow.com/questions/214764/…
-
C.Evenhuis over 12 yearsNote that programs written in VB.NET generate the same "type" of executable as those written in C#, and you can only guess what was used to create the unmanaged application. As the others mentioned, you can distinguish unmanaged from managed code.
-
shraysalvi over 12 yearsBy writing your own code or using other tools ?
-
Lynn Crumbling over 12 yearsAre you looking to do this programmatically or as a manual operation?
-
John Dibling over 12 yearsI don't know, so this is not an answer. Is it possible to check the dependancies? I would guess if there are .NET runtime dependancies, then it must be C#?
-
Nawaz over 12 years.NET executables are usually big :P
-
BlueRaja - Danny Pflughoeft over 12 years
-
-
Diogo over 12 yearsDoes "mscoree.dll" is the main dll resource of a C# managed code binnary? Can I trust that there is no C++ binary that uses the same dll?
-
Kevin Gosse over 12 yearsIt is not a managed library, but everything in this dll is related to the .NET Framework. It does not guarantee that the process is a pure .NET application, but at the very least it's hosting some .NET code. For instance, if you load a .NET add-in in Microsoft Word, this dll will appear in the modules, even though Word isn't a .NET application by itself.
-
dragonfly02 about 7 yearsWhy a console app written in C# doesn't show as a managed app when using this way to check?
-
dragonfly02 about 7 yearsWhile this is a good answer but comparing string using
==
can be misleading because some process module name isMSCOREE.DLL
. Changing==
tom.ModuleName.Equals("mscoree.dll", StringComparison.InvariantCultureIgnoreCase)
is a safer choice. -
Kevin Gosse about 7 years@stt106 Good point, thanks. I've edited the answer
-
SoLaR over 5 yearsyour example code it self is .Net c# and is using mscoree.dll, also if it is using COM that is written in .Net will also load mscoree.dll, etc... Only way I see is to examine process image and check if PE IMAGE HEADER and has IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR descriptor set. see answer from HABJAN
-
SoLaR over 5 yearsLink describes proper way