Example #1
0
        /// <summary>
        /// Creates a new private <see cref="MemoryBuffer"/> in a specified location in memory with a specified size.
        /// </summary>
        internal static PrivateMemoryBuffer CreatePrivateBuffer(Process process, nuint bufferAddress, int allocationSize, bool allocateMemory = true)
        {
            if (allocateMemory)
            {
                AllocateBuffer(process, bufferAddress, allocationSize);
            }

            var memorySource = GetMemorySource(process);

            // Setup buffer after Magic.
            var dataPtr                = (UIntPtr)bufferAddress + PrivateBufferOverhead;
            var realBufSize            = allocationSize - PrivateBufferOverhead;
            var memoryBufferProperties = new MemoryBufferProperties(dataPtr, realBufSize);

            var buffer = new PrivateMemoryBuffer(memorySource, bufferAddress, memoryBufferProperties);

            buffer.SetupMutex(process);
            return(buffer);
        }
Example #2
0
        /// <summary>
        /// Creates a new buffer in a specified location in memory with a specified size.
        /// </summary>
        /// <param name="process">The process inside which the <see cref="MemoryBuffer"/> will be allocated.</param>
        /// <param name="bufferAddress">Base address of the new buffer to be created. </param>
        /// <param name="allocationSize">The amount of bytes allocated at bufferAddress</param>
        /// <param name="allocateMemory">Set this to false if the allocationSize bytes have already been preallocated at bufferAddress.</param>
        /// <remarks>
        /// This constructor will override any existing buffer! Please use <see cref="MemoryBufferFactory.FromAddress"/> instead
        /// if you wish to get a hold of an already existing buffer at the given bufferAddress.
        /// </remarks>
        internal static MemoryBuffer CreateBuffer(Process process, nuint bufferAddress, int allocationSize, bool allocateMemory = true)
        {
            if (allocateMemory)
            {
                AllocateBuffer(process, bufferAddress, allocationSize);
            }

            var memorySource = GetMemorySource(process);

            // Write buffer "magic".
            memorySource.Write(bufferAddress, ref _bufferMagic);

            // Setup buffer after Magic.
            var headerAddress          = (UIntPtr)bufferAddress + sizeof(MemoryBufferMagic);
            var dataPtr                = (UIntPtr)bufferAddress + BufferOverhead;
            var realBufSize            = allocationSize - BufferOverhead;
            var memoryBufferProperties = new MemoryBufferProperties(dataPtr, realBufSize);

            var buffer = new MemoryBuffer(memorySource, headerAddress, memoryBufferProperties);

            buffer.SetupMutex(process);
            return(buffer);
        }