1

I want to put a text to clipboard, my code :

#include <windows.h>


void copier_dans_presse_papier(const char *s)
{
    if(OpenClipboard(NULL))
    {
        HGLOBAL h;
        EmptyClipboard();
        h = GlobalAlloc(GHND, strlen(s) + 1);
        if(h)
        {
            char *p = GlobalLock(h);   /* line 13 */
            if(p)
            {
                strcpy(p, s);
                GlobalUnlock(h);
                SetClipboardData(CF_TEXT, h);
            }
            GlobalFree(h);
        }
        CloseClipboard();
    }
}

int main(void)
{
    copier_dans_presse_papier("Hello World !");
    return 0;
}

I got this error in compile : [Error] invalid conversion from 'LPVOID {aka void*}' to 'char*' [-fpermissive]

(line 13)

How fix this ?

El virtuos
  • 17
  • 5

1 Answers1

0

GlobalLock doesn't return a char*, instead it returns a LPVOID (which is just a typedef for a void*). Just cast the result like this:

char *p = (char*) GlobalLock(h);

And it works, at least it did for me. You probably made the code using a tutorial for C, where that cast would not have been necessary.

Blaze
  • 16,736
  • 2
  • 25
  • 44