twain_library icon indicating copy to clipboard operation
twain_library copied to clipboard

Can't read TWAIN source from Array

Open Finn0811 opened this issue 1 year ago • 1 comments

Hi, thanks a lot for providing this library. I somehow can't read twain sources from DTWAIN_ARRAYSOURCE.

This is the code I tried with (C#):

               // Get the total number of available sources (TWAIN devices)
                var sourceArray = TwainAPI.DTWAIN_ArrayCreate(TwainAPI.DTWAIN_ARRAYSOURCE, 0);
                int result1 = TwainAPI.DTWAIN_EnumSources(ref sourceArray);
                if (result1 == 0)
                {
                    Console.WriteLine("Error enumerating TWAIN sources.");
                    return;
                }

                int numSources = TwainAPI.DTWAIN_ArrayGetCount(sourceArray);
                if (numSources == 0)
                {
                    Console.WriteLine("No TWAIN sources found.");
                    return;
                }

                Console.WriteLine($"Found {numSources} TWAIN sources:");
                // Loop through all sources
                for (int i = 0; i < numSources; i++)
                {
                    IntPtr sourcePtr = IntPtr.Zero;

                    long arrayType = TwainAPI.DTWAIN_ArrayGetType(sourceArray);
                    Console.WriteLine("array type: " + arrayType);
                    if (TwainAPI.DTWAIN_ArrayGetAt(sourceArray, i, sourcePtr) != 0)
                    {
                        Console.WriteLine(sourcePtr);
                        // Get the product name of the source
                        StringBuilder productName = new StringBuilder(255);
                        if (TwainAPI.DTWAIN_GetSourceProductName(sourcePtr, productName, productName.Capacity) == 0)
                        {
                            Console.WriteLine($"{i + 1}. {productName.ToString()}");
                        }
                        else
                        {
                            int lastError = TwainAPI.DTWAIN_GetLastError();
                            Console.WriteLine($"Error retrieving product name for source {i + 1}. Error code: {lastError}");
                        }
                    }
                    else
                    {
                        int lastError = TwainAPI.DTWAIN_GetLastError();
                        Console.WriteLine($"Error retrieving source at index {i}. Error code: {lastError}");
                    }
                }

This is the output:

Found 1 TWAIN sources:
array type: 5
Error retrieving source at index 0. Error code: 0

What am I doing wrong? :)

Finn0811 avatar Sep 30 '24 14:09 Finn0811

See the sample code here.

This is in the C# Full Demo program. You should use the demo to get an idea of how to use most of the DTWAIN functionality within C#. Since that function populates a custom "select source" dialog with the names, you can use it as a guide in getting all the source information.

This uses the undocumented DTWAIN_ArrayGetSourceAt function. Using DTWAIN_ArrayGetAt alone is cumbersome when it comes to retrieving Sources, since it requires pointer-to-pointer access. The DTWAIN_ArrayGetSourceAt makes this a lot simpler for C# programmers.

Even though DTWAIN_ArrayGetSourceAt is undocumented, it will never be removed from DTWAIN, so your code is safe to use it.

dynarithmic avatar Oct 01 '24 01:10 dynarithmic

Awesome, that works perfectly. Thank you! 👍

Looks like that's the reason why I missed that function while looking at https://www.dynarithmic.com/onlinehelp/dtwain/newversion/DTWAIN_ArrayGetAt.html

Finn0811 avatar Oct 01 '24 08:10 Finn0811