#region Process class
/// <summary>
/// Summary description for Process.
/// </summary>
public class Process
{
private string processName;
private IntPtr handle;
private int threadCount;
private int baseAddress; //default constructor
public Process()
{ } private Process(IntPtr id, string procname, int threadcount, int baseaddress)
{
handle = id;
processName = procname;
threadCount = threadcount;
baseAddress = baseaddress;
} //ToString implementation for ListBox binding
public override string ToString()
{
return processName;
} public int BaseAddress
{
get
{
return baseAddress;
}
} public int ThreadCount
{
get
{
return threadCount;
}
} public IntPtr Handle
{
get
{
return handle;
}
} public string ProcessName
{
get
{
return processName;
}
} public int BaseAddess
{
get
{
return baseAddress;
}
} public void Kill()
{
IntPtr hProcess; hProcess = OpenProcess(PROCESS_TERMINATE, false, (int) handle); if(hProcess != (IntPtr) INVALID_HANDLE_VALUE)
{
bool bRet;
bRet = TerminateProcess(hProcess, 0);
CloseHandle(hProcess);
} } public static Process[] GetProcesses()
{
ArrayList procList = new ArrayList(); IntPtr handle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if ((int)handle > 0)
{
try
{
PROCESSENTRY32 peCurrent;
PROCESSENTRY32 pe32 = new PROCESSENTRY32();
//Get byte array to pass to the API calls
byte[] peBytes = pe32.ToByteArray();
//Get the first process
int retval = Process32First(handle, peBytes); while (retval == 1)
{
//Convert bytes to the class
peCurrent = new PROCESSENTRY32(peBytes);
//New instance
Process proc = new Process(new IntPtr((int)peCurrent.PID), peCurrent.Name, (int)peCurrent.ThreadCount, (int)peCurrent.BaseAddress); procList.Add(proc); retval = Process32Next(handle, peBytes);
}
}
catch (Exception ex)
{
throw new Exception("Exception: " + ex.Message);
} //Close handle
CloseToolhelp32Snapshot(handle); return (Process[])procList.ToArray(typeof(Process)); }
else
{
throw new Exception("Unable to create snapshot");
} } #region PROCESSENTRY32 implementation // typedef struct tagPROCESSENTRY32
// {
// DWORD dwSize;
// DWORD cntUsage;
// DWORD th32ProcessID;
// DWORD th32DefaultHeapID;
// DWORD th32ModuleID;
// DWORD cntThreads;
// DWORD th32ParentProcessID;
// LONG pcPriClassBase;
// DWORD dwFlags;
// TCHAR szExeFile[MAX_PATH];
// DWORD th32MemoryBase;
// DWORD th32AccessKey;
// } PROCESSENTRY32; private class PROCESSENTRY32
{
// constants for structure definition
private const int SizeOffset = 0;
private const int UsageOffset = 4;
private const int ProcessIDOffset=8;
private const int DefaultHeapIDOffset = 12;
private const int ModuleIDOffset = 16;
private const int ThreadsOffset = 20;
private const int ParentProcessIDOffset = 24;
private const int PriClassBaseOffset = 28;
private const int dwFlagsOffset = 32;
private const int ExeFileOffset = 36;
private const int MemoryBaseOffset = 556;
private const int AccessKeyOffset = 560;
private const int Size = 564;
private const int MAX_PATH = 260; // data members
public uint dwSize;
public uint cntUsage;
public uint th32ProcessID;
public uint th32DefaultHeapID;
public uint th32ModuleID;
public uint cntThreads;
public uint th32ParentProcessID;
public long pcPriClassBase;
public uint dwFlags;
public string szExeFile;
public uint th32MemoryBase;
public uint th32AccessKey; //Default constructor
public PROCESSENTRY32()
{ } // create a PROCESSENTRY instance based on a byte array
public PROCESSENTRY32(byte[] aData)
{
dwSize = GetUInt(aData, SizeOffset);
cntUsage = GetUInt(aData, UsageOffset);
th32ProcessID = GetUInt(aData, ProcessIDOffset);
th32DefaultHeapID = GetUInt(aData, DefaultHeapIDOffset);
th32ModuleID = GetUInt(aData, ModuleIDOffset);
cntThreads = GetUInt(aData, ThreadsOffset);
th32ParentProcessID = GetUInt(aData, ParentProcessIDOffset);
pcPriClassBase = (long) GetUInt(aData, PriClassBaseOffset);
dwFlags = GetUInt(aData, dwFlagsOffset);
szExeFile = GetString(aData, ExeFileOffset, MAX_PATH);
th32MemoryBase = GetUInt(aData, MemoryBaseOffset);
th32AccessKey = GetUInt(aData, AccessKeyOffset);
} #region Helper conversion functions
// utility: get a uint from the byte array
private static uint GetUInt(byte[] aData, int Offset)
{
return BitConverter.ToUInt32(aData, Offset);
} // utility: set a uint int the byte array
private static void SetUInt(byte[] aData, int Offset, int Value)
{
byte[] buint = BitConverter.GetBytes(Value);
Buffer.BlockCopy(buint, 0, aData, Offset, buint.Length);
} // utility: get a ushort from the byte array
private static ushort GetUShort(byte[] aData, int Offset)
{
return BitConverter.ToUInt16(aData, Offset);
} // utility: set a ushort int the byte array
private static void SetUShort(byte[] aData, int Offset, int Value)
{
byte[] bushort = BitConverter.GetBytes((short)Value);
Buffer.BlockCopy(bushort, 0, aData, Offset, bushort.Length);
} // utility: get a unicode string from the byte array
private static string GetString(byte[] aData, int Offset, int Length)
{
String sReturn = Encoding.Unicode.GetString(aData, Offset, Length);
return sReturn;
} // utility: set a unicode string in the byte array
private static void SetString(byte[] aData, int Offset, string Value)
{
byte[] arr = Encoding.ASCII.GetBytes(Value);
Buffer.BlockCopy(arr, 0, aData, Offset, arr.Length);
}
#endregion // create an initialized data array
public byte[] ToByteArray()
{
byte[] aData;
aData = new byte[Size];
//set the Size member
SetUInt(aData, SizeOffset, Size);
return aData;
} public string Name
{
get
{
return szExeFile.Substring(0, szExeFile.IndexOf('\0'));
}
} public ulong PID
{
get
{
return th32ProcessID;
}
} public ulong BaseAddress
{
get
{
return th32MemoryBase;
}
} public ulong ThreadCount
{
get
{
return cntThreads;
}
}
}
#endregion #region PInvoke declarations
private const int TH32CS_SNAPPROCESS = 0x00000002;
[DllImport("toolhelp.dll")]
public static extern IntPtr CreateToolhelp32Snapshot(uint flags, uint processid);
[DllImport("toolhelp.dll")]
public static extern int CloseToolhelp32Snapshot(IntPtr handle);
[DllImport("toolhelp.dll")]
public static extern int Process32First(IntPtr handle, byte[] pe);
[DllImport("toolhelp.dll")]
public static extern int Process32Next(IntPtr handle, byte[] pe);
[DllImport("coredll.dll")]
private static extern IntPtr OpenProcess(int flags, bool fInherit, int PID);
private const int PROCESS_TERMINATE = 1;
[DllImport("coredll.dll")]
private static extern bool TerminateProcess(IntPtr hProcess, uint ExitCode);
[DllImport("coredll.dll")]
private static extern bool CloseHandle(IntPtr handle);
private const int INVALID_HANDLE_VALUE = -1; #endregion
} #endregion

