Untapped resources in Windows

By: Lubomir Rosenstein

Abstract: Scanning and creating shortcuts using COM and Delphi. By Lubomir Rosenstein

Writing programs for Windows can be like playing with Lego blocks. You have an abundance of bricks and building blocks. Even a very intricate design can be implemented simply by arranging them, experimenting, and having fun.

But there is no need to reinvent the wheel. In this pursuit the curiosity and energy of an explorer can be quite handy. COM objects in Windows offer plenty of built-in, easy-to-use solutions. Often they are hidden: undocumented and unexplored. You must discover for yourself when, where, and what these Lego blocks can do for you.

THRIFTY SCANNING

You can buy or build a special component to add an image-scanning ability to your application. But you don't need to do this: You already have everything you need. Imaging for Windows ships as part of the operating system. The setup program for Imaging installs the Image Scan control (OCX) and the 32-bit TWAIN DLLs, and makes the necessary entries in the Windows registry. So you are ready to work with TWAIN-compliant scanners. Once you have installed Windows Imaging, importing its capabilities into your Delphi application is a straightforward task. The most efficient way is by importing the type library and generating a component wrapper as shown here:

If you need to scan an image and to save it to a file, the Image Scan Control is all you need. But you could also import the Kodak Image Edit Control, which allows you to present and edit the scanned image on your application's form. And the Thumbnail control, so you can command the full functionality of Imaging from your Delphi application. When distributing your image-enabled apps, all you need is to make sure that your users set up Imaging for Windows before installing your applications.

Here is how easy it is to access the Imaging controls' interfaces using component wrapping:

{ Imgscan: TImgScan} 
If Imgscan.ScannerAvailable then //checks if there is a scanner 
  try  
    Imgscan.OpenScanner ; 
    ImgScan.ScanTo := 2;    //file  
    Imgscan.Image:=stFName; //the scanned image will be saved 
                            //to this destination   
    Imgscan.StartScan ; 
    Application.ProcessMessages; 
  finally  
    Imgscan.CloseScanner ; 
  end 
  else 
exit;

More information about the properties, methods, and events of the Imaging controls is available in the imgocxd.hlp help file and on the Kodak Imaging site.

AUTOMATING WINDOWS SCRIPTING

Automating the Microsoft Windows Script Host may sound paradoxical, but in Delphi it can save much time in accomplishing such tasks as creating a shortcut, mapping a network drive, or accessing network information. If you like extravagance (or you don't like the Windows API), you can even use it to execute another program from within your application. Through its object model, the Windows Script Host gives easy access to the network and the Windows shell, something like a back door for the curious developer. This tool is hidden in the OS directory and the documentation (and updated versions) can be downloaded from Microsoft's Web site.

In Delphi you can choose early or late binding. If you prefer to import the library, search for Winscript.exe (first it should be added to the Import Type Library list) and you will have two new components. I find it even easier to use the late binding in this case.

Using the Shell object, you can easily create a shortcut or an Internet shortcut, change its icon, or define its target. Accessing the Shell SpecialFolders collection, the shortcut can be placed onto the desktop, in the Start Menu, or in the "My Documents" folder. All of this can be achieved with a few simple lines of code.

Here's how you create a shortcut and place it on the desktop:

 procedure CreateShortcut( sName,sPath:string);  
 var  
   objShortcut, objShell :variant; 
   strDesk:string; 
 begin  
   objShell:= CreateOleObject('Wscript.Shell'); 
   strDesk:= objShell.SpecialFolders.Item('Desktop'); 
   objShortcut:= objShell.CreateShortcut(strDesk +''+ sName +'.lnk'); 
   objShortcut.Targetpath:= sPath; 
   objShortcut.Save; 
   objShell:=UnAssigned; 
   objShortcut:=UnAssigned; 
 end;

Use this same code, but a ".url" extension, and you can create an Internet shortcut. If you use a "mailto:"' instead of an "http://" target, you will have a shortcut to a new e-mail message for a constant receiver. You can also add "&subject=" and "&body=" in the target string and create an e-mail template for Outlook.

The Run method of the Shell object executes any given executable. For instance, Shell.Run('Calc') is enough to execute the Calculator. Shell also has a Popup method which shows a message on the screen.

The second important Windows Scripting Host object is Network. This code uses it to obtain network information:

 function ShowNetwork:string;  
 var  
   ObjNetwork :variant; 
 begin  
   ObjNetwork:= CreateOleObject('Wscript.Network'); 
   Result:= 'User Domain : ' + ObjNetwork.UserDomain + #10 + #13 + 'User Name : ' + 
   ObjNetwork.UserName + #10 +#13 + 'ComputerName : ' + 
   ObjNetwork.ComputerName ; 
   ObjNetwork:=UnAssigned; 
end;

Scanning and creating shortcuts are just two examples of how to use Windows untapped resources. Do some exploring of your own -- you will can discover many other ways of completing your tasks simply by playing with ready-to-use COM building blocks.

Lubomir Rosenstein is a Delphi developer specializing in database application programming. He has written many articles on computer programming on the Internet. He works for NTR Co. in Tel Aviv, Israel, and can be reached at lubo@ntr.co.il

Server Response from: SC4