Copy To Clipboard

I needed a program to copy its command-line parameters to the MS Windows clipboard. Since this is a very simple program, it serves as a good comparison of various compilers.

The VB 6.0 exe (16 kb) is about the same size as the Delphi 2.0 exe (14 kb). However, the VB distribution package is much larger - 1.46 Mb vs 14 kb. When used via Windows Explorer, both programs are fairly quick.

These programs were designed to let the user copy a full path name to the clipboard using Windows Explorer. Using View / Options... / File Types, edit Folder and add a Copy Path to Clipboard menu selection. The command line should be C:\Utils\CopyToClip.exe "%1" (as appropriate). Notice that Delphi requires the double quotes around the %1; for VB, the double quotes are optional.

To use this, in Windows Explorer, right click on any directory and select Copy Path to Clipboard. Then you can paste the path from the clipboard into most text editors and text fields on forms.

There are times that you will want both the path and a filename. There are 2 possibilities

Unfortunately, the SendTo method produces only a DOS path - typical 8.3 with tildes and no spaces. When the registry is used, long names are placed in the clipboard. (Great OS :)

Visual Basic | Delphi | C++ Builder

Visual Basic 6.0

Complete contents of CopyToClipboard.bas Notice that Command() returns the command-line parameters in a single string.

CopyToClipboard.exe 16 Kb ----- Distribution 1.46 Mb, Support 2.84 Mb
(If you want this, compile it yourself. Its just too large to distribute.)


Delphi

Complete contents of CopyToClip.dpr CmdLine returns the name of the executable as well as the command-line parameters. ParamStr (1) returns the first command-line parameter. Therefore, in the registry, because some paths contain spaces, the %1 must be placed in double quotes.

Well, this file is 165 K. I decided to see what would happen if I made Windows API calls instead of using the clipbrd unit. The program is about 30 lines of code instead of just 1 ... but the exe is only 14 K. For a discussion of what I tried and the results, see here.
File Size Version
CopyToClip.exe 165 Kb 2.0 No additional files needed for distribution
CopyToClip.exe 291 Kb 5.0 Converts long filenames to 8.3 with tildes and no spaces
CopyToClipboard.exe 14 Kb 2.0 No additional files needed for distribution
CopyToClipboard5.exe 17 Kb 5.0 Converts long filenames to 8.3 with tildes and no spaces
CopyToClipboard.dpr Source code

I don't get it! Delphi 2.0 works perfectly. You right click a file or directory, select copy to clipboard, and you get the expected long file name ... spaces and all. But when you compile the exact same code, the new improved Delphi 5.0 executable saves the worthless 8.3 filenames with tildes and no spaces. I have verified that Delphi 5.0 is not making this change. Therefore, Windows 95 must be assuming the Delphi is producing 16-bit Windows 3.1 code. As explained above, you can use "%L" to force Windows to use the long file and directory names.

When "%1" (percent one) is used, my NT 4.0 and Windows 95 machines at work (one is running IE 5 and the other IE 4.72) do not have this problem with the Delphi 5.0 executable - the file names are fine.


C++ Builder

I tried 2 methods to implement this - VCL (Visual Component Library written in Object Pascal) and direct Windows API calls.

VCL Console App

The Windows VCL Clipboard routines are defined via Clipbrd.hpp. Upon inspection, the hpp file indicates that the source file is Clipbrd.pas and that TClipboard is a Delphi class.

Some VCL hints from the help file.

// TClipboard::SetTextBuf
// void __fastcall SetTextBuf(char * Buffer);
// Clipboard()->Assign(SpeedButton1->Glyph);

Create a new console application and click the Include VCL box. Enter the following code.

The VCL executable is 291 Kb. However, it is very slow and flashes a command window.
In the registry, be sure to place double quotes around the %1.


Windows API App

Create a Windows console application. Copy CopyToClipboard.cpp and compile. The executable is 54 Kb.

I was never able to get the try...finally blocks to work. It is very confusing. Delphi seems to do this better.

In the registry, you will not need double quotes around the %1.


Author: Robert Clemenzi - clemenzi@cpcug.org
URL: http:// cpcug.org / user / clemenzi / technical / Languages / CopyToClipboard.htm