Visual Basic Concepts

Creating a Custom Data Format

If the formats supplied in Visual Basic are insufficient for some specific purpose, you can create a custom data format for use in an OLE drag-and-drop operation. For example, a custom data format is useful if your application defines a unique data format that you need to drag between two instances of your application, or just within the application itself.

To create a custom data format, you have to call the Windows API RegisterClipboardFormat function. For example:

Private Declare Function RegisterClipboardFormat Lib _
      "user32.dll" Alias "RegisterClipboardFormatA" _
      (ByVal lpszFormat$) As Integer
Dim MyFormat As Integer

Once defined, you can use your custom format as you would any other DataObject object data format. For example:

Dim a() As Byte
a = Data.GetData(MyFormat)

To use this functionality, you have to place data into and retrieve data from the DataObject object as a Byte array. You can then assign your custom data format to a string variable because it is automatically converted.

Caution   Retrieving your custom data format with the GetData method may yield unpredictable results.

Because Visual Basic doesn’t understand your custom data format (because you defined it), it doesn’t have a way to determine the size of the data. Visual Basic can determine the memory size of the Byte array because it has been allocated by Windows, but the operating system usually assigns more memory than is needed.

Therefore, when you retrieve a custom data format, you get back a Byte array containing at least, and possibly more than, the number of bytes that the source actually placed into the DataObject object. You must then correctly interpret your custom data format when it is retrieved from the DataObject object. For example, in a simple string, you have to search for the NULL character and then truncate the string to that length.