VintaSoft Twain .NET SDK 15.4: Documentation for .NET developer
Vintasoft.WpfTwain Namespace / Device Class / PerformTwainCommand(TwainDataGroup,UInt16,TwainMessage,IntPtr) Method
Syntax Example Requirements SeeAlso
In This Topic
PerformTwainCommand(TwainDataGroup,UInt16,TwainMessage,IntPtr) Method (Device)
In This Topic
Performs TWAIN command.
Syntax
'Declaration

Public Function PerformTwainCommand( _
ByVal group
An identifier of command data group.
As TwainDataGroup, _
ByVal dataArgumentType
The type of object, which must be passed as the data parameter.
As UShort, _
ByVal twainMessage
The type of message, which must be sent to the device.
As TwainMessage, _
ByRef data
A pointer to the command data.
As IntPtr _
) As TwainReturnCode

Parameters

group
An identifier of command data group.
dataArgumentType
The type of object, which must be passed as the data parameter.
twainMessage
The type of message, which must be sent to the device.
data
A pointer to the command data.

Return Value

TWAIN return code.
Example

This C#/VB.NET code shows how to get value of XferCount capability using TWAIN command.


''' <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();
}

Requirements

Target Platforms: .NET 10; .NET 9; .NET 8; .NET 7; .NET 6; .NET Framework 4.8, 4.7, 4.6, 4.5, 4.0, 3.5

See Also