public MemoryResult ReadArray <T>(IntPtr address, int count, out T[] outArray) { ResetError(); if (count <= 0) { outArray = default(T[]); return(LastMemoryResult); } //Read in the number of bytes required for each array index int cnt; byte[] buffer; MemoryResult result = ReadMemory(address, Marshal.SizeOf(typeof(T)) * count, out cnt, out buffer); // Calls error handler if needed so we do not have to here if (cnt > 0) { GCHandle pinned = GCHandle.Alloc(buffer, GCHandleType.Pinned); try { //Read in the structure at each position and coerce it into its slot var output = new T[count]; IntPtr current = pinned.AddrOfPinnedObject(); for (var i = 0; i < count; i++) { output[i] = (T)Marshal.PtrToStructure(current, typeof(T)); current = (IntPtr)((int)current + Marshal.SizeOf(output[i])); //advance to the next index } // @ for (int i = 0; i < Count; i++) outArray = output; return(result); #if DEBUG } catch (Exception ex) { Debug.WriteLine("ReadStruct: Unable to coerce foreign data into <" + typeof(T).ToString() + ">: " + ex.Message); #else } catch { #endif } finally { pinned.Free(); } } outArray = default(T[]); return(result); }
private void HandleError(string details) { _lastErrorSystemCode = MemoryApi.GetLastError(); if (Enum.IsDefined(typeof(MemoryResult), (int)_lastErrorSystemCode)) { _lastMemoryResult = (MemoryResult)_lastErrorSystemCode; } else { _lastMemoryResult = MemoryResult.Other; } if (_errorHandler != null) { _errorHandler(_lastMemoryResult, _lastErrorSystemCode, details); } }
public MemoryResult ReadStruct <T>(IntPtr address, out T outStruct) { ResetError(); var cnt = 0; byte[] buffer; MemoryResult result = ReadMemory(address, Marshal.SizeOf(typeof(T)), out cnt, out buffer); // Will call error handler if needed so we do not have to if (cnt > 0) { GCHandle pinned = GCHandle.Alloc(buffer, GCHandleType.Pinned); try { outStruct = (T)Marshal.PtrToStructure(pinned.AddrOfPinnedObject(), typeof(T)); return(result); #if DEBUG } catch (Exception ex) { Debug.WriteLine("ReadStruct: Unable to coerce foreign data into <" + typeof(T).ToString() + ">: " + ex.Message); #else } catch { #endif } finally { pinned.Free(); } } outStruct = default(T); return(result); }
public MemoryResult ReadString(IntPtr address, int maxLen, out string outString) { ResetError(); // read the memory into a buffer int cnt; byte[] buffer; MemoryResult result = ReadMemory(address, maxLen, out cnt, out buffer); // Calls error handler if needed so we do not have to here if (cnt > 0) { GCHandle pinned = GCHandle.Alloc(buffer, GCHandleType.Pinned); try { outString = Marshal.PtrToStringAnsi(pinned.AddrOfPinnedObject()); return(result); #if DEBUG } catch (Exception ex) { Debug.WriteLine("ReadString: Unable to coerce foreign data into string: " + ex.Message); #else } catch { #endif } finally { pinned.Free(); } } outString = String.Empty; return(result); }
private void ResetError() { _lastErrorSystemCode = 0; _lastMemoryResult = MemoryResult.Success; }
public void Handle(MemoryResult message) { AppendToLastOutputLine(message.ConsumedMemory / 1024 + "KB"); }