mirror of
https://github.com/soukoku/ntwain.git
synced 2025-04-05 20:59:23 +08:00
Tried to load and use entry point for memory mgmt.
This commit is contained in:
parent
e68525775d
commit
5aa808024f
@ -15,16 +15,10 @@ namespace WinForm32
|
||||
{
|
||||
InitializeComponent();
|
||||
|
||||
InitTwain();
|
||||
}
|
||||
|
||||
private void InitTwain()
|
||||
{
|
||||
TwainPlatform.PreferLegacyDSM = true;
|
||||
|
||||
twain = new TwainSession(Assembly.GetExecutingAssembly().Location);
|
||||
twain.StateChanged += Twain_StateChanged;
|
||||
|
||||
}
|
||||
|
||||
private static void Twain_StateChanged(TwainSession session, TWAINWorkingGroup.STATE state)
|
||||
|
@ -529,6 +529,24 @@ namespace TWAINWorkingGroup
|
||||
{
|
||||
return $"{Manufacturer} - {ProductFamily} - {ProductName} {Version} (TWAIN {ProtocolMajor}.{ProtocolMinor})";
|
||||
}
|
||||
public static implicit operator TW_IDENTITY_LEGACY(TW_IDENTITY_MACOSX value) => new()
|
||||
{
|
||||
Id = value.Id,
|
||||
Manufacturer = value.Manufacturer,
|
||||
ProductFamily = value.ProductFamily,
|
||||
ProductName = value.ProductName,
|
||||
ProtocolMajor = value.ProtocolMajor,
|
||||
ProtocolMinor = value.ProtocolMinor,
|
||||
SupportedGroups = value.SupportedGroups,
|
||||
Version = new()
|
||||
{
|
||||
Country = value.Version.Country,
|
||||
Info = value.Version.Info,
|
||||
Language = value.Version.Language,
|
||||
MajorNum = value.Version.MajorNum,
|
||||
MinorNum = value.Version.MinorNum,
|
||||
}
|
||||
};
|
||||
}
|
||||
partial struct TW_IDENTITY_LEGACY
|
||||
{
|
||||
|
85
src/NTwain/Triplets/DATEntryPoint.cs
Normal file
85
src/NTwain/Triplets/DATEntryPoint.cs
Normal file
@ -0,0 +1,85 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using TWAINWorkingGroup;
|
||||
|
||||
namespace NTwain.Triplets
|
||||
{
|
||||
/// <summary>
|
||||
/// Contains calls used with <see cref="DG.CONTROL"/> and <see cref="DAT.ENTRYPOINT"/>.
|
||||
/// </summary>
|
||||
public class DATEntryPoint : TripletBase
|
||||
{
|
||||
public DATEntryPoint(TwainSession session) : base(session)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads and opens the DSM.
|
||||
/// </summary>
|
||||
/// <param name="entry"></param>
|
||||
/// <returns></returns>
|
||||
public STS Get(ref TW_ENTRYPOINT_DELEGATES entry)
|
||||
{
|
||||
TW_ENTRYPOINT rawentry = default;
|
||||
var rc = DoIt(MSG.GET, ref rawentry);
|
||||
if (rc == STS.SUCCESS)
|
||||
{
|
||||
entry.Size = rawentry.Size;
|
||||
entry.DSM_Entry = rawentry.DSM_Entry;
|
||||
if (rawentry.DSM_MemAllocate != IntPtr.Zero)
|
||||
{
|
||||
entry.DSM_MemAllocate = (DSM_MEMALLOC)Marshal.GetDelegateForFunctionPointer(rawentry.DSM_MemAllocate, typeof(DSM_MEMALLOC));
|
||||
}
|
||||
if (rawentry.DSM_MemFree != IntPtr.Zero)
|
||||
{
|
||||
entry.DSM_MemFree = (DSM_MEMFREE)Marshal.GetDelegateForFunctionPointer(rawentry.DSM_MemFree, typeof(DSM_MEMFREE));
|
||||
}
|
||||
if (rawentry.DSM_MemLock != IntPtr.Zero)
|
||||
{
|
||||
entry.DSM_MemLock = (DSM_MEMLOCK)Marshal.GetDelegateForFunctionPointer(rawentry.DSM_MemLock, typeof(DSM_MEMLOCK));
|
||||
}
|
||||
if (rawentry.DSM_MemUnlock != IntPtr.Zero)
|
||||
{
|
||||
entry.DSM_MemUnlock = (DSM_MEMUNLOCK)Marshal.GetDelegateForFunctionPointer(rawentry.DSM_MemUnlock, typeof(DSM_MEMUNLOCK));
|
||||
}
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
STS DoIt(MSG msg, ref TW_ENTRYPOINT entry)
|
||||
{
|
||||
var rc = STS.FAILURE;
|
||||
if (TwainPlatform.IsWindows)
|
||||
{
|
||||
var app = Session._appIdentityLegacy;
|
||||
TW_IDENTITY_LEGACY dummy = default;
|
||||
if (TwainPlatform.Is32bit && TwainPlatform.PreferLegacyDSM)
|
||||
{
|
||||
rc = (STS)NativeMethods.WindowsTwain32DsmEntryEntrypoint(ref app, ref dummy, DG.CONTROL, DAT.PARENT, msg, ref entry);
|
||||
}
|
||||
else
|
||||
{
|
||||
rc = (STS)NativeMethods.WindowsTwaindsmDsmEntryEntrypoint(ref app, ref dummy, DG.CONTROL, DAT.PARENT, msg, ref entry);
|
||||
}
|
||||
}
|
||||
//else if (TwainPlatform.IsLinux)
|
||||
//{
|
||||
// var app = Session._appIdentityLegacy;
|
||||
//}
|
||||
else if (TwainPlatform.IsMacOSX)
|
||||
{
|
||||
var app = Session._appIdentityOSX;
|
||||
TW_IDENTITY_MACOSX dummy = default;
|
||||
if (TwainPlatform.PreferLegacyDSM)
|
||||
{
|
||||
rc = (STS)NativeMethods.MacosxTwainDsmEntryEntrypoint(ref app, ref dummy, DG.CONTROL, DAT.PARENT, msg, ref entry);
|
||||
}
|
||||
else
|
||||
{
|
||||
rc = (STS)NativeMethods.MacosxTwaindsmDsmEntryEntrypoint(ref app, ref dummy, DG.CONTROL, DAT.PARENT, msg, ref entry);
|
||||
}
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
}
|
||||
}
|
@ -23,6 +23,15 @@ namespace NTwain.Triplets
|
||||
if ((rc = DoIt(MSG.OPENDSM, ref hwnd)) == STS.SUCCESS)
|
||||
{
|
||||
Session.State = STATE.S3;
|
||||
// determine memory mgmt routines used
|
||||
if ((((DG)Session._appIdentityLegacy.SupportedGroups) & DG.DSM2) == DG.DSM2)
|
||||
{
|
||||
TW_ENTRYPOINT_DELEGATES entry = default;
|
||||
if (Session.DGControl.EntryPoint.Get(ref entry) == STS.SUCCESS)
|
||||
{
|
||||
Session._entryPoint = entry;
|
||||
}
|
||||
}
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
@ -38,6 +47,7 @@ namespace NTwain.Triplets
|
||||
if ((rc = DoIt(MSG.CLOSEDSM, ref hwnd)) == STS.SUCCESS)
|
||||
{
|
||||
Session.State = STATE.S2;
|
||||
Session._entryPoint = default;
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
@ -57,15 +67,14 @@ namespace NTwain.Triplets
|
||||
{
|
||||
rc = (STS)NativeMethods.WindowsTwaindsmDsmEntryParent(ref app, IntPtr.Zero, DG.CONTROL, DAT.PARENT, msg, ref hwnd);
|
||||
}
|
||||
if (rc == STS.SUCCESS) Session._appIdentity = app;
|
||||
}
|
||||
else if (TwainPlatform.IsLinux)
|
||||
{
|
||||
var app = Session._appIdentityLegacy;
|
||||
|
||||
rc = (STS)NativeMethods.LinuxDsmEntryParent(ref app, IntPtr.Zero, DG.CONTROL, DAT.PARENT, msg, ref hwnd);
|
||||
if (rc == STS.SUCCESS) Session._appIdentity = app;
|
||||
if (rc == STS.SUCCESS) Session._appIdentityLegacy = app;
|
||||
}
|
||||
//else if (TwainPlatform.IsLinux)
|
||||
//{
|
||||
// var app = Session._appIdentityLegacy;
|
||||
// rc = (STS)NativeMethods.LinuxDsmEntryParent(ref app, IntPtr.Zero, DG.CONTROL, DAT.PARENT, msg, ref hwnd);
|
||||
// if (rc == STS.SUCCESS) Session._appIdentityLegacy = app;
|
||||
//}
|
||||
else if (TwainPlatform.IsMacOSX)
|
||||
{
|
||||
var app = Session._appIdentityOSX;
|
||||
@ -77,7 +86,11 @@ namespace NTwain.Triplets
|
||||
{
|
||||
rc = (STS)NativeMethods.MacosxTwaindsmDsmEntryParent(ref app, IntPtr.Zero, DG.CONTROL, DAT.PARENT, msg, ref hwnd);
|
||||
}
|
||||
if (rc == STS.SUCCESS) Session._appIdentityOSX = app;
|
||||
if (rc == STS.SUCCESS)
|
||||
{
|
||||
Session._appIdentityOSX = app;
|
||||
Session._appIdentityLegacy = app;
|
||||
}
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
@ -12,5 +12,8 @@
|
||||
private DATParent? _parent;
|
||||
public DATParent Parent => _parent ??= new DATParent(Session);
|
||||
|
||||
private DATEntryPoint? _entryPoint;
|
||||
public DATEntryPoint EntryPoint => _entryPoint ??= new DATEntryPoint(Session);
|
||||
|
||||
}
|
||||
}
|
118
src/NTwain/TwainSession.Memory.cs
Normal file
118
src/NTwain/TwainSession.Memory.cs
Normal file
@ -0,0 +1,118 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using TWAINWorkingGroup;
|
||||
|
||||
namespace NTwain
|
||||
{
|
||||
// this file contains memory methods
|
||||
|
||||
partial class TwainSession
|
||||
{
|
||||
internal TW_ENTRYPOINT_DELEGATES _entryPoint;
|
||||
|
||||
public IntPtr Alloc(uint size)
|
||||
{
|
||||
if (_entryPoint.DSM_MemAllocate != null)
|
||||
{
|
||||
return _entryPoint.DSM_MemAllocate(size);
|
||||
}
|
||||
else if (TwainPlatform.IsWindows)
|
||||
{
|
||||
return NativeMethods.GlobalAlloc(0x0042 /* GHND */, (UIntPtr)size);
|
||||
}
|
||||
else if (TwainPlatform.IsLinux)
|
||||
{
|
||||
return Marshal.AllocHGlobal((int)size);
|
||||
}
|
||||
else if (TwainPlatform.IsMacOSX)
|
||||
{
|
||||
return Marshal.AllocHGlobal((int)size);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new PlatformNotSupportedException();
|
||||
}
|
||||
}
|
||||
|
||||
public void Free(IntPtr handle)
|
||||
{
|
||||
if (handle == IntPtr.Zero) return;
|
||||
|
||||
if (_entryPoint.DSM_MemFree != null)
|
||||
{
|
||||
_entryPoint.DSM_MemFree(handle);
|
||||
}
|
||||
else if (TwainPlatform.IsWindows)
|
||||
{
|
||||
NativeMethods.GlobalFree(handle);
|
||||
}
|
||||
else if (TwainPlatform.IsLinux)
|
||||
{
|
||||
Marshal.FreeHGlobal(handle);
|
||||
}
|
||||
else if (TwainPlatform.IsMacOSX)
|
||||
{
|
||||
Marshal.FreeHGlobal(handle);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new PlatformNotSupportedException();
|
||||
}
|
||||
}
|
||||
|
||||
public IntPtr Lock(IntPtr handle)
|
||||
{
|
||||
if (handle == IntPtr.Zero) return IntPtr.Zero;
|
||||
|
||||
if (_entryPoint.DSM_MemLock != null)
|
||||
{
|
||||
return _entryPoint.DSM_MemLock(handle);
|
||||
}
|
||||
else if (TwainPlatform.IsWindows)
|
||||
{
|
||||
return NativeMethods.GlobalLock(handle);
|
||||
}
|
||||
else if (TwainPlatform.IsLinux)
|
||||
{
|
||||
return handle;
|
||||
}
|
||||
else if (TwainPlatform.IsMacOSX)
|
||||
{
|
||||
return handle;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new PlatformNotSupportedException();
|
||||
}
|
||||
}
|
||||
|
||||
public void Unlock(IntPtr handle)
|
||||
{
|
||||
if (handle == IntPtr.Zero) return;
|
||||
|
||||
if (_entryPoint.DSM_MemUnlock != null)
|
||||
{
|
||||
_entryPoint.DSM_MemUnlock(handle);
|
||||
}
|
||||
else if (TwainPlatform.IsWindows)
|
||||
{
|
||||
NativeMethods.GlobalUnlock(handle);
|
||||
}
|
||||
else if (TwainPlatform.IsLinux)
|
||||
{
|
||||
}
|
||||
else if (TwainPlatform.IsMacOSX)
|
||||
{
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new PlatformNotSupportedException();
|
||||
}
|
||||
}
|
||||
|
||||
public delegate IntPtr DSM_MEMALLOC(uint size);
|
||||
public delegate void DSM_MEMFREE(IntPtr handle);
|
||||
public delegate IntPtr DSM_MEMLOCK(IntPtr handle);
|
||||
public delegate void DSM_MEMUNLOCK(IntPtr handle);
|
||||
}
|
||||
}
|
@ -23,7 +23,7 @@ namespace NTwain
|
||||
/// </summary>
|
||||
public TW_IDENTITY_LEGACY AppIdentity => _appIdentityLegacy;
|
||||
internal TW_IDENTITY_LEGACY _appIdentityLegacy;
|
||||
internal TW_IDENTITY _appIdentity;
|
||||
//internal TW_IDENTITY _appIdentity;
|
||||
internal TW_IDENTITY_MACOSX _appIdentityOSX;
|
||||
|
||||
/// <summary>
|
||||
@ -31,10 +31,9 @@ namespace NTwain
|
||||
/// </summary>
|
||||
public TW_IDENTITY_LEGACY DSIdentity => _dsIdentityLegacy;
|
||||
internal TW_IDENTITY_LEGACY _dsIdentityLegacy;
|
||||
internal TW_IDENTITY _dsIdentity;
|
||||
//internal TW_IDENTITY _dsIdentity;
|
||||
internal TW_IDENTITY_MACOSX _dsIdentityOSX;
|
||||
|
||||
|
||||
private STATE _state = STATE.S1;
|
||||
|
||||
/// <summary>
|
||||
|
@ -31,8 +31,11 @@ namespace NTwain
|
||||
/// <param name="appCountry"></param>
|
||||
public TwainSession(FileVersionInfo appInfo,
|
||||
TWLG appLanguage = TWLG.ENGLISH_USA, TWCY appCountry = TWCY.USA) :
|
||||
this(appInfo.CompanyName, appInfo.ProductName, appInfo.ProductName, new Version(appInfo.FileVersion),
|
||||
appInfo.FileDescription, appLanguage, appCountry)
|
||||
this(appInfo.CompanyName ?? "",
|
||||
appInfo.ProductName ?? "",
|
||||
appInfo.ProductName ?? "",
|
||||
new Version(appInfo.FileVersion ?? "1.0"),
|
||||
appInfo.FileDescription ?? "", appLanguage, appCountry)
|
||||
{ }
|
||||
/// <summary>
|
||||
/// Creates TWAIN session with explicit app info.
|
||||
@ -73,8 +76,8 @@ namespace NTwain
|
||||
MinorNum = (ushort)productVersion.Minor,
|
||||
}
|
||||
};
|
||||
if (TwainPlatform.IsLinux) _appIdentity = _appIdentityLegacy;
|
||||
else if (TwainPlatform.IsMacOSX) _appIdentityOSX = _appIdentityLegacy;
|
||||
//if (TwainPlatform.IsLinux) _appIdentity = _appIdentityLegacy;
|
||||
if (TwainPlatform.IsMacOSX) _appIdentityOSX = _appIdentityLegacy;
|
||||
|
||||
DGControl = new DGControl(this);
|
||||
DGImage = new DGImage(this);
|
||||
|
Loading…
Reference in New Issue
Block a user