Added memory mgmt entry.

This commit is contained in:
Eugene Wang
2018-11-13 20:04:05 -05:00
parent 4d3690874d
commit 63526070f8
13 changed files with 126 additions and 39 deletions

View File

@@ -8,18 +8,33 @@ namespace NetCoreConsole
{
static void Main(string[] args)
{
var config = new TwainConfigurationBuilder()
.DefineApp(Assembly.GetExecutingAssembly())
.Build();
using (var session = new TwainSession(config))
try
{
session.PropertyChanged += Session_PropertyChanged;
var config = new TwainConfigBuilder()
.DefineApp(Assembly.GetExecutingAssembly())
.Build();
Console.WriteLine($"App = {(config.Is64Bit ? "64bit" : "32bit")}");
Console.WriteLine($"Platform = {config.Platform}");
var handle = IntPtr.Zero;
session.Open(ref handle);
using (var session = new TwainSession(config))
{
session.PropertyChanged += Session_PropertyChanged;
var handle = IntPtr.Zero;
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)
@@ -27,7 +42,7 @@ namespace NetCoreConsole
var session = (TwainSession)sender;
if (e.PropertyName == "State")
{
Console.WriteLine($"State changed to {session.State}");
Console.WriteLine($"Session state changed to {session.State}");
}
}
}

View File

@@ -546,7 +546,7 @@ namespace NTwain.Data
DataGroups dg, DataArgumentType dat, Message msg, TW_MEMREF data);
[StructLayout(LayoutKind.Sequential, Pack = 2)]
partial struct TW_ENTRYPOINT
partial class TW_ENTRYPOINT
{
// TODO: linux 64 is different?
TW_UINT32 _size;

View File

@@ -2364,19 +2364,18 @@ namespace NTwain.Data
}
/// <summary>
/// Provides entry points required by TWAIN 2.0 Applications and Sources.
/// </summary>
partial struct TW_ENTRYPOINT : IMemoryManager
partial class TW_ENTRYPOINT : IMemoryManager
{
///// <summary>
///// Initializes a new instance of the <see cref="TW_ENTRYPOINT"/> class.
///// </summary>
//public TW_ENTRYPOINT()
//{
// _size = (uint)Marshal.SizeOf(this);
//}
/// <summary>
/// Initializes a new instance of the <see cref="TW_ENTRYPOINT"/> class.
/// </summary>
public TW_ENTRYPOINT()
{
_size = (uint)Marshal.SizeOf(this);
}
#region IMemoryManager Members

View File

@@ -3,12 +3,12 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NTwain.Internals
namespace NTwain
{
/// <summary>
/// Interface that provides the correct methods for managing memory on data exchanged with TWAIN sources.
/// </summary>
interface IMemoryManager
public interface IMemoryManager
{
/// <summary>
/// Function to allocate memory. Calls to this must be coupled with <see cref="Free"/> later.

View File

@@ -1,10 +1,14 @@
using System;
using NTwain.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NTwain.Triplets
{
/// <summary>
/// Represents <see cref="DataGroups.Audio"/>.
/// </summary>
public partial class DGAudio : BaseTriplet
{
internal DGAudio(TwainSession session) : base(session) { }

View 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);
}
}
}

View File

@@ -6,20 +6,40 @@ using System.Text;
namespace NTwain.Triplets
{
public partial class Parent : BaseTriplet
sealed partial class Parent : BaseTriplet
{
internal Parent(TwainSession session) : base(session) { }
public ReturnCode OpenDSM(ref IntPtr hWnd)
{
var rc = NativeMethods.DsmWin32(session.Config.AppWin32, null, DataGroups.Control, DataArgumentType.Parent, Message.OpenDSM, ref hWnd);
if (rc == ReturnCode.Success) session.State = TwainState.DsmOpened;
var rc = NativeMethods.DsmWin32(session.Config.AppWin32, null,
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;
}
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;
return rc;
}

View File

@@ -1,15 +1,22 @@
using System;
using NTwain.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NTwain.Triplets
{
/// <summary>
/// Represents <see cref="DataGroups.Control"/>.
/// </summary>
public partial class DGControl : BaseTriplet
{
internal DGControl(TwainSession session) : base(session) { }
Parent _parent;
internal Parent Parent => _parent ?? (_parent = new Parent(session));
EntryPoint _entryPoint;
internal EntryPoint EntryPoint => _entryPoint ?? (_entryPoint = new EntryPoint(session));
}
}

View File

@@ -5,6 +5,9 @@ using System.Text;
namespace NTwain.Triplets
{
/// <summary>
/// Provides direct access to the triplet call.
/// </summary>
public partial class DGCustom : BaseTriplet
{
internal DGCustom(TwainSession session) : base(session) { }

View File

@@ -1,10 +1,14 @@
using System;
using NTwain.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NTwain.Triplets
{
/// <summary>
/// Represents <see cref="DataGroups.Image"/>.
/// </summary>
public partial class DGImage : BaseTriplet
{
internal DGImage(TwainSession session) : base(session) { }

View File

@@ -88,7 +88,7 @@ namespace NTwain.Triplets
DataGroups dg,
DataArgumentType dat,
Message msg,
ref TW_ENTRYPOINT data);
[In, Out]TW_ENTRYPOINT data);
[DllImport(WinDsmDll, EntryPoint = EntryName)]
public static extern ReturnCode DsmWin32(

View File

@@ -13,12 +13,17 @@ namespace NTwain
{
internal TwainConfig() { }
public bool Is64Bit { get; internal set; }
public PlatformID Platform { get; internal set; }
//public bool PreferLegacyDsm { get; internal set; }
internal TW_IDENTITY AppWin32 { get; set; }
internal TW_IDENTITY SrcWin32 { get; set; }
internal IMemoryManager MemoryManager { get; set; }
public IMemoryManager MemoryManager { get; internal set; }
}
}

View File

@@ -1,11 +1,15 @@
using NTwain.Data;
using NTwain.Internals;
using System;
using System.Diagnostics;
using System.Reflection;
namespace NTwain
{
public class TwainConfigurationBuilder
/// <summary>
/// Builder for generating a <see cref="TwainConfig"/> object.
/// </summary>
public class TwainConfigBuilder
{
private bool _legacy;
private string _appName;
@@ -20,7 +24,7 @@ namespace NTwain
/// <summary>
/// Default ctor.
/// </summary>
public TwainConfigurationBuilder()
public TwainConfigBuilder()
{
_64bit = IntPtr.Size == 8;
_platform = Environment.OSVersion.Platform;
@@ -47,7 +51,7 @@ namespace NTwain
/// <param name="image"></param>
/// <param name="audio"></param>
/// <returns></returns>
public TwainConfigurationBuilder HandlesDataType(bool image = true, bool audio = false)
public TwainConfigBuilder HandlesDataType(bool image = true, bool audio = false)
{
DataGroups dg = DataGroups.None;
if (image) dg |= DataGroups.Image;
@@ -68,7 +72,7 @@ namespace NTwain
/// <param name="language"></param>
/// <param name="country"></param>
/// <returns></returns>
public TwainConfigurationBuilder DefineApp(string appName,
public TwainConfigBuilder DefineApp(string appName,
Version appVersion, string companyName = null,
Language language = Language.EnglishUSA, Country country = Country.USA)
{
@@ -87,7 +91,7 @@ namespace NTwain
/// <param name="language"></param>
/// <param name="country"></param>
/// <returns></returns>
public TwainConfigurationBuilder DefineApp(Assembly appAssembly,
public TwainConfigBuilder DefineApp(Assembly appAssembly,
Language language = Language.EnglishUSA, Country country = Country.USA)
{
var info = FileVersionInfo.GetVersionInfo(appAssembly.Location);
@@ -101,15 +105,17 @@ namespace NTwain
/// <returns></returns>
public TwainConfig Build()
{
var config = new TwainConfig();
var config = new TwainConfig
{
Platform = _platform,
Is64Bit = _64bit
};
// todo: change id based on platform
switch (_platform)
{
case PlatformID.Win32NT:
case PlatformID.Unix:
case PlatformID.MacOSX:
default:
config.MemoryManager = new WinMemoryManager(); // initial default
config.AppWin32 = new TW_IDENTITY
{
DataFunctionalities = DataFunctionalities.App2,
@@ -129,6 +135,10 @@ namespace NTwain
},
};
break;
case PlatformID.Unix:
case PlatformID.MacOSX:
default:
throw new PlatformNotSupportedException($"This platform {_platform} is not supported.");
}
return config;
}