Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WIN32 IsClipboardFormatAvailable function does not set last error code

Tags:

c

winapi

In documentation

If the clipboard format is not available, the return value is zero. To get extended error information, call GetLastError. IsClipboardFormatAvailable function (winuser.h)

but the output of this code is 0:

#include <Windows.h>
#include <cstdio>
#include <winerror.h>
int main()
{
    //I copied an image to the clipboard before running the program
    if(!IsClipboardFormatAvailable(CF_UNICODETEXT))
    {

        printf("%lu", GetLastError());
    }
}

Why didn't the IsClipboardFormatAvailable function set the last error?

like image 381
kaan Avatar asked Dec 22 '25 02:12

kaan


1 Answers

I've seen this pattern before. Typically IsClipboardFormatAvailable() is not a function that can fail; and there's not expected to be anything useful to report because the last thing in the clipboard just doesn't match any known format.

If you're really in a situation where IsClipboardFormatAvailable() can fail and need to check and do something about the difference, this fragment is correct:

   SetLastError(0);
   if(!IsClipboardFormatAvailable(CF_UNICODETEXT))
   {
       if (GetLastError() == 0)
       {
           printf("Clipboard doesn't contain text.");
       } else {
           printf("IsClipboardFormatAvailable: %lu", GetLastError());
       }
   }

You described a case of image in the clipboard in the question. The function didn't fail.

like image 193
Joshua Avatar answered Dec 24 '25 17:12

Joshua



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!