|
|
|
date: Tue, 22 Jul 2008 07:13:01 -0700,
group: microsoft.public.dotnet.framework
back
RE: Getting information from a process
Hi Scott,
I am not a MSMQ expert, but I want to share my experience on .Net process
programming.
Do you start the remote processes using Process.Start()? If so, .Net
Process class will store the reference to the ProcessStartInfo in the
Process.StartInfo property, so we can retrieve it later. For example, the
following code snippet demonstrates the logic and works well on my side:
Process m_RemoteProcess;
private void button1_Click(object sender, EventArgs e)
{
ProcessStartInfo psi = new ProcessStartInfo(@"C:\windows\notepad.exe",
@"D:\msjeff2008_services.txt");
m_RemoteProcess = Process.Start(psi);
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show("The command line argument for the process is: " +
m_RemoteProcess.StartInfo.Arguments);
}
However, if the remote processes are not started by your code, the .Net
Process class has no chance to store the ProcessStartInfo structure. Then,
we can not retrieve it this way; we have to find another way to get this
done.
From the OS point of view, the CommandLine is stored in a private structure
that is accessible via the PEB. Windows provides GetCommandLine() API to
retrieve command line argument for current process, however, there is no
API to retrieve the command line of another process since different
processes have different virtual address space.
Fortunately, WMI exposes Win32_Process class which contains the CommandLine
property. In .Net, we can use the classes in System.Management namespace to
invoke WMI. The code snippet below dumps the command line of all the
notepad.exe processes:
using System.Management;
private void button1_Click(object sender, EventArgs e)
{
ManagementObjectSearcher mos = new
ManagementObjectSearcher(@"\root\cimv2", "Select * from Win32_Process where
name = \"notepad.exe\"");
foreach (ManagementObject mo in mos.Get())
{
MessageBox.Show("Command Line : " + mo["CommandLine"].ToString());
}
}
Hope this helps.
Best regards,
Jeffrey Tan
Microsoft Online Community Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@microsoft.com.
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
date: Wed, 23 Jul 2008 01:50:25 GMT
author: (Jeffrey Tan[MSFT])
RE: Getting information from a process
Jeffery,
Thanks for this, works just fine!
I keep forgetting about WMI, in my mind it is for static machine
configuration values but (obviously) it is much more.
--
Scott
""Jeffrey Tan[MSFT]"" wrote:
> Hi Scott,
>
> I am not a MSMQ expert, but I want to share my experience on .Net process
> programming.
>
> Do you start the remote processes using Process.Start()? If so, .Net
> Process class will store the reference to the ProcessStartInfo in the
> Process.StartInfo property, so we can retrieve it later. For example, the
> following code snippet demonstrates the logic and works well on my side:
>
> Process m_RemoteProcess;
> private void button1_Click(object sender, EventArgs e)
> {
> ProcessStartInfo psi = new ProcessStartInfo(@"C:\windows\notepad.exe",
> @"D:\msjeff2008_services.txt");
> m_RemoteProcess = Process.Start(psi);
> }
>
> private void button2_Click(object sender, EventArgs e)
> {
> MessageBox.Show("The command line argument for the process is: " +
> m_RemoteProcess.StartInfo.Arguments);
> }
>
> However, if the remote processes are not started by your code, the .Net
> Process class has no chance to store the ProcessStartInfo structure. Then,
> we can not retrieve it this way; we have to find another way to get this
> done.
>
> From the OS point of view, the CommandLine is stored in a private structure
> that is accessible via the PEB. Windows provides GetCommandLine() API to
> retrieve command line argument for current process, however, there is no
> API to retrieve the command line of another process since different
> processes have different virtual address space.
>
> Fortunately, WMI exposes Win32_Process class which contains the CommandLine
> property. In .Net, we can use the classes in System.Management namespace to
> invoke WMI. The code snippet below dumps the command line of all the
> notepad.exe processes:
>
> using System.Management;
> private void button1_Click(object sender, EventArgs e)
> {
> ManagementObjectSearcher mos = new
> ManagementObjectSearcher(@"\root\cimv2", "Select * from Win32_Process where
> name = \"notepad.exe\"");
> foreach (ManagementObject mo in mos.Get())
> {
> MessageBox.Show("Command Line : " + mo["CommandLine"].ToString());
> }
> }
> Hope this helps.
>
> Best regards,
> Jeffrey Tan
> Microsoft Online Community Support
>
> Delighting our customers is our #1 priority. We welcome your comments and
> suggestions about how we can improve the support we provide to you. Please
> feel free to let my manager know what you think of the level of service
> provided. You can send feedback directly to my manager at:
> msdnmg@microsoft.com.
> ==================================================
> Get notification to my posts through email? Please refer to
> http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
> ications.
>
> Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
> where an initial response from the community or a Microsoft Support
> Engineer within 1 business day is acceptable. Please note that each follow
> up response may take approximately 2 business days as the support
> professional working with you may need further investigation to reach the
> most efficient resolution. The offering is not appropriate for situations
> that require urgent, real-time or phone-based interactions or complex
> project analysis and dump analysis issues. Issues of this nature are best
> handled working with a dedicated Microsoft Support Engineer by contacting
> Microsoft Customer Support Services (CSS) at
> http://msdn.microsoft.com/subscriptions/support/default.aspx.
> ==================================================
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
>
>
date: Wed, 23 Jul 2008 11:15:00 -0700
author: Scott am
|
|