Search code examples
c#.nettwain

Get List of Twain 1.x installed drivers


I am using version 3.0 of the nTwain library ( https://bitbucket.org/soukoku/ntwain ). I need to get a list of Twain 1.x drivers instead of the default Twain 2.x drivers. Here is the code I am using to get the list:

            List<string> tempScanners = new List<string>;

        System.Diagnostics.FileVersionInfo appVer = System.Diagnostics.FileVersionInfo.GetVersionInfo(Assembly.GetEntryAssembly().Location);

        TWIdentity appId = TWIdentity.Create(DataGroups.Image, new Version(appVer.ProductMajorPart, appVer.ProductMinorPart),
             appVer.CompanyName, appVer.ProductName, appVer.InternalName, new Guid().ToString());

        TwainSession m_twain = new TwainSession(appId);

        m_twain.DataTransferred += Twain_DataTransferred;
        m_twain.SourceDisabled += Twain_SourceDisabled;
        m_twain.TransferReady += Twain_TransferReady;

        //TODO: how to set for Twain 1.x drivers instead of 2.x drivers

        m_twain.Open();

        foreach (var src in m_twain.GetSources())
        {
            tempScanners.Add(src.Name);
        }

Solution

  • Kinda surprised, are you sure that the 1.X drivers are not seen? I believe data sources are still installed to the C:\Windows\twain_32 (or twain_64) folder. The twain 2.X spec does need to work with 1.X, so twaindsm.dll should handle this and NTwain doesn't do anything to modify that behavior that I can see. I don't see any explicit checks in NTwain beyond using the old or new dsm.

    public static ReturnCode DsmEntry(
            TWIdentity origin,
            Message msg,
            TWIdentity data)
        {
            if (Platform.IsWin)
            {
                if (Platform.UseNewDSM) { return NativeMethods.DsmWinNew(origin, IntPtr.Zero, DataGroups.Control, DataArgumentType.Identity, msg, data); }
                else { return NativeMethods.DsmWinOld(origin, IntPtr.Zero, DataGroups.Control, DataArgumentType.Identity, msg, data); }
            }
            else if (Platform.IsLinux)
            {
                return NativeMethods.DsmLinux(origin, IntPtr.Zero, DataGroups.Control, DataArgumentType.Identity, msg, data);
            }
            throw new PlatformNotSupportedException();
        }
    

    Twain 2.3 Spec Download

    Just as a experiment, I would try setting the Platform to use the old DSM and see if the data sources are different. I'm just a bit skeptical that 1.X drivers are not seen.

    EDIT Got a bit interested and started digging into my version of NTwain, interesting enough, I noticed checks just on Identity.ProtocolMajor to see if it is 2.0 or not....which isn't great. The 2.3 spec even warns that is not enough since I believe that is just set by the driver writer to whatever usually. What we need is to see if anything is mapped to the TW_Identity.SupportedGroups for DF_DS2.