ntwain/NTwain/MemoryManager.cs

110 lines
3.4 KiB
C#
Raw Normal View History

2014-04-03 07:13:15 +08:00
using NTwain.Data;
2014-04-03 07:01:21 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
namespace NTwain
{
/// <summary>
/// Provides methods for managing memory on data exchanged with twain sources.
2014-04-06 04:48:28 +08:00
/// This should only be used after the DSM has been opened.
2014-04-03 07:01:21 +08:00
/// </summary>
2014-04-06 04:48:28 +08:00
public class MemoryManager : IMemoryManager
2014-04-03 07:01:21 +08:00
{
/// <summary>
2014-04-06 04:48:28 +08:00
/// Gets the singleton <see cref="MemoryManager"/> instance.
2014-04-03 07:01:21 +08:00
/// </summary>
2014-04-06 04:48:28 +08:00
public static readonly MemoryManager Instance = new MemoryManager();
2014-04-03 07:01:21 +08:00
private MemoryManager() { }
/// <summary>
/// Updates the entry point used by TWAIN.
/// </summary>
/// <param name="entryPoint">The entry point.</param>
internal void UpdateEntryPoint(TWEntryPoint entryPoint)
{
_twain2Entry = entryPoint;
}
TWEntryPoint _twain2Entry;
2014-04-05 10:19:16 +08:00
/// <summary>
2014-04-06 06:33:21 +08:00
/// Function to allocate memory. Calls to this must be coupled with <see cref="Free"/> later.
2014-04-05 10:19:16 +08:00
/// </summary>
/// <param name="size">The size in bytes.</param>
/// <returns>Handle to the allocated memory.</returns>
2014-04-06 06:33:21 +08:00
public IntPtr Allocate(uint size)
2014-04-03 07:01:21 +08:00
{
2014-04-06 20:22:59 +08:00
IntPtr retVal = IntPtr.Zero;
2014-04-03 07:01:21 +08:00
if (_twain2Entry != null && _twain2Entry.AllocateFunction != null)
{
2014-04-06 20:22:59 +08:00
retVal = _twain2Entry.AllocateFunction(size);
2014-04-03 07:01:21 +08:00
}
else
{
// 0x0040 is GPTR
2014-04-06 20:22:59 +08:00
retVal = NativeMethods.WinGlobalAlloc(0x0040, new UIntPtr(size));
}
if (retVal == IntPtr.Zero)
{
throw new OutOfMemoryException("Failed to allocate requested memory.");
2014-04-03 07:01:21 +08:00
}
2014-04-06 20:22:59 +08:00
return retVal;
2014-04-03 07:01:21 +08:00
}
2014-04-05 10:19:16 +08:00
/// <summary>
/// Function to free memory.
/// </summary>
2014-04-06 06:33:21 +08:00
/// <param name="handle">The handle from <see cref="Allocate"/>.</param>
public void Free(IntPtr handle)
2014-04-03 07:01:21 +08:00
{
if (_twain2Entry != null && _twain2Entry.FreeFunction != null)
{
_twain2Entry.FreeFunction(handle);
}
else
{
2014-04-06 20:22:59 +08:00
NativeMethods.WinGlobalFree(handle);
2014-04-03 07:01:21 +08:00
}
}
2014-04-05 10:19:16 +08:00
/// <summary>
2014-04-06 06:33:21 +08:00
/// Function to lock some memory. Calls to this must be coupled with <see cref="Unlock"/> later.
2014-04-05 10:19:16 +08:00
/// </summary>
/// <param name="handle">The handle to allocated memory.</param>
/// <returns>Handle to the lock.</returns>
2014-04-06 06:33:21 +08:00
public IntPtr Lock(IntPtr handle)
2014-04-03 07:01:21 +08:00
{
if (_twain2Entry != null && _twain2Entry.LockFunction != null)
{
return _twain2Entry.LockFunction(handle);
}
else
{
2014-04-06 20:22:59 +08:00
return NativeMethods.WinGlobalLock(handle);
2014-04-03 07:01:21 +08:00
}
}
2014-04-05 10:19:16 +08:00
/// <summary>
/// Function to unlock a previously locked memory region.
/// </summary>
2014-04-06 06:33:21 +08:00
/// <param name="handle">The handle from <see cref="Lock"/>.</param>
public void Unlock(IntPtr handle)
2014-04-03 07:01:21 +08:00
{
if (_twain2Entry != null && _twain2Entry.UnlockFunction != null)
{
_twain2Entry.UnlockFunction(handle);
}
else
{
2014-04-06 20:22:59 +08:00
NativeMethods.WinGlobalUnlock(handle);
2014-04-03 07:01:21 +08:00
}
}
}
}