Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grabbing the process id/name that executed my managed assembly/hosts CLR runtime (CLR runtime host location)

Tags:

c#

I'm using some code to start the CLR Runtime inside a native process, then call my .NET DLL to load another .NET executable inside that process.

To load the other .NET executable I'm using reflection, like so:

Assembly.Load(file).EntryPoint.Invoke(null, null);

Now, I inject a C++ dll that starts the runtime, it then calls the .NET dll which uses Assembly.Load to load another .NET executable into memory and execute it.

Once the dll loads my .NET executable, calling:

System.Reflection.Assembly.GetExecutingAssembly().Location;

or even

Process.GetCurrentProcess().MainModule.FileName;

This, of course, returns the location of the executable itself, even though its running inside another host process. Is there any way I can grab the name of that host process? Process explorer shows it as running inside the host process so I know I have that part working correctly.

Of course if I were to run these commands inside the .NET DLL that was loaded first then it would show the proper process name.

Thanks.

EDIT:

I have tried GetEntryAssembly() and GetCallingAssembly() as well.

like image 324
cronzik Avatar asked Oct 11 '22 06:10

cronzik


1 Answers

DON'T DO THAT.

You may not inject the .NET framework into any process that is not yours. It might have a different framework already loaded or desire to load a different framework version later.

You can get the name of the host process by calling GetModuleFileName(GetModuleHandle(NULL)); (Both API calls are P/Invoke. [1] and [2]).

like image 79
Joshua Avatar answered Oct 13 '22 01:10

Joshua