mirror of
https://github.com/soukoku/ntwain.git
synced 2026-01-26 21:48:36 +08:00
Added memory mgmt entry.
This commit is contained in:
@@ -8,26 +8,41 @@ namespace NetCoreConsole
|
|||||||
{
|
{
|
||||||
static void Main(string[] args)
|
static void Main(string[] args)
|
||||||
{
|
{
|
||||||
var config = new TwainConfigurationBuilder()
|
try
|
||||||
|
{
|
||||||
|
var config = new TwainConfigBuilder()
|
||||||
.DefineApp(Assembly.GetExecutingAssembly())
|
.DefineApp(Assembly.GetExecutingAssembly())
|
||||||
.Build();
|
.Build();
|
||||||
|
Console.WriteLine($"App = {(config.Is64Bit ? "64bit" : "32bit")}");
|
||||||
|
Console.WriteLine($"Platform = {config.Platform}");
|
||||||
|
|
||||||
using (var session = new TwainSession(config))
|
using (var session = new TwainSession(config))
|
||||||
{
|
{
|
||||||
session.PropertyChanged += Session_PropertyChanged;
|
session.PropertyChanged += Session_PropertyChanged;
|
||||||
|
|
||||||
var handle = IntPtr.Zero;
|
var handle = IntPtr.Zero;
|
||||||
session.Open(ref handle);
|
if (session.Open(ref handle) == NTwain.Data.ReturnCode.Success)
|
||||||
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Console.WriteLine("Test ended, press Enter to exit...");
|
||||||
|
Console.ReadLine();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine("ERROR: " + ex.ToString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static void Session_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
|
private static void Session_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
|
||||||
{
|
{
|
||||||
var session = (TwainSession)sender;
|
var session = (TwainSession)sender;
|
||||||
if (e.PropertyName == "State")
|
if (e.PropertyName == "State")
|
||||||
{
|
{
|
||||||
Console.WriteLine($"State changed to {session.State}");
|
Console.WriteLine($"Session state changed to {session.State}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -546,7 +546,7 @@ namespace NTwain.Data
|
|||||||
DataGroups dg, DataArgumentType dat, Message msg, TW_MEMREF data);
|
DataGroups dg, DataArgumentType dat, Message msg, TW_MEMREF data);
|
||||||
|
|
||||||
[StructLayout(LayoutKind.Sequential, Pack = 2)]
|
[StructLayout(LayoutKind.Sequential, Pack = 2)]
|
||||||
partial struct TW_ENTRYPOINT
|
partial class TW_ENTRYPOINT
|
||||||
{
|
{
|
||||||
// TODO: linux 64 is different?
|
// TODO: linux 64 is different?
|
||||||
TW_UINT32 _size;
|
TW_UINT32 _size;
|
||||||
|
|||||||
@@ -2364,19 +2364,18 @@ namespace NTwain.Data
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Provides entry points required by TWAIN 2.0 Applications and Sources.
|
/// Provides entry points required by TWAIN 2.0 Applications and Sources.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
partial struct TW_ENTRYPOINT : IMemoryManager
|
partial class TW_ENTRYPOINT : IMemoryManager
|
||||||
{
|
{
|
||||||
///// <summary>
|
/// <summary>
|
||||||
///// Initializes a new instance of the <see cref="TW_ENTRYPOINT"/> class.
|
/// Initializes a new instance of the <see cref="TW_ENTRYPOINT"/> class.
|
||||||
///// </summary>
|
/// </summary>
|
||||||
//public TW_ENTRYPOINT()
|
public TW_ENTRYPOINT()
|
||||||
//{
|
{
|
||||||
// _size = (uint)Marshal.SizeOf(this);
|
_size = (uint)Marshal.SizeOf(this);
|
||||||
//}
|
}
|
||||||
|
|
||||||
#region IMemoryManager Members
|
#region IMemoryManager Members
|
||||||
|
|
||||||
|
|||||||
@@ -3,12 +3,12 @@ using System.Collections.Generic;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace NTwain.Internals
|
namespace NTwain
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Interface that provides the correct methods for managing memory on data exchanged with TWAIN sources.
|
/// Interface that provides the correct methods for managing memory on data exchanged with TWAIN sources.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
interface IMemoryManager
|
public interface IMemoryManager
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Function to allocate memory. Calls to this must be coupled with <see cref="Free"/> later.
|
/// Function to allocate memory. Calls to this must be coupled with <see cref="Free"/> later.
|
||||||
@@ -1,10 +1,14 @@
|
|||||||
using System;
|
using NTwain.Data;
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace NTwain.Triplets
|
namespace NTwain.Triplets
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Represents <see cref="DataGroups.Audio"/>.
|
||||||
|
/// </summary>
|
||||||
public partial class DGAudio : BaseTriplet
|
public partial class DGAudio : BaseTriplet
|
||||||
{
|
{
|
||||||
internal DGAudio(TwainSession session) : base(session) { }
|
internal DGAudio(TwainSession session) : base(session) { }
|
||||||
|
|||||||
20
src/NTwain/Triplets/DGControl.EntryPoint.cs
Normal file
20
src/NTwain/Triplets/DGControl.EntryPoint.cs
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
using NTwain.Data;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace NTwain.Triplets
|
||||||
|
{
|
||||||
|
sealed class EntryPoint : BaseTriplet
|
||||||
|
{
|
||||||
|
internal EntryPoint(TwainSession session) : base(session) { }
|
||||||
|
|
||||||
|
public ReturnCode Get(out TW_ENTRYPOINT entryPoint)
|
||||||
|
{
|
||||||
|
entryPoint = new TW_ENTRYPOINT();
|
||||||
|
return NativeMethods.DsmWin32(session.Config.AppWin32, null,
|
||||||
|
DataGroups.Control, DataArgumentType.EntryPoint, Message.Get, entryPoint);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,20 +6,40 @@ using System.Text;
|
|||||||
|
|
||||||
namespace NTwain.Triplets
|
namespace NTwain.Triplets
|
||||||
{
|
{
|
||||||
public partial class Parent : BaseTriplet
|
sealed partial class Parent : BaseTriplet
|
||||||
{
|
{
|
||||||
internal Parent(TwainSession session) : base(session) { }
|
internal Parent(TwainSession session) : base(session) { }
|
||||||
|
|
||||||
public ReturnCode OpenDSM(ref IntPtr hWnd)
|
public ReturnCode OpenDSM(ref IntPtr hWnd)
|
||||||
{
|
{
|
||||||
var rc = NativeMethods.DsmWin32(session.Config.AppWin32, null, DataGroups.Control, DataArgumentType.Parent, Message.OpenDSM, ref hWnd);
|
var rc = NativeMethods.DsmWin32(session.Config.AppWin32, null,
|
||||||
if (rc == ReturnCode.Success) session.State = TwainState.DsmOpened;
|
DataGroups.Control, DataArgumentType.Parent, Message.OpenDSM, ref hWnd);
|
||||||
|
if (rc == ReturnCode.Success)
|
||||||
|
{
|
||||||
|
session.State = TwainState.DsmOpened;
|
||||||
|
|
||||||
|
// if twain2 then get memory management functions
|
||||||
|
if ((session.Config.AppWin32.DataFunctionalities & DataFunctionalities.Dsm2) == DataFunctionalities.Dsm2)
|
||||||
|
{
|
||||||
|
TW_ENTRYPOINT entry;
|
||||||
|
rc = session.DGControl.EntryPoint.Get(out entry);
|
||||||
|
if (rc == ReturnCode.Success)
|
||||||
|
{
|
||||||
|
session.Config.MemoryManager = entry;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
rc = CloseDSM(ref hWnd);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ReturnCode CloseDSM(ref IntPtr hWnd)
|
public ReturnCode CloseDSM(ref IntPtr hWnd)
|
||||||
{
|
{
|
||||||
var rc = NativeMethods.DsmWin32(session.Config.AppWin32, null, DataGroups.Control, DataArgumentType.Parent, Message.CloseDSM, ref hWnd);
|
var rc = NativeMethods.DsmWin32(session.Config.AppWin32, null, DataGroups.Control,
|
||||||
|
DataArgumentType.Parent, Message.CloseDSM, ref hWnd);
|
||||||
if (rc == ReturnCode.Success) session.State = TwainState.DsmLoaded;
|
if (rc == ReturnCode.Success) session.State = TwainState.DsmLoaded;
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,22 @@
|
|||||||
using System;
|
using NTwain.Data;
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace NTwain.Triplets
|
namespace NTwain.Triplets
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Represents <see cref="DataGroups.Control"/>.
|
||||||
|
/// </summary>
|
||||||
public partial class DGControl : BaseTriplet
|
public partial class DGControl : BaseTriplet
|
||||||
{
|
{
|
||||||
internal DGControl(TwainSession session) : base(session) { }
|
internal DGControl(TwainSession session) : base(session) { }
|
||||||
|
|
||||||
Parent _parent;
|
Parent _parent;
|
||||||
internal Parent Parent => _parent ?? (_parent = new Parent(session));
|
internal Parent Parent => _parent ?? (_parent = new Parent(session));
|
||||||
|
|
||||||
|
EntryPoint _entryPoint;
|
||||||
|
internal EntryPoint EntryPoint => _entryPoint ?? (_entryPoint = new EntryPoint(session));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,9 @@ using System.Text;
|
|||||||
|
|
||||||
namespace NTwain.Triplets
|
namespace NTwain.Triplets
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Provides direct access to the triplet call.
|
||||||
|
/// </summary>
|
||||||
public partial class DGCustom : BaseTriplet
|
public partial class DGCustom : BaseTriplet
|
||||||
{
|
{
|
||||||
internal DGCustom(TwainSession session) : base(session) { }
|
internal DGCustom(TwainSession session) : base(session) { }
|
||||||
|
|||||||
@@ -1,10 +1,14 @@
|
|||||||
using System;
|
using NTwain.Data;
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace NTwain.Triplets
|
namespace NTwain.Triplets
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Represents <see cref="DataGroups.Image"/>.
|
||||||
|
/// </summary>
|
||||||
public partial class DGImage : BaseTriplet
|
public partial class DGImage : BaseTriplet
|
||||||
{
|
{
|
||||||
internal DGImage(TwainSession session) : base(session) { }
|
internal DGImage(TwainSession session) : base(session) { }
|
||||||
|
|||||||
@@ -88,7 +88,7 @@ namespace NTwain.Triplets
|
|||||||
DataGroups dg,
|
DataGroups dg,
|
||||||
DataArgumentType dat,
|
DataArgumentType dat,
|
||||||
Message msg,
|
Message msg,
|
||||||
ref TW_ENTRYPOINT data);
|
[In, Out]TW_ENTRYPOINT data);
|
||||||
|
|
||||||
[DllImport(WinDsmDll, EntryPoint = EntryName)]
|
[DllImport(WinDsmDll, EntryPoint = EntryName)]
|
||||||
public static extern ReturnCode DsmWin32(
|
public static extern ReturnCode DsmWin32(
|
||||||
|
|||||||
@@ -13,12 +13,17 @@ namespace NTwain
|
|||||||
{
|
{
|
||||||
internal TwainConfig() { }
|
internal TwainConfig() { }
|
||||||
|
|
||||||
|
public bool Is64Bit { get; internal set; }
|
||||||
|
|
||||||
|
public PlatformID Platform { get; internal set; }
|
||||||
|
|
||||||
//public bool PreferLegacyDsm { get; internal set; }
|
//public bool PreferLegacyDsm { get; internal set; }
|
||||||
|
|
||||||
internal TW_IDENTITY AppWin32 { get; set; }
|
internal TW_IDENTITY AppWin32 { get; set; }
|
||||||
|
|
||||||
internal TW_IDENTITY SrcWin32 { get; set; }
|
internal TW_IDENTITY SrcWin32 { get; set; }
|
||||||
|
|
||||||
internal IMemoryManager MemoryManager { get; set; }
|
|
||||||
|
public IMemoryManager MemoryManager { get; internal set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,15 @@
|
|||||||
using NTwain.Data;
|
using NTwain.Data;
|
||||||
|
using NTwain.Internals;
|
||||||
using System;
|
using System;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
|
||||||
namespace NTwain
|
namespace NTwain
|
||||||
{
|
{
|
||||||
public class TwainConfigurationBuilder
|
/// <summary>
|
||||||
|
/// Builder for generating a <see cref="TwainConfig"/> object.
|
||||||
|
/// </summary>
|
||||||
|
public class TwainConfigBuilder
|
||||||
{
|
{
|
||||||
private bool _legacy;
|
private bool _legacy;
|
||||||
private string _appName;
|
private string _appName;
|
||||||
@@ -20,7 +24,7 @@ namespace NTwain
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Default ctor.
|
/// Default ctor.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public TwainConfigurationBuilder()
|
public TwainConfigBuilder()
|
||||||
{
|
{
|
||||||
_64bit = IntPtr.Size == 8;
|
_64bit = IntPtr.Size == 8;
|
||||||
_platform = Environment.OSVersion.Platform;
|
_platform = Environment.OSVersion.Platform;
|
||||||
@@ -47,7 +51,7 @@ namespace NTwain
|
|||||||
/// <param name="image"></param>
|
/// <param name="image"></param>
|
||||||
/// <param name="audio"></param>
|
/// <param name="audio"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public TwainConfigurationBuilder HandlesDataType(bool image = true, bool audio = false)
|
public TwainConfigBuilder HandlesDataType(bool image = true, bool audio = false)
|
||||||
{
|
{
|
||||||
DataGroups dg = DataGroups.None;
|
DataGroups dg = DataGroups.None;
|
||||||
if (image) dg |= DataGroups.Image;
|
if (image) dg |= DataGroups.Image;
|
||||||
@@ -68,7 +72,7 @@ namespace NTwain
|
|||||||
/// <param name="language"></param>
|
/// <param name="language"></param>
|
||||||
/// <param name="country"></param>
|
/// <param name="country"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public TwainConfigurationBuilder DefineApp(string appName,
|
public TwainConfigBuilder DefineApp(string appName,
|
||||||
Version appVersion, string companyName = null,
|
Version appVersion, string companyName = null,
|
||||||
Language language = Language.EnglishUSA, Country country = Country.USA)
|
Language language = Language.EnglishUSA, Country country = Country.USA)
|
||||||
{
|
{
|
||||||
@@ -87,7 +91,7 @@ namespace NTwain
|
|||||||
/// <param name="language"></param>
|
/// <param name="language"></param>
|
||||||
/// <param name="country"></param>
|
/// <param name="country"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public TwainConfigurationBuilder DefineApp(Assembly appAssembly,
|
public TwainConfigBuilder DefineApp(Assembly appAssembly,
|
||||||
Language language = Language.EnglishUSA, Country country = Country.USA)
|
Language language = Language.EnglishUSA, Country country = Country.USA)
|
||||||
{
|
{
|
||||||
var info = FileVersionInfo.GetVersionInfo(appAssembly.Location);
|
var info = FileVersionInfo.GetVersionInfo(appAssembly.Location);
|
||||||
@@ -101,15 +105,17 @@ namespace NTwain
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public TwainConfig Build()
|
public TwainConfig Build()
|
||||||
{
|
{
|
||||||
var config = new TwainConfig();
|
var config = new TwainConfig
|
||||||
|
{
|
||||||
|
Platform = _platform,
|
||||||
|
Is64Bit = _64bit
|
||||||
|
};
|
||||||
|
|
||||||
// todo: change id based on platform
|
// todo: change id based on platform
|
||||||
switch (_platform)
|
switch (_platform)
|
||||||
{
|
{
|
||||||
case PlatformID.Win32NT:
|
case PlatformID.Win32NT:
|
||||||
case PlatformID.Unix:
|
config.MemoryManager = new WinMemoryManager(); // initial default
|
||||||
case PlatformID.MacOSX:
|
|
||||||
default:
|
|
||||||
config.AppWin32 = new TW_IDENTITY
|
config.AppWin32 = new TW_IDENTITY
|
||||||
{
|
{
|
||||||
DataFunctionalities = DataFunctionalities.App2,
|
DataFunctionalities = DataFunctionalities.App2,
|
||||||
@@ -129,6 +135,10 @@ namespace NTwain
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
break;
|
break;
|
||||||
|
case PlatformID.Unix:
|
||||||
|
case PlatformID.MacOSX:
|
||||||
|
default:
|
||||||
|
throw new PlatformNotSupportedException($"This platform {_platform} is not supported.");
|
||||||
}
|
}
|
||||||
return config;
|
return config;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user