代码来至:http://msdn.microsoft.com/en-us/library/aa446560.aspx

关于 windows mobile 进程操作的方法的更多相关文章

  1. 转载扩展Windows Mobile模拟器存储空间的方法

    扩展Windows Mobile模拟器存储空间的方法 在Windows Mobile应用程序开发的初期,可以使用SDK自带的模拟器来进行调试,这给我们开发人员提供了一种方便的途径.一般的应用程序,占用 ...

  2. Windows的拖放操作使用方法

    Windows的拖放操作使用方法

  3. 在Linux下和Windows下遍历目录的方法及如何达成一致性操作

    最近因为测试目的需要遍历一个目录下面的所有文件进行操作,主要是读每个文件的内容,只要知道文件名就OK了.在Java中直接用File类就可以搞定,因为Java中使用了组合模式,使得客户端对单个文件和文件 ...

  4. Windows提供了两种将DLL映像到进程地址空间的方法(隐式和显式)

    调用DLL,首先需要将DLL文件映像到用户进程的地址空间中,然后才能进行函数调用,这个函数和进程内部一般函数的调用方法相同.Windows提供了两种将DLL映像到进程地址空间的方法: 1. 隐式的加载 ...

  5. Windows提供了两种将DLL映像到进程地址空间的方法

    调用DLL,首先需要将DLL文件映像到用户进程的地址空间中,然后才能进行函数调用,这个函数和进程内部一般函数的调用方法相同.Windows提供了两种将DLL映像到进程地址空间的方法: 1. 隐式的加载 ...

  6. Day9 进程理论 开启进程的两种方式 多进程实现并发套接字 join方法 Process对象的其他属性或者方法 守护进程 操作系统介绍

    操作系统简介(转自林海峰老师博客介绍) #一 操作系统的作用: 1:隐藏丑陋复杂的硬件接口,提供良好的抽象接口 2:管理.调度进程,并且将多个进程对硬件的竞争变得有序 #二 多道技术: 1.产生背景: ...

  7. Win10下windows mobile设备中心连接不上的方法无法启动

    微软Win10自动更细补丁后windows mobile设备中心就无法启动了 需要重新启动相关的服务并授予 本机登录用户 权限 1.点击屏幕左下角“开始”图标,点击“运行”,在弹出的输入框中输入“se ...

  8. C#进程操作

    C#进程操作 转:http://www.cnblogs.com/vienna/p/3560804.html 一.C#关闭word进程  foreach (System.Diagnostics.Proc ...

  9. Windows Mobile和Wince(Windows Embedded CE)的字符集问题

    背景 开发过Windows Mobile和Wince(Windows Embedded CE)的开发者,特别是Native C++开发者,或多或少都遇到过ANSI字符集和Unicode字符集的转换问题 ...

