private void Marshal() { if (typeof(T) == typeof(string)) { var text = Value.ToString(); Allocated = m_Process.Memory.Allocate(text.Length + 1); Allocated.WriteString(0, text); Reference = Allocated.BaseAddress; } else { var byteArray = MarshalType <T> .ObjectToByteArray(Value); if (MarshalType <T> .CanBeStoredInRegisters) { Reference = MarshalType <IntPtr> .ByteArrayToObject(byteArray); } else { Allocated = m_Process.Memory.Allocate(MarshalType <T> .Size); Allocated.Write(0, Value); Reference = Allocated.BaseAddress; } } }
/// <summary> /// Marshals the value into the remote process. /// </summary> private void Marshal() { if (typeof(IMarshallableValue).IsAssignableFrom(typeof(T))) { IMarshallableValue marshalVal = Value as IMarshallableValue; // Allocate memory in the remote process Allocated = _process.MemoryFactory.Allocate(Randomizer.GenerateString(), marshalVal.GetSize()); // Write the value(s) marshalVal.Write(Allocated); // Get the pointer Reference = marshalVal.GetReference(Allocated.BaseAddress); } // If the type is string, it's a special case else if (typeof(T) == typeof(string)) { var text = Value.ToString(); // Allocate memory in the remote process (string + '\0') Allocated = _process.MemoryFactory.Allocate(Randomizer.GenerateString(), text.Length + 1); // Write the value Allocated.Write(0, text, Encoding.UTF8); // Get the pointer Reference = Allocated.BaseAddress; } else { // For all other types // Convert the value into a byte array var byteArray = MarshalType <T> .ObjectToByteArray(Value); // If the value can be stored directly in registers if (MarshalType <T> .CanBeStoredInRegisters) { // Convert the byte array into a pointer Reference = MarshalType <IntPtr> .ByteArrayToObject(byteArray); } else { // It's a bit more complicated, we must allocate some space into // the remote process to store the value and get its pointer // Allocate memory in the remote process Allocated = _process.MemoryFactory.Allocate(Randomizer.GenerateString(), MarshalType <T> .Size); // Write the value Allocated.Write(0, Value); // Get the pointer Reference = Allocated.BaseAddress; } } }
/// <summary> /// Marshals the value into the remote process. /// </summary> private void Marshal() { // If the type is string, it's a special case if (typeof(T) == typeof(string)) { var text = Value.ToString(); // Allocate memory in the remote process (string + '\0') Allocated = MemorySharp.Memory.Allocate(text.Length + 1); // Write the value Allocated.WriteString(0, text); // Get the pointer Reference = Allocated.BaseAddress; } else { // For all other types // Convert the value into a byte array var byteArray = MarshalType <T> .ObjectToByteArray(Value); // If the value can be stored directly in registers if (MarshalType <T> .CanBeStoredInRegisters) { // Convert the byte array into a pointer Reference = MarshalType <IntPtr> .ByteArrayToObject(byteArray); } else { // It's a bit more complicated, we must allocate some space into // the remote process to store the value and get its pointer // Allocate memory in the remote process Allocated = MemorySharp.Memory.Allocate(MarshalType <T> .Size); // Write the value Allocated.Write(0, Value); // Get the pointer Reference = Allocated.BaseAddress; } } }