/// <summary> /// 写内存小数型 /// </summary> /// <param name="pID">进程ID</param> /// <param name="bAddress">0x地址</param> /// <param name="value">写入数据</param> /// <returns>false失败</returns> public static bool WriteMemoryFloat(int pID, int bAddress, float value) { //byte[] byteSet = MProcess.GetByteSet(value); byte[] byteSet = BitConverter.GetBytes(value); //https://msdn.microsoft.com/en-us/library/yhwsaf3w //byte[] byteSet = Encoding.GetEncoding("gb2312").GetBytes(value.ToString()); return(MProcess.WriteMemoryByteSet(pID, bAddress, byteSet, 0)); }
/// <summary> /// 字节集转小数型 /// </summary> /// <param name="sourceValue">字节集</param> /// <param name="index">索引</param> /// <returns></returns> public static float GetFloatFromByteSet(byte[] sourceValue, int index) { float result = 0f; MProcess._CopyMemory_ByteSet_Float(ref result, ref sourceValue[index], 4); return(result); }
/// <summary> /// 写内存整数型 /// </summary> /// <param name="pID">进程ID</param> /// <param name="bAddress">0x地址</param> /// <param name="value">写入值</param> /// <returns>false失败 true成功</returns> public static bool WriteMemoryInt32(int pID, int bAddress, int value) { int handle = GetProcessHandle(pID); int num2 = MProcess._MemoryWriteInt32(handle, bAddress, ref value, 4, 0); MProcess.CloseHandle(handle); return(num2 != 0); }
/// <summary> /// 写内存字节集 /// </summary> /// <param name="pID">进程ID</param> /// <param name="bAddress">0x地址</param> /// <param name="value">字节数据</param> /// <param name="length">写入长度 0代表字节数据的长度</param> /// <returns>false失败</returns> private static bool WriteMemoryByteSet(int pID, int bAddress, byte[] value, int length = 0) { int handle = MProcess.GetProcessHandle(pID); int nSize = (length == 0) ? value.Length : length; int tmp = MProcess._MemoryWriteByteSet(handle, bAddress, value, nSize, 0);//byte[]属于引用类型 引用类型不用ref也是以传址方式进行运算 //MProcess.CloseHandle(pID); return(tmp != 0); }
/// <summary> /// 取进程句柄 /// </summary> /// <param name="pID">进程ID</param> /// <returns>进程句柄</returns> public static int GetProcessHandle(int pID) { if (pID == -1) { return(MProcess.GetCurrentProcess()); } else { return(MProcess.OpenProcess(PROCESS_POWER_MAX, 0, pID)); } }
/// <summary> /// 读内存小数型 /// </summary> /// <param name="pID">进程ID</param> /// <param name="bAddress">0x地址</param> /// <returns>0失败</returns> public static float ReadMemoryFloat(int pID, int bAddress) { //byte[] array = MProcess.GetVoidByteSet(4); byte[] array = new byte[4];//不取空字节集也可以正确转换成单精度小数型 int handle = GetProcessHandle(pID); int temp = MProcess._MemoryReadByteSet(handle, bAddress, array, 4, 0); if (temp == 0) { return(0f); } else { return(MProcess.GetFloatFromByteSet(array, 0)); } }
/// <summary> /// 读内存整数型 /// </summary> /// <param name="pID">进程ID</param> /// <param name="bAddress">0x地址</param> /// <returns>0失败</returns> public static int ReadMemoryInt32(int pID, int bAddress) { int num = 0; int handle = GetProcessHandle(pID); int num3 = MProcess._MemoryReadInt32(handle, bAddress, ref num, 4, 0); MProcess.CloseHandle(handle); if (num3 == 0) { return(0); } else { return(num); } }