Ronnie
Ronnie

Reputation: 69

Saving image to clipboard (from Javascript to C++)

I am currently working on saving an image to clipboard. The base64 data comes from the browser (base64) passed through WebSocket and handled by a C++ app.

My issue is on the C++ side converting the base64 code to byte array to HBITMAP to BITMAP then, saving it to the clipboard.

I used CryptStringToBinaryA to convert from base64 (len: 95000) to byte array (len: 70000).

bRet = CryptStringToBinaryA(
        base64,
        strlen(base64),
        CRYPT_STRING_BASE64,
        &bytes[0],
        &length,
        NULL,
        NULL);

Then, from byte array, I used CreateDIBSection to get HBITMAP.

    tagBITMAPFILEHEADER bmfh = *(tagBITMAPFILEHEADER*)bytes;
    tagBITMAPINFOHEADER bmih = *(tagBITMAPINFOHEADER*)(bytes + sizeof(tagBITMAPFILEHEADER));
    RGBQUAD rgb = *(RGBQUAD*)(bytes + sizeof(tagBITMAPFILEHEADER) + sizeof(tagBITMAPINFOHEADER));

    BITMAPINFO bi;
    bi.bmiColors[0] = rgb;
    bi.bmiHeader = bmih;

    byte* pPixels = (bytes+bmfh.bfOffBits);

    void* ppvBits;
    HDC hdc = GetDC(NULL);
    HBITMAP hBitmap = CreateDIBSection(hdc, &bi, DIB_RGB_COLORS, &ppvBits, NULL, 0);
    SetDIBits(NULL, hBitmap, 0, 500, pPixels, &bi, DIB_RGB_COLORS);

This is causing the issue because ppvBits returns 0.

And when I try to do GetObject, it returns 0.

GetObject(hBitmap, sizeof(BITMAP), NULL)

I also tried to do GetLastError after GetObject. And I am getting this error.

Cannot create a file when that file already exists.

Does anyone have any idea what could be the issue?

Upvotes: 0

Views: 301

Answers (0)

Related Questions