/// <summary> /// Creates a <see cref="PrivateMemoryBuffer"/> that satisfies a set size constraint and proximity to a set address. /// </summary> /// <param name="size">The minimum size the <see cref="PrivateMemoryBuffer"/> will have to accomodate.</param> /// <param name="minimumAddress">The minimum absolute address to create a buffer in.</param> /// <param name="maximumAddress">The maximum absolute address to create a buffer in.</param> /// <param name="retryCount">In the case the memory allocation fails; the amount of times memory allocation is to be retried.</param> /// <exception cref="System.Exception">Memory allocation failure due to possible race condition with other process/process itself/Windows scheduling.</exception> public PrivateMemoryBuffer CreatePrivateMemoryBuffer(int size, long minimumAddress = 0x10000, long maximumAddress = 0x7FFFFFFF, int retryCount = 3) { if (minimumAddress <= 0) { throw new ArgumentException("Please do not set the minimum address to 0 or negative. It collides with the return values of Windows API functions" + "where e.g. 0 is returned on failure but you can also allocate successfully on 0."); } // Keep retrying memory allocation. _allocateMemoryMutex.WaitOne(); try { return(Run(retryCount, () => { var memoryLocation = FindBufferLocation(size, minimumAddress, maximumAddress, true); var buffer = MemoryBufferFactory.CreatePrivateBuffer(Process, memoryLocation.MemoryAddress, memoryLocation.Size); _allocateMemoryMutex.ReleaseMutex(); return buffer; })); } catch (Exception) { _allocateMemoryMutex.ReleaseMutex(); throw; } }
/// <summary> /// Creates a <see cref="PrivateMemoryBuffer"/> that satisfies a set size constraint and proximity to a set address. /// </summary> /// <param name="size">The minimum size the <see cref="PrivateMemoryBuffer"/> will have to accomodate.</param> /// <param name="minimumAddress">The minimum absolute address to create a buffer in.</param> /// <param name="maximumAddress">The maximum absolute address to create a buffer in.</param> /// <param name="retryCount">In the case the memory allocation fails; the amount of times memory allocation is to be retried.</param> /// <exception cref="System.Exception">Memory allocation failure due to possible race condition with other process/process itself/Windows scheduling.</exception> public PrivateMemoryBuffer CreatePrivateMemoryBuffer(int size, long minimumAddress = 0x10000, long maximumAddress = 0x7FFFFFFF, int retryCount = 3) { if (minimumAddress <= 0) { throw new ArgumentException("Please do not set the minimum address to 0 or negative. It collides with the return values of Windows API functions" + "where e.g. 0 is returned on failure but you can also allocate successfully on 0."); } // Keep retrying memory allocation. lock (_threadLock) { Exception caughtException = new Exception("Temp Exception in CreatePrivateMemoryBuffer(). This should not throw."); for (int retries = 0; retries < retryCount; retries++) { try { var memoryLocation = FindBufferLocation(size, minimumAddress, maximumAddress, true); var buffer = MemoryBufferFactory.CreatePrivateBuffer(Process, memoryLocation.MemoryAddress, memoryLocation.Size); return(buffer); } catch (Exception ex) { caughtException = ex; } } throw caughtException; } }