ntwain/NTwain/MemoryManager.cs

120 lines
3.8 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>
/// Function to allocate memory. Calls to this must be coupled with <see cref="MemFree"/> later.
/// </summary>
/// <param name="size">The size in bytes.</param>
/// <returns>Handle to the allocated memory.</returns>
2014-04-03 07:01:21 +08:00
public IntPtr MemAllocate(uint size)
{
if (_twain2Entry != null && _twain2Entry.AllocateFunction != null)
{
return _twain2Entry.AllocateFunction(size);
}
else
{
// 0x0040 is GPTR
2014-04-06 04:48:28 +08:00
return WinGlobalAlloc(0x0040, new UIntPtr(size));
2014-04-03 07:01:21 +08:00
}
}
2014-04-05 10:19:16 +08:00
/// <summary>
/// Function to free memory.
/// </summary>
/// <param name="handle">The handle from <see cref="MemAllocate"/>.</param>
2014-04-03 07:01:21 +08:00
public void MemFree(IntPtr handle)
{
if (_twain2Entry != null && _twain2Entry.FreeFunction != null)
{
_twain2Entry.FreeFunction(handle);
}
else
{
2014-04-06 04:48:28 +08:00
WinGlobalFree(handle);
2014-04-03 07:01:21 +08:00
}
}
2014-04-05 10:19:16 +08:00
/// <summary>
/// Function to lock some memory. Calls to this must be coupled with <see cref="MemUnlock"/> later.
/// </summary>
/// <param name="handle">The handle to allocated memory.</param>
/// <returns>Handle to the lock.</returns>
2014-04-03 07:01:21 +08:00
public IntPtr MemLock(IntPtr handle)
{
if (_twain2Entry != null && _twain2Entry.LockFunction != null)
{
return _twain2Entry.LockFunction(handle);
}
else
{
2014-04-06 04:48:28 +08:00
return 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>
/// <param name="handle">The handle from <see cref="MemLock"/>.</param>
2014-04-03 07:01:21 +08:00
public void MemUnlock(IntPtr handle)
{
if (_twain2Entry != null && _twain2Entry.UnlockFunction != null)
{
_twain2Entry.UnlockFunction(handle);
}
else
{
2014-04-06 04:48:28 +08:00
WinGlobalUnlock(handle);
2014-04-03 07:01:21 +08:00
}
}
#region old mem stuff for twain 1.x
2014-04-06 04:48:28 +08:00
[DllImport("kernel32", SetLastError = true, EntryPoint = "GlobalAlloc")]
static extern IntPtr WinGlobalAlloc(uint uFlags, UIntPtr dwBytes);
2014-04-03 07:01:21 +08:00
2014-04-06 04:48:28 +08:00
[DllImport("kernel32", SetLastError = true, EntryPoint = "GlobalFree")]
static extern IntPtr WinGlobalFree(IntPtr hMem);
2014-04-03 07:01:21 +08:00
2014-04-06 04:48:28 +08:00
[DllImport("kernel32", SetLastError = true, EntryPoint = "GlobalLock")]
static extern IntPtr WinGlobalLock(IntPtr handle);
[DllImport("kernel32", SetLastError = true, EntryPoint = "GlobalUnlock")]
2014-04-03 07:01:21 +08:00
[return: MarshalAs(UnmanagedType.Bool)]
2014-04-06 04:48:28 +08:00
static extern bool WinGlobalUnlock(IntPtr handle);
2014-04-03 07:01:21 +08:00
#endregion
}
}