VintaSoft Twain .NET SDK v10.3
Vintasoft.Twain Namespace / Device Class / PerformTwainCommand Method
An identifier of command data group.
The type of object, which must be passed as the data parameter.
The type of message, which must be sent to the device.
A pointer to the command data.
Example




In This Topic
    PerformTwainCommand Method (Device)
    In This Topic
    Performs TWAIN command.
    Syntax
    'Declaration
     
    Public Function PerformTwainCommand( _
       ByVal group As TwainDataGroup, _
       ByVal dataArgumentType As UShort, _
       ByVal twainMessage As TwainMessage, _
       ByRef data As IntPtr _
    ) As TwainReturnCode
    'Usage
     
    Dim instance As Device
    Dim group As TwainDataGroup
    Dim dataArgumentType As UShort
    Dim twainMessage As TwainMessage
    Dim data As IntPtr
    Dim value As TwainReturnCode
     
    value = instance.PerformTwainCommand(group, dataArgumentType, twainMessage, data)
    public TwainReturnCode PerformTwainCommand( 
       TwainDataGroup group,
       ushort dataArgumentType,
       TwainMessage twainMessage,
       ref IntPtr data
    )
    public: TwainReturnCode PerformTwainCommand( 
       TwainDataGroup group,
       ushort dataArgumentType,
       TwainMessage twainMessage,
       ref IntPtr data
    ) 
    public:
    TwainReturnCode PerformTwainCommand( 
       TwainDataGroup group,
       ushort dataArgumentType,
       TwainMessage twainMessage,
       IntPtr% data
    ) 

    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.
    Example
    This example 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 UInt16
        Friend ConType As Int16
        Friend Handle As 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 Int32
    End Class
                
    ''' <summary>
    ''' Performs a TWAIN command for getting value of XferCount capability.
    ''' </summary>
    Public Shared Sub GetXferCountValueUsingTwainCommand(ByVal device As Device)
        ' open the device
        device.Open()
                
        ' create TWAIN capability
        Dim twainCapability As New TwainCapability()
        twainCapability.Cap = CType(DeviceCapabilityId.XferCount, UInt16)
        twainCapability.ConType = CType(TwainValueContainerType.OneValue, Int16)
        twainCapability.Handle = IntPtr.Zero
                
        ' create container in not managed memory for TWAIN capability
        Dim twainCapabilitySize As Integer = System.Runtime.InteropServices.Marshal.SizeOf(twainCapability)
        Dim twainCapabilityPtr As IntPtr = IntPtr.Zero
        Try
            twainCapabilityPtr = System.Runtime.InteropServices.Marshal.AllocHGlobal(twainCapabilitySize)
            System.Runtime.InteropServices.Marshal.StructureToPtr(twainCapability, twainCapabilityPtr, True)
                
            ' perform TWAIN command
            Dim result As TwainReturnCode = device.PerformTwainCommand(TwainDataGroup.Control, CUShort(TwainDataArgumentType.Capability), TwainMessage.[Get], twainCapabilityPtr)
            ' if command is performed successfully
            If result = 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 
                Console.WriteLine(String.Format("XferCount: Type={0}, Value={1}", twainOneValue.ItemType, twainOneValue.Item))
            Else
                ' if command is failed
                Console.WriteLine(String.Format("Error: {0}", result))
            End If
        Finally
            If twainCapabilityPtr <> IntPtr.Zero Then
                If twainCapability.Handle <> 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 UInt16 Cap;
        internal Int16 ConType;
        internal 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 UInt32 Item;
    }
                
    /// <summary>
    /// Performs a TWAIN command for getting value of XferCount capability.
    /// </summary>
    public static void GetXferCountValueUsingTwainCommand(Device device)
    {
        // open the device
        device.Open();
                
        // create TWAIN capability
        TwainCapability twainCapability = new TwainCapability();
        twainCapability.Cap = (UInt16)DeviceCapabilityId.XferCount;
        twainCapability.ConType = (Int16)TwainValueContainerType.OneValue;
        twainCapability.Handle = IntPtr.Zero;
                
        // create container in not managed memory for TWAIN capability
        int twainCapabilitySize = System.Runtime.InteropServices.Marshal.SizeOf(twainCapability);
        IntPtr twainCapabilityPtr = IntPtr.Zero;
        try
        {
            twainCapabilityPtr = System.Runtime.InteropServices.Marshal.AllocHGlobal(twainCapabilitySize);
            System.Runtime.InteropServices.Marshal.StructureToPtr(twainCapability, twainCapabilityPtr, true);
                
            // perform TWAIN command
            TwainReturnCode result = device.PerformTwainCommand(
                TwainDataGroup.Control,
                (ushort)TwainDataArgumentType.Capability,
                TwainMessage.Get,
                ref twainCapabilityPtr);
            // if command is performed successfully
            if (result == 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 
                Console.WriteLine(string.Format("XferCount: Type={0}, Value={1}", twainOneValue.ItemType, (Int32)twainOneValue.Item));
            }
            // if command is failed
            else
            {
                Console.WriteLine(string.Format("Error: {0}", result));
            }
        }
        finally
        {
            if (twainCapabilityPtr != IntPtr.Zero)
            {
                if (twainCapability.Handle != IntPtr.Zero)
                    System.Runtime.InteropServices.Marshal.FreeHGlobal(twainCapability.Handle);
                System.Runtime.InteropServices.Marshal.FreeHGlobal(twainCapabilityPtr);
            }
        }
                
        // close the device
        device.Close();
    }
    Requirements

    Target Platforms: Windows 10, Windows 8, Windows 7, Windows Vista, Windows XP, Windows Server 2016, Windows Server 2012, Windows Server 2008, Windows Server 2003

    See Also