PerformTwainCommand(TwainDataGroup,UInt16,TwainMessage,IntPtr) Метод (Device)
Выполняет команду TWAIN.
Вот C#/VB.NET код, который демонстрирует, как получить значение возможности XferCount с помощью команды TWAIN.
''' <summary>
''' Contains information about TWAIN capability.
''' </summary>
<System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential, Pack := 2)> _
Private Class TwainCapability
Friend Cap As System.UInt16
Friend ConType As System.Int16
Friend Handle As System.IntPtr
End Class
''' <summary>
''' Contains information about single TWAIN value.
''' </summary>
<System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential, Pack := 2)> _
Private Class TwainOneValue
Friend ItemType As Short
Friend Item As System.UInt32
End Class
''' <summary>
''' Performs a TWAIN command for getting value of XferCount capability.
''' </summary>
Public Shared Sub GetXferCountValueUsingTwainCommand(device As Vintasoft.Twain.Device)
' open the device
device.Open()
' create TWAIN capability
Dim twainCapability As New TwainCapability()
twainCapability.Cap = CUShort(Vintasoft.Twain.DeviceCapabilityId.XferCount)
twainCapability.ConType = CShort(Vintasoft.Twain.TwainValueContainerType.OneValue)
twainCapability.Handle = System.IntPtr.Zero
' create container in not managed memory for TWAIN capability
Dim twainCapabilitySize As Integer = System.Runtime.InteropServices.Marshal.SizeOf(twainCapability)
Dim twainCapabilityPtr As System.IntPtr = System.IntPtr.Zero
Try
twainCapabilityPtr = System.Runtime.InteropServices.Marshal.AllocHGlobal(twainCapabilitySize)
System.Runtime.InteropServices.Marshal.StructureToPtr(twainCapability, twainCapabilityPtr, True)
' perform TWAIN command
Dim result As Vintasoft.Twain.TwainReturnCode = device.PerformTwainCommand(Vintasoft.Twain.TwainDataGroup.Control, CUShort(Vintasoft.Twain.TwainDataArgumentType.Capability), Vintasoft.Twain.TwainMessage.[Get], twainCapabilityPtr)
' if command is performed successfully
If result = Vintasoft.Twain.TwainReturnCode.Success Then
' get value of TWAIN capability
System.Runtime.InteropServices.Marshal.PtrToStructure(twainCapabilityPtr, twainCapability)
' get value of XferCount capability
Dim twainOneValue As New TwainOneValue()
System.Runtime.InteropServices.Marshal.PtrToStructure(twainCapability.Handle, twainOneValue)
' ouput value of XferCount capability
System.Console.WriteLine(String.Format("XferCount: Type={0}, Value={1}", twainOneValue.ItemType, CInt(twainOneValue.Item)))
Else
' if command is failed
System.Console.WriteLine(String.Format("Error: {0}", result))
End If
Finally
If twainCapabilityPtr <> System.IntPtr.Zero Then
If twainCapability.Handle <> System.IntPtr.Zero Then
System.Runtime.InteropServices.Marshal.FreeHGlobal(twainCapability.Handle)
End If
System.Runtime.InteropServices.Marshal.FreeHGlobal(twainCapabilityPtr)
End If
End Try
' close the device
device.Close()
End Sub
/// <summary>
/// Contains information about TWAIN capability.
/// </summary>
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential, Pack = 2)]
class TwainCapability
{
internal System.UInt16 Cap;
internal System.Int16 ConType;
internal System.IntPtr Handle;
}
/// <summary>
/// Contains information about single TWAIN value.
/// </summary>
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential, Pack = 2)]
class TwainOneValue
{
internal short ItemType;
internal System.UInt32 Item;
}
/// <summary>
/// Performs a TWAIN command for getting value of XferCount capability.
/// </summary>
public static void GetXferCountValueUsingTwainCommand(Vintasoft.Twain.Device device)
{
// open the device
device.Open();
// create TWAIN capability
TwainCapability twainCapability = new TwainCapability();
twainCapability.Cap = (System.UInt16)Vintasoft.Twain.DeviceCapabilityId.XferCount;
twainCapability.ConType = (System.Int16)Vintasoft.Twain.TwainValueContainerType.OneValue;
twainCapability.Handle = System.IntPtr.Zero;
// create container in not managed memory for TWAIN capability
int twainCapabilitySize = System.Runtime.InteropServices.Marshal.SizeOf(twainCapability);
System.IntPtr twainCapabilityPtr = System.IntPtr.Zero;
try
{
twainCapabilityPtr = System.Runtime.InteropServices.Marshal.AllocHGlobal(twainCapabilitySize);
System.Runtime.InteropServices.Marshal.StructureToPtr(twainCapability, twainCapabilityPtr, true);
// perform TWAIN command
Vintasoft.Twain.TwainReturnCode result = device.PerformTwainCommand(
Vintasoft.Twain.TwainDataGroup.Control,
(ushort)Vintasoft.Twain.TwainDataArgumentType.Capability,
Vintasoft.Twain.TwainMessage.Get,
ref twainCapabilityPtr);
// if command is performed successfully
if (result == Vintasoft.Twain.TwainReturnCode.Success)
{
// get value of TWAIN capability
System.Runtime.InteropServices.Marshal.PtrToStructure(twainCapabilityPtr, twainCapability);
// get value of XferCount capability
TwainOneValue twainOneValue = new TwainOneValue();
System.Runtime.InteropServices.Marshal.PtrToStructure(twainCapability.Handle, twainOneValue);
// ouput value of XferCount capability
System.Console.WriteLine(string.Format("XferCount: Type={0}, Value={1}", twainOneValue.ItemType, (System.Int32)twainOneValue.Item));
}
// if command is failed
else
{
System.Console.WriteLine(string.Format("Error: {0}", result));
}
}
finally
{
if (twainCapabilityPtr != System.IntPtr.Zero)
{
if (twainCapability.Handle != System.IntPtr.Zero)
System.Runtime.InteropServices.Marshal.FreeHGlobal(twainCapability.Handle);
System.Runtime.InteropServices.Marshal.FreeHGlobal(twainCapabilityPtr);
}
}
// close the device
device.Close();
}
Целевые платформы: .NET 8; .NET 7; .NET 6; .NET Framework 4.8, 4.7, 4.6, 4.5, 4.0, 3.5