Using TwainSave in a program

Top  Previous  Next

Since TwainSave is a command-line utility, a programmer can directly run this utility from their own programs.

 

The Windows® API functions CreateProcess and ShellExecute can be used to start executable programs such as TwainSave.  The older WinExec function can also be used for this purpose.

 

For example, the following executes TwainSave and saves the acquired image to a file named test.tif as a TIFF/LZW file:

twainsave -filename test.tif -filetype tif7

 

To execute this programatically, the following C/C++ console-program shows how to do this:

 

#include <windows.h>

#include <tchar.h>

 

int main( )

{

   TCHAR pOptions[ ] =  _T("-filename test.tif -filetype tif7");

 

   // Start TwainSave with the options above, and hide the command window when starting up.

   ShellExecute( NULL, _T("open"), _T("twainsave.exe"), pOptions, _T(""), SW_HIDE );

}

 

If your application needs to wait until TwainSave exits, you should call the more complex CreateProcess( ) function, and then call WaitForSingleObject( ) to wait for TwainSave to complete processing:

 

#include <windows.h>

#include <tchar.h>

 

int main( )

{

   TCHAR pOptions[ ] = _T(" -filename test.tif -filetype tif7");

   STARTUPINFO si;

   PROCESS_INFORMATION pi;

 

   ZeroMemory(&si, sizeof(STARTUPINFO));

   si.cb = sizeof(STARTUPINFO);

 

   // start TwainSave with the options " -filename test.tif -filetype tif7" -- assume that twainsave.exe is in the current directory.

   if ( CreateProcess(_T("twainsave.exe"), pOptions, NULL, NULL, TRUE, NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &pi ) )

        // wait for this process to terminate

        WaitForSingleObject( pi.hProcess, INFINITE );

}

 

Note that the examples above are console programs, however there is nothing stopping a GUI application from utilizing TwainSave.

 

For other languages, please consult your programming manual as to which functions are used to run executable programs.  Most, if not all Windows based languages have this facility.  For example, for Visual Basic, the Shell function is used to start executable programs.