0 out of 1 rated this helpful - Rate this topic

GetClipboardFormatName function

Applies to: desktop apps only

Retrieves from the clipboard the name of the specified registered format. The function copies the name to the specified buffer.

Syntax

int WINAPI GetClipboardFormatName(
  __in   UINT format,
  __out  LPTSTR lpszFormatName,
  __in   int cchMaxCount
);

Parameters

format [in]

Type: UINT

The type of format to be retrieved. This parameter must not specify any of the predefined clipboard formats.

lpszFormatName [out]

Type: LPTSTR

The buffer that is to receive the format name.

cchMaxCount [in]

Type: int

The maximum length, in characters, of the string to be copied to the buffer. If the name exceeds this limit, it is truncated.

Return value

Type: int

If the function succeeds, the return value is the length, in characters, of the string copied to the buffer.

If the function fails, the return value is zero, indicating that the requested format does not exist or is predefined. To get extended error information, call GetLastError.

Remarks

Security Considerations

Using this function incorrectly might compromise the security of your program. For example, miscalculating the proper size of the lpszFormatName buffer, especially when the application is used in both ANSI and Unicode versions, can cause a buffer overflow. Also, note that the string is truncated if it is longer than the cchMaxCount parameter, which can lead to loss of information.

Examples

For an example, see Example of a Clipboard Viewer.

Requirements

Minimum supported client

Windows 2000 Professional

Minimum supported server

Windows 2000 Server

Header

Winuser.h (include Windows.h)

Library

User32.lib

DLL

User32.dll

Unicode and ANSI names

GetClipboardFormatNameW (Unicode) and GetClipboardFormatNameA (ANSI)

See also

Reference
EnumClipboardFormats
RegisterClipboardFormat
Conceptual
Clipboard

 

 

Send comments about this topic to Microsoft

Build date: 3/6/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
lpszFormatName is NUL terminated
The API documentation fails to mention that the data dropped into the lpszFormatName buffer is always NUL (zero) terminated. The length returned is the number of characters not including the terminating NUL. Thus if the returned length is one less than the size of your lpszFormatName (in characters) then it's possible the format name was truncated.

This document also fails to mention what the maximum possible size is for a name that could get dropped into the lpszFormatName buffer Microsoft's example code uses an 80 character buffer. The RegisterClipboardFormat() API documentation is also silent on this other than to note that it's a case-insensitive string. A scan of the registered formats from 0xC000 to 0xFFFF finds the names range from 3 to 113 characters on my system at this instant. Testing finds that RegisterClipboardFormat() returns error 87 ERROR_INVALID_PARAMETER if lpszFormatName is 256 characters or longer. Attempts to register a zero length name result in error 123 ERROR_INVALID_NAME. In other words, the lpszFormatName can be from 1 to 255 characters plus the terminating NUL.  Thus using a 256 character buffer will work for most, if not all, systems.
C# syntax
[DllImport("user32.dll", CharSet=CharSet.Auto, SetLastError=true)]
public static extern int GetClipboardFormatName(int format, StringBuilder lpString, int cchMax);
vb.net syntax
<DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
Public Shared Function GetClipboardFormatName(ByVal format As Integer, ByVal lpString As StringBuilder, ByVal cchMax As Integer) As Integer
End Function
GetClipboardFormatName as inverse of RegisterWindowMessage
It seems that GetClipboardFormatName can be used also to get the the message string given the message identifier, returned by RegisterWindowMessage - i.e. a kind of inverse of RegisterWindowMessage.

RegisterWindowMessage:
http://msdn2.microsoft.com/en-us/library/ms644947.aspx

See RegisterWindowMessage community content for details, or follow this newsgroup link:

http://groups.google.it/group/microsoft.public.vc.mfc/browse_thread/thread/f83f7c12c80e4ada/460bc4c43a844a37

Giovanni