问题
An architecture which I have to deal with is quite complicated: a native C++ Windows Service creates an out-of-process COM object (native C++, CoCreateInstance with context = CLSCTX_LOCAL_SERVER) which dynamically loads (AfxLoadLibrary) my C# DLL.
I would like to debug my DLL when I click Debug -> Start Debugging.
I know that I can debug it when I attach to COM object process using Debug -> Attach to process and I can use System.Diagnostics.Debugger.Launch()
in my DLL code if I need to debug startup but none of this options work automatically.
I also tried this technique: https://docs.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2010/a329t4ed(v=vs.100) but the COM process runs as a SYSTEM user so that I cannot select my existing Visual Studio Solution with my C# project to use as a debugger, I can only start a new instance with only EXE loaded.
I tried "Start external program" too but what I need to start is a service which is not the process I want to debug, because my DLL runs in another process (the COM-one). So, debugging stops as soon as service is started (because I run it with a script, maybe this is a problem).
回答1:
I don't see an option to automate it entirely with standard Visual Studio features. So I would propose you to write a simple Visual Studio extension. Just use the template and add a button to the menu or to some custom window, which on click will:
- iterate through the running processes and wait until an instance of the dllhost.exe with your COM class guid on the command line, appears
- when you find the process, attach to it using the Process2 interface (code in VB is available)
I'm not sure if it will help, but you may check my simple VS extension. I wrote it some time ago to configure symbols in the debugger.
来源:https://stackoverflow.com/questions/51879324/how-do-i-attach-a-debugger-to-other-process-than-i-start-in-visual-studio-2017