随机推荐

  1. redhat5.8 alt+ctrl+f1 黑屏

    /********************************************************************** * redhat5.8 alt+ctrl+f1 黑屏 * ...

  2. 《Javascript高级程序设计》阅读记录(六):第六章 下

    这个系列以往文字地址: <Javascript高级程序设计>阅读记录(一):第二.三章 <Javascript高级程序设计>阅读记录(二):第四章 <Javascript ...

  3. 洛谷 P1854 花店橱窗布置

    题目描述 某花店现有F束花,每一束花的品种都不一样,同时至少有同样数量的花瓶,被按顺序摆成一行,花瓶的位置是固定的,从左到右按1到V顺序编号,V是花瓶的数目.花束可以移动,并且每束花用1到F的整数标识 ...

  4. BZOJ2120:数颜色(莫队版)

    浅谈莫队:https://www.cnblogs.com/AKMer/p/10374756.html 题目传送门:https://lydsy.com/JudgeOnline/problem.php?i ...

  5. CentOS7中配置基于Nginx+Supervisor+Gunicorn的Flask项目

    配置Nginx 1.安装nginx yum install nginx 2.安装好后在/etc/nginx/default.d中添加location的配置,并指向8001端口,以后Gunicorn会监 ...

  6. Nginx报错 connect() failed (111: Connection refused) while connecting to upstream 的解决方法

    今天访问公司的网站突然报错,抛出一些英文,提示看一下Nginx的error.log日志: cd  /usr/local/nginx/logs/  看到了error.log ,下一步 tail -n 2 ...

  7. JSF使用HTML5的custom attribute

    只需要在页面引用这样的命名空间: xmlns:pt="http://xmlns.jcp.org/jsf/passthrough"之后,在JSF的控件的html5属性加上前缀&quo ...

  8. 50 states of America

    美国州名 州名英文  州名音标 简写 首府 首府 阿拉巴马州 Alabama   [ˌæləˈbæmə] AL 蒙哥马利 Montgomery[mənt'gʌməri] 阿拉斯加州 Alaska  [ ...

  9. 【ZooKeeper怎么玩】之一:为什么需要ZK

    博客已经搬家,见[ZooKeeper怎么玩]之一:为什么需要ZK 学习新东西首先需要搞清楚为什么学它,这是符合我们的一个认知过程.<!--more-->#ZooKeeper是什么ZooKe ...

  10. Windows安装mysql 5.7.*.zip步骤

    1.去官网上下载.zip格式的文件. 2.解压到一个文件夹,这里我用D:\MySql表示 3.在D:\MySql\mysql-5.7.17-winx64下新建my.ini配置文件 黄色背景色的地方需要 ...