Example #1
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="AAllocatedMemory" /> class.
 /// </summary>
 /// <param name="processPlus">The reference of the <see cref="AProcessSharp" /> object.</param>
 /// <param name="name"></param>
 /// <param name="size">The size of the allocated memory.</param>
 /// <param name="protection">The protection of the allocated memory.</param>
 /// <param name="mustBeDisposed">The allocated memory will be released when the finalizer collects the object.</param>
 public AAllocatedMemory(AProcessSharp processPlus, string name, int size,
                         MemoryProtectionFlags protection = MemoryProtectionFlags.ExecuteReadWrite,
                         bool mustBeDisposed = true)
     : base(processPlus, AMemoryHelper.Allocate(processPlus.Handle, size, protection))
 {
     // Set local vars
     Identifier     = name;
     MustBeDisposed = mustBeDisposed;
     IsDisposed     = false;
     Size           = size;
 }
Example #2
0
        public void SetUserDefinedSymbolAllocSize(String name, UInt32 size, UIntPtr preferredAddress)
        {
            const String PREV_DEC = "The symbol named %s was previously declared with a size of %s instead of %s." +
                                    " all scripts that use this memory must give the same size. " +
                                    "Adjust the size, or delete the old alloc from the userdefined symbol list";

            if (size == 0)
            {
                throw new Exception("Please provide a bigger size");
            }
            UIntPtr p;
            int     i;

            for (i = 0; i < UserDefinedSymbols.Length; i++)
            {
                if (!UserDefinedSymbols[i].IsMatch(name))
                {
                    continue; //it exists, check first
                }
                if (UserDefinedSymbols[i].AllocSize > 0 && UserDefinedSymbols[i].ProcessId == Process.Native.Id)
                {
                    if (size != UserDefinedSymbols[i].AllocSize)
                    {
                        throw new Exception(UStringUtils.Sprintf(PREV_DEC, UserDefinedSymbols[i].Name, UserDefinedSymbols[i].AllocSize, size));
                    }
                }
                if (UserDefinedSymbols[i].ProcessId != Process.Native.Id)
                {
                    if (preferredAddress != UIntPtr.Zero)
                    {
                        p = AMemoryHelper.Allocate(Process.Handle, preferredAddress.ToIntPtr(), (int)size).ToUIntPtr();
                    }
                    else
                    {
                        p = AMemoryHelper.Allocate(Process.Handle, (int)size).ToUIntPtr();
                    }
                    if (p == UIntPtr.Zero)
                    {
                        throw new Exception("Error allocating memory");
                    }
                    UserDefinedSymbols[i].Address       = p;
                    UserDefinedSymbols[i].AddressString = AStringUtils.IntToHex(p, 8);
                    UserDefinedSymbols[i].AllocSize     = size;
                    UserDefinedSymbols[i].ProcessId     = Process.Native.Id;
                }
                return; // Redefined the symbol and exit;
            }
            //Still here, symbol Not exists, let's define a new one.
            if (preferredAddress != UIntPtr.Zero)
            {
                p = AMemoryHelper.Allocate(Process.Handle, preferredAddress.ToIntPtr(), (int)size).ToUIntPtr();
            }
            else
            {
                p = AMemoryHelper.Allocate(Process.Handle, (int)size).ToUIntPtr();
            }
            if (p == UIntPtr.Zero)
            {
                throw new Exception("Error allocating memory");
            }
            AddUserDefinedSymbol(AStringUtils.IntToHex(p, 8), name);
            UserDefinedSymbols[i].AllocSize = size;
            UserDefinedSymbols[i].ProcessId = Process.Native.Id;
        }