111,126
社区成员
发帖
与我相关
我的任务
分享using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Reflection;
using System.Runtime;
using System.Runtime.InteropServices;
namespace Test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[StructLayout(LayoutKind.Sequential)]
private struct PROCESSENTRY32
{
public uint dwSize;
public uint cntUsage;
public uint th32ProcessID;
public IntPtr th32DefaultHeapID;
public uint th32ModuleID;
public uint cntThreads;
public uint th32ParentProcessID;
public int pcPriClassBase;
public uint dwFlags;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x100)]
public string szExeFile;
}
[DllImport("Kernel32.dll")]
private static extern int CloseToolhelp32Snapshot(IntPtr snaph);
[DllImport("Kernel32.dll")]
private static extern int CloseHandle(IntPtr hObject);
[DllImport("Kernel32.dll")]
private static extern IntPtr CreateToolhelp32Snapshot(uint dwFlags, uint th32ProcessID);
[DllImport("Kernel32.dll")]
private static extern int Process32First(int snaph, ref PROCESSENTRY32 lppe);
[DllImport("Kernel32.dll")]
private static extern int Process32Next(IntPtr snaph, ref PROCESSENTRY32 lppe);
[DllImport("Kernel32.dll")]
private static extern bool TerminateProcess(IntPtr h, uint c);
private void button1_Click(object sender, EventArgs e)
{
IntPtr snaph = CreateToolhelp32Snapshot(2, 0);
PROCESSENTRY32 lppe = new PROCESSENTRY32();
lppe.dwSize = 0x400;
if (Process32First((int)snaph, ref lppe) == 0)
{
CloseToolhelp32Snapshot(snaph);
}
while (Process32Next(snaph, ref lppe) != 0)
{
this.listBox1.Items.Add(lppe.szExeFile);
}
}
}
}
using System;
using System.Diagnostics;
using System.ComponentModel;
namespace MyProcessSample
{
/// <summary>
/// Shell for the sample.
/// </summary>
class MyProcess
{
void BindToRunningProcesses()
{
// Get the current process.
Process currentProcess = Process.GetCurrentProcess();
// Get all instances of Notepad running on the local
// computer.
Process [] localByName = Process.GetProcessesByName("notepad");
// Get all instances of Notepad running on the specifiec
// computer.
// 1. Using the computer alias (do not precede with "\\").
Process [] remoteByName = Process.GetProcessesByName("notepad", "myComputer");
// 2. Using an IP address to specify the machineName parameter.
Process [] ipByName = Process.GetProcessesByName("notepad", "169.0.0.0");
// Get all processes running on the local computer.
Process [] localAll = Process.GetProcesses();
// Get all processes running on the remote computer.
Process [] remoteAll = Process.GetProcesses("myComputer");
// Get a process on the local computer, using the process id.
Process localById = Process.GetProcessById(1234);
// Get a process on a remote computer, using the process id.
Process remoteById = Process.GetProcessById(2345, "myComputer");
}
static void Main()
{
MyProcess myProcess = new MyProcess();
myProcess.BindToRunningProcesses();
}
}
}