0

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?

Ronnie
  • 69
  • 1
  • 7
  • I don't know the API in use, but from the error, I conclude that some of the functions involved tries to create some file somewhere, probably using `open` or `fopen` with a file mode that does not allow overwriting existing files. Try to find out where that file is created and delete it explicitly in code before. If don't manage to do so: Function signatures (initial capital letters) hint to Windows API, so I would try to clean all temporary folders (current user and system) and retry... – Aconcagua Oct 20 '17 at 09:23
  • I tried to delete the temp folders/files but it is still throwing the same error. – Ronnie Oct 20 '17 at 09:57

0 Answers0