Selecting the Default TWAIN Source

Applications that provide document scanning SHOULD provide a means of choosing the scanner. The mechanism is to send a command to the Data Source Manager “DSM” which then remembers the chosen default.

This provides an easy way of picking a default for any application. Just use an app that allows you to “Select Source …”, pick a source, and then use any application you like with that source.

TWAIN DSM Internals

If you want to choose a source yourself, under program control, here is how it all works …

In a Windows environment, the DSM is a Dynamic Link Library (TWAINDSM.DLL) that resides within the WINDOWS tree:
twaindsm

The DSM was actually written by the TWAIN Working Group and is provided by Microsoft during a Windows install.

The TWAIN DSM source code is available on line.

If you look through the source code, you’ll see that the default source is set by storing a value in the registry. A simplified version of the code is:
DSM_SetDefaultDS

As you can see, the hive used is HKEY_CURRENT_USER. The registry path used varies for 64 bit and 32 bit OSes. It is:

#if TWNDSM_OS_64BIT
#define TWNDSM_DS_REG_LOC "Software\\Microsoft\\Windows NT\\CurrentVersion\\TWAIN64"
#else
#define TWNDSM_DS_REG_LOC "Software\\Microsoft\\Windows NT\\CurrentVersion\\TWAIN"
#endif

The path to the “Source”, the actual scanner driver, is typically “C:\WINDOWS\twain_32”:
windows-twain_32

The reason is also given in the source code:

#ifndef kTWAIN_DS_DIR
#if TWNDSM_OS_64BIT
#define kTWAIN_DS_DIR "twain_64"
#else
#define kTWAIN_DS_DIR "twain_32"
#endif
#endif

// Work out the full path to our drivers (if needed)…
#if (TWNDSM_CMP == TWNDSM_CMP_VISUALCPP)
(void)::GetWindowsDirectory(szDsm,sizeof(szDsm));
SSTRCAT(szDsm,sizeof(szDsm),”\\”);
SSTRCAT(szDsm,sizeof(szDsm),kTWAIN_DS_DIR);
#elif

The registry setting looks like:
registry-setting-for-default-twain-source

Setting the Registry

If you are writing a proper TWAIN application, you should choose the default source by calling functions within the DSM. However, given what the DSM currently does, an easy way to set the default document scanning device programatically is to add or update the registry setting directly. This is very straight forward in most languages (eg VB, VC++, VBA, .Net, VBScript).

A simple example is setting the default TWAIN Source using VBScript.

You could also supply or pick a registry settings file to “run”. That would cause RegEdit to load and do the update for you.

As always, exercise care when tinkering with the registry.

Leave a Reply

Your email address will not be published. Required fields are marked *