Extracted used parts of nativemethods.

This commit is contained in:
Eugene Wang 2023-04-02 20:05:18 -04:00
parent e67c6af195
commit 4e336ff110
6 changed files with 3929 additions and 3885 deletions

View File

@ -5,10 +5,11 @@
<Description>Library containing the TWAIN API for dotnet.</Description>
<TargetFrameworks>net6.0;net6.0-windows;net462;</TargetFrameworks>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<!--<AllowUnsafeBlocks>true</AllowUnsafeBlocks>-->
</PropertyGroup>
<PropertyGroup Condition=" '$(TargetFramework)' == 'net462' OR '$(TargetFramework)' == 'net6.0-windows'">
<UseWindowsForms>true</UseWindowsForms>
<UseWPF>true</UseWPF>
</PropertyGroup>
@ -27,8 +28,4 @@
<LastGenOutput>DSMGenerator.dummy</LastGenOutput>
</None>
</ItemGroup>
<ItemGroup>
<Service Include="{508349b6-6b84-4df5-91f0-309beebad82d}" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,41 @@
using System;
using System.Runtime.InteropServices;
namespace NTwain.Native
{
static class NativeMemoryMethods
{
[DllImport("kernel32", SetLastError = true, EntryPoint = "GlobalAlloc")]
public static extern IntPtr WinGlobalAlloc(AllocFlag uFlags, UIntPtr dwBytes);
[DllImport("kernel32", SetLastError = true, EntryPoint = "GlobalFree")]
public static extern IntPtr WinGlobalFree(IntPtr hMem);
[DllImport("kernel32", SetLastError = true, EntryPoint = "GlobalLock")]
public static extern IntPtr WinGlobalLock(IntPtr handle);
[DllImport("kernel32", SetLastError = true, EntryPoint = "GlobalUnlock")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool WinGlobalUnlock(IntPtr handle);
[Flags]
public enum AllocFlag : uint
{
/// <summary>
/// Allocates fixed memory. The return value is a pointer.
/// </summary>
GMEM_FIXED = 0,
/// <summary>
/// Allocates movable memory. Memory blocks are never moved in physical memory, but they can be moved within the default heap.
/// The return value is a handle to the memory object. To translate the handle into a pointer, use the GlobalLock function.
/// </summary>
GMEM_MOVEABLE = 2,
/// <summary>
/// Initializes memory contents to zero.
/// </summary>
GMEM_ZEROINIT = 0x40,
GPTR = GMEM_FIXED | GMEM_ZEROINIT,
GHND = GMEM_MOVEABLE | GMEM_ZEROINIT
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -3,7 +3,6 @@ using NTwain.Triplets;
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using TWAINWorkingGroup;
namespace NTwain
{
@ -11,10 +10,26 @@ namespace NTwain
partial class TwainSession
{
delegate ushort LegacyIDCallbackDelegate(
ref TW_IDENTITY_LEGACY origin, ref TW_IDENTITY_LEGACY dest,
DG dg, DAT dat, MSG msg, IntPtr twnull
);
delegate ushort BotchedLinuxCallbackDelegate
(
ref TW_IDENTITY origin, ref TW_IDENTITY dest,
DG dg, DAT dat, MSG msg, IntPtr twnull
);
delegate ushort OSXCallbackDelegate
(
ref TW_IDENTITY_MACOSX origin, ref TW_IDENTITY_MACOSX dest,
DG dg, DAT dat, MSG msg, IntPtr twnull
);
// these are kept around while a callback ptr is registered so they
// don't get gc'd
readonly NativeMethods.WindowsDsmEntryCallbackDelegate _legacyCallbackDelegate;
readonly NativeMethods.MacosxDsmEntryCallbackDelegate _osxCallbackDelegate;
readonly LegacyIDCallbackDelegate _legacyCallbackDelegate;
readonly OSXCallbackDelegate _osxCallbackDelegate;
/// <summary>
/// Try to registers callbacks for after opening the source.
@ -50,12 +65,8 @@ namespace NTwain
private ushort LegacyCallbackHandler
(
ref TW_IDENTITY_LEGACY origin,
ref TW_IDENTITY_LEGACY dest,
DG dg,
DAT dat,
MSG msg,
IntPtr twnull
ref TW_IDENTITY_LEGACY origin, ref TW_IDENTITY_LEGACY dest,
DG dg, DAT dat, MSG msg, IntPtr twnull
)
{
Debug.WriteLine($"Legacy callback got {msg}");
@ -65,12 +76,8 @@ namespace NTwain
private ushort OSXCallbackHandler
(
ref TW_IDENTITY_MACOSX origin,
ref TW_IDENTITY_MACOSX dest,
DG dg,
DAT dat,
MSG msg,
IntPtr twnull
ref TW_IDENTITY_MACOSX origin, ref TW_IDENTITY_MACOSX dest,
DG dg, DAT dat, MSG msg, IntPtr twnull
)
{
Debug.WriteLine($"OSX callback got {msg}");

View File

@ -1,7 +1,7 @@
using NTwain.Data;
using NTwain.Native;
using System;
using System.Runtime.InteropServices;
using TWAINWorkingGroup;
namespace NTwain
{
@ -19,7 +19,7 @@ namespace NTwain
}
else if (TwainPlatform.IsWindows)
{
return NativeMethods.GlobalAlloc(0x0042 /* GHND */, (UIntPtr)size);
return NativeMemoryMethods.WinGlobalAlloc(NativeMemoryMethods.AllocFlag.GHND, (UIntPtr)size);
}
else if (TwainPlatform.IsLinux)
{
@ -45,7 +45,7 @@ namespace NTwain
}
else if (TwainPlatform.IsWindows)
{
NativeMethods.GlobalFree(handle);
NativeMemoryMethods.WinGlobalFree(handle);
}
else if (TwainPlatform.IsLinux)
{
@ -71,7 +71,7 @@ namespace NTwain
}
else if (TwainPlatform.IsWindows)
{
return NativeMethods.GlobalLock(handle);
return NativeMemoryMethods.WinGlobalLock(handle);
}
else if (TwainPlatform.IsLinux)
{
@ -97,7 +97,7 @@ namespace NTwain
}
else if (TwainPlatform.IsWindows)
{
NativeMethods.GlobalUnlock(handle);
NativeMemoryMethods.WinGlobalUnlock(handle);
}
else if (TwainPlatform.IsLinux)
{

View File

@ -3,7 +3,6 @@ using NTwain.Triplets;
using System;
using System.Diagnostics;
using System.Text;
using TWAINWorkingGroup;
namespace NTwain
{