-1
我創建了一個函數,允許我用當前打開的進程從內存中的指針返回一個泛型類型。ReadProcessMemory在多次調用後返回False
public static T ReadValue<T>(this IntPtr ptr, int length = 0)
{
Type valT = typeof(T);
byte[] buffer = length > 0 ? new byte[length] : new byte[Marshal.SizeOf(valT)];
IntPtr retBytes;
object result;
if (!ReadProcessMemory(CurrentProcessHandle, ptr, buffer, buffer.Length, out retBytes))
return default(T);
Console.WriteLine(ptr);
if (valT == typeof(byte))
result = buffer[0];
else if (valT == typeof(bool))
result = buffer[0] > 0;
else if (valT == typeof(char))
result = BitConverter.ToChar(buffer, 0);
else if (valT == typeof(double))
result = BitConverter.ToDouble(buffer, 0);
else if (valT == typeof(float))
result = BitConverter.ToSingle(buffer, 0);
else if (valT == typeof(int))
result = BitConverter.ToInt32(buffer, 0);
else if (valT == typeof(long))
result = BitConverter.ToInt64(buffer, 0);
else if (valT == typeof(object))
result = buffer;
else if (valT == typeof(short))
result = BitConverter.ToInt16(buffer, 0);
else if (valT == typeof(string))
result = Encoding.Default.GetString(buffer);
else if (valT == typeof(uint))
result = BitConverter.ToUInt32(buffer, 0);
else if (valT == typeof(ulong))
result = BitConverter.ToUInt64(buffer, 0);
else if (valT == typeof(ushort))
result = BitConverter.ToUInt16(buffer, 0);
else
{
IntPtr newVal = Marshal.AllocHGlobal(buffer.Length);
Marshal.Copy(buffer, 0, newVal, buffer.Length);
result = newVal;
Marshal.FreeHGlobal(newVal);
}
return (T) result;
}
正在發生的事情是,經過〜39600個電話,ReadProcessMemory
開始在以下呼叫100%返回false。起初,我認爲這是IntPtr被複制和釋放之前被不恰當地分配;但是,在刪除IntPtr默認值後,問題仍然存在。這麼多次被調用後會導致這個返回錯誤的是什麼?
額外的編碼信息:
是調用了ReadValue的對象。
我所在的地方分配的IntPtr代碼:這是無效的句柄
GameObject player = new GameObject().GetObjectFromIndex(1805);
使用['GetLastError'](https://msdn.microsoft.com/en-us/library/windows/desktop/ms679360(v = vs.85).aspx)無法找到更多關於問題。 – cubrr
啊。謝謝你推薦我。它拋出錯誤代碼(0x6),這是無效句柄。顯然模塊的句柄正在改變,所以我的指針不再正確。我感謝提示:) – Approved
不錯!一定要回答你的問題。 – cubrr