Added first struct selection logic by platform & bitness.

This commit is contained in:
Eugene Wang
2018-11-15 20:31:39 -05:00
parent be8291521e
commit e937c35a53
7 changed files with 84 additions and 17 deletions

View File

@@ -20,6 +20,15 @@ namespace NTwain.Triplets
protected BaseTriplet(TwainSession session)
{
this.Session = session ?? throw new ArgumentNullException(nameof(session));
// windows can always use 32bit structs even in 64bit app
Use32BitData = session.Config.Platform == System.PlatformID.Win32NT ||
!session.Config.Is64Bit;
}
/// <summary>
/// Whether to use 32bit data structures.
/// </summary>
protected readonly bool Use32BitData;
}
}

View File

@@ -9,8 +9,12 @@ namespace NTwain.Triplets.Control
public ReturnCode RegisterCallback(ref TW_CALLBACK callback)
{
return NativeMethods.Dsm32(Session.Config.App32, Session.CurrentSource.Identity,
DataGroups.Control, DataArgumentType.Callback, Message.RegisterCallback, ref callback);
if (Use32BitData)
{
return NativeMethods.Dsm32(Session.Config.App32, Session.CurrentSource.Identity,
DataGroups.Control, DataArgumentType.Callback, Message.RegisterCallback, ref callback);
}
return ReturnCode.Failure;
}
}
}

View File

@@ -9,8 +9,12 @@ namespace NTwain.Triplets.Control
public ReturnCode RegisterCallback(ref TW_CALLBACK2 callback)
{
return NativeMethods.Dsm32(Session.Config.App32, Session.CurrentSource.Identity,
if (Use32BitData)
{
return NativeMethods.Dsm32(Session.Config.App32, Session.CurrentSource.Identity,
DataGroups.Control, DataArgumentType.Callback2, Message.RegisterCallback, ref callback);
}
return ReturnCode.Failure;
}
}
}

View File

@@ -6,11 +6,15 @@ namespace NTwain.Triplets.Control
sealed class DeviceEvent : BaseTriplet
{
internal DeviceEvent(TwainSession session) : base(session) { }
public ReturnCode Get(ref TW_DEVICEEVENT sourceDeviceEvent)
{
return NativeMethods.Dsm32(Session.Config.App32, Session.CurrentSource.Identity,
{
if (Use32BitData)
{
return NativeMethods.Dsm32(Session.Config.App32, Session.CurrentSource.Identity,
DataGroups.Control, DataArgumentType.DeviceEvent, Message.Get, ref sourceDeviceEvent);
}
}
return ReturnCode.Failure;
}
}
}

View File

@@ -13,8 +13,12 @@ namespace NTwain.Triplets.Control
public ReturnCode Get(out TW_ENTRYPOINT entryPoint)
{
entryPoint = new TW_ENTRYPOINT();
return NativeMethods.Dsm32(Session.Config.App32, null,
if (Use32BitData)
{
return NativeMethods.Dsm32(Session.Config.App32, null,
DataGroups.Control, DataArgumentType.EntryPoint, Message.Get, entryPoint);
}
return ReturnCode.Failure;
}
}
}

View File

@@ -10,8 +10,14 @@ namespace NTwain.Triplets.Control
public ReturnCode CloseDS(TW_IDENTITY source)
{
var rc = NativeMethods.Dsm32(Session.Config.App32, IntPtr.Zero,
var rc = ReturnCode.Failure;
if (Use32BitData)
{
rc = NativeMethods.Dsm32(Session.Config.App32, IntPtr.Zero,
DataGroups.Control, DataArgumentType.Identity, Message.CloseDS, source);
}
if (rc == ReturnCode.Success)
{
Session.State = TwainState.S3;
@@ -23,16 +29,24 @@ namespace NTwain.Triplets.Control
public ReturnCode GetDefault(out TW_IDENTITY source)
{
source = new TW_IDENTITY();
return NativeMethods.Dsm32(Session.Config.App32, IntPtr.Zero,
if (Use32BitData)
{
return NativeMethods.Dsm32(Session.Config.App32, IntPtr.Zero,
DataGroups.Control, DataArgumentType.Identity, Message.GetDefault, source);
}
return ReturnCode.Failure;
}
public ReturnCode GetFirst(out TW_IDENTITY source)
{
source = new TW_IDENTITY();
return NativeMethods.Dsm32(Session.Config.App32, IntPtr.Zero,
if (Use32BitData)
{
return NativeMethods.Dsm32(Session.Config.App32, IntPtr.Zero,
DataGroups.Control, DataArgumentType.Identity, Message.GetFirst, source);
}
return ReturnCode.Failure;
}
public ReturnCode GetNext(out TW_IDENTITY source)
@@ -45,9 +59,14 @@ namespace NTwain.Triplets.Control
public ReturnCode OpenDS(TW_IDENTITY source)
{
Session.StepDown(TwainState.DsmOpened);
var rc = ReturnCode.Failure;
var rc = NativeMethods.Dsm32(Session.Config.App32, IntPtr.Zero,
if (Use32BitData)
{
rc = NativeMethods.Dsm32(Session.Config.App32, IntPtr.Zero,
DataGroups.Control, DataArgumentType.Identity, Message.OpenDS, source);
}
if (rc == ReturnCode.Success)
{
Session.CurrentSource = Session.GetSourceSingleton(source);
@@ -59,15 +78,23 @@ namespace NTwain.Triplets.Control
public ReturnCode Set(DataSource source)
{
return NativeMethods.Dsm32(Session.Config.App32, IntPtr.Zero,
if (Use32BitData)
{
return NativeMethods.Dsm32(Session.Config.App32, IntPtr.Zero,
DataGroups.Control, DataArgumentType.Identity, Message.Set, source?.Identity);
}
return ReturnCode.Failure;
}
public ReturnCode UserSelect(out TW_IDENTITY source)
{
source = new TW_IDENTITY();
return NativeMethods.Dsm32(Session.Config.App32, IntPtr.Zero,
if (Use32BitData)
{
return NativeMethods.Dsm32(Session.Config.App32, IntPtr.Zero,
DataGroups.Control, DataArgumentType.Identity, Message.UserSelect, source);
}
return ReturnCode.Failure;
}
}
}

View File

@@ -12,8 +12,14 @@ namespace NTwain.Triplets.Control
public ReturnCode OpenDSM(IntPtr hWnd)
{
var rc = NativeMethods.Dsm32(Session.Config.App32, null,
var rc = ReturnCode.Failure;
if (Use32BitData)
{
rc = NativeMethods.Dsm32(Session.Config.App32, null,
DataGroups.Control, DataArgumentType.Parent, Message.OpenDSM, ref hWnd);
}
if (rc == ReturnCode.Success)
{
Session.State = TwainState.DsmOpened;
@@ -37,9 +43,18 @@ namespace NTwain.Triplets.Control
public ReturnCode CloseDSM(IntPtr hWnd)
{
var rc = NativeMethods.Dsm32(Session.Config.App32, null, DataGroups.Control,
var rc = ReturnCode.Failure;
if (Use32BitData)
{
rc = NativeMethods.Dsm32(Session.Config.App32, 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;
}
}