开发者

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

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.


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]).


IF I understand your question correctly then you want to find the "parent process"... There are different ways to do that, either managed or via P/Invoke... for nice code samples on how to do this see How to get parent process in .NET in managed way

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