问题
I have small issue. I have application using latest installed IE engine. Sometimes it might encounter issue and when it's happen .NET opens default browser with error (that SSL has to be enabled to connect with server, I'm using proxies).
I want to suppress any external browsers from showing because of my application.
I know that I can start browser using Process.Start and stop. But how to recognise that new process is started by my application (is there any parent of process or Tree ?)
回答1:
Ok, here's how you can retreive the PPID (parent process id) of any given process via the WMI wrapper System.Management
First you'll have to add a reference to System.Management
obviously.
Then you'll have to query for the process in question:
Dim pid As Int32 = Process.GetCurrentProcess.Id
Dim query As SelectQuery = New SelectQuery(String.Format("select * from Win32_Process where ProcessId = {0}", pid))
Next you need to feed the query to a ManagementObjectSearcher and put the results into a ManagementObjectCollection
Dim mobjSearcher As ManagementObjectSearcher = New ManagementObjectSearcher(query)
Dim mobjProcesses As ManagementObjectCollection = mobjSearcher.Get()
Finally select the first (and only, hopefully) process and it's ParentProcessId
Dim ppid As Int32 = mobjProcesses(0)("ParentProcessId")
Unfortunatly the ManagementObjectCollection
doesn't know what type of object it's enumerating, so you'll eighter have to look it up and cast it or be satisfied with late binding at this point.
There - of course - is a way to do this nativly via the Windows API. That way you'll have to call the NtQueryInformationProcess from the ntdll.dll
When calling the function you'd pass it a PROCESS_BASIC_INFORMATION struct along with the process handle and such.
On successfull return you would find the parent PID in the IntPtr InheritedFromUniqueProcessId
field of the stuct (which you'd have to cast to Int32
)
Now that I have answered the question "How do I retreive the parent PID of a process" I just realized that, though it might help you in your endevour to identify any browser process that is started unwillingly by your application... this most likly will not eliminate the underlying design flaw of said application.
It might be the answer X' to an XY Problem
来源:https://stackoverflow.com/questions/22603971/how-to-suppress-default-browser-from-opening-while-vb-net-webbrowser-encounter-s