使用GlobalSize函数处理剪贴板数据时出现错误
GlobalSize() Errors with Clipboard Data Hey there! Let's walk through the most common issues that cause errors when using GlobalSize() to get clipboard data sizes—these are the pitfalls I see most often when working with Windows clipboard APIs:
You’re passing an invalid
HGLOBALtoGlobalSize()
Before callingGlobalSize(), you must first validate the handle returned byGetClipboardData(). If you try to useGlobalSize()on aNULLhandle (which happens if the clipboard doesn’t have data for the current format, or you forgot to open the clipboard first), you’ll get an error every time. Here’s a corrected snippet to handle this:// Always open the clipboard first and check success if (!OpenClipboard(hWnd)) { // Handle open failure (e.g., another app holds the lock) return; } UINT currentFormat = EnumClipboardFormats(0); while (currentFormat != 0) { HGLOBAL hClipData = GetClipboardData(currentFormat); if (hClipData != NULL) { // Critical validation step SIZE_T dataSize = GlobalSize(hClipData); if (dataSize == 0) { // Handle GlobalSize failure (unlikely if hClipData is valid, but possible) OutputDebugString(L"GlobalSize failed for clipboard format\n"); } else { // Proceed to lock and copy the data LPVOID pRawData = GlobalLock(hClipData); if (pRawData != NULL) { // Allocate your own memory and copy pRawData here GlobalUnlock(hClipData); } } } currentFormat = EnumClipboardFormats(currentFormat); } CloseClipboard(); // Don't forget to release the clipboard lock!Ignoring delayed-rendered clipboard formats
Some clipboard formats (like display-only formats starting withCF_DSP*) use delayed rendering—the data isn’t actually stored in memory until an app requests it. If you callGlobalSize()on a handle for one of these formats before the owner app has rendered the data, the function might return 0 or an invalid value.
To handle this:- Check if the format is a display format using
GetClipboardFormatInfo() - If you don’t need display formats, skip them entirely
- If you need the data, you can force rendering by trying to lock the handle with
GlobalLock()—if it succeeds, the data is ready; if not, you may need to retry later.
- Check if the format is a display format using
Using the wrong data type for the size
GlobalSize()returns aSIZE_Tvalue, which is 4 bytes on 32-bit systems and 8 bytes on 64-bit systems. If you store this value in a smaller type (likeint), you’ll get overflow or truncated values, leading to incorrect memory allocation and errors. Always useSIZE_Tto store the result ofGlobalSize().Mishandling special clipboard formats
Formats likeCF_HDROP(file drops) or custom formats don’t store raw bytes directly—theirHGLOBALpoints to a structured data type. For example,CF_HDROPuses aDROPFILESstructure followed by null-terminated file paths.GlobalSize()returns the total size of this structure plus the file data, but if you treat it as raw byte data, you might misinterpret the size and run into errors. Make sure you parse structured formats according to their specifications.
If you can share a small snippet of your code where the error occurs (especially around the GlobalSize() call), I can help you pinpoint the exact problem!
内容的提问来源于stack exchange,提问作者lauqerm

