I don't know why this code isn't working properly:
#define UNICODE
#include <iostream>
#include <sstream>
#include <windows.h>
void main(void)
{
wchar_t* strData = L"CreateWindowExA";
MessageBox(NULL, strData, L"Warning", MB_OK);
if (OpenClipboard(0)) {
EmptyClipboard();
HGLOBAL hClipboardData;
hClipboardData = GlobalAlloc(GMEM_DDESHARE,
wcslen(strData) + 1);
char* pchData;
pchData = (char*)GlobalLock(hClipboardData);
strcpy(pchData, LPCSTR(strData));
GlobalUnlock(hClipboardData);
SetClipboardData(CF_TEXT, hClipboardData);
CloseClipboard();
}
MessageBox(NULL, L"Copied to Clipboard", L"Title", MB_OK);
}
GlobalAlloc
allocates the number of bytes, not the number of characters (which in your case are 2 bytes wide). You need to figure out how many bytes to allocate. Second, you don't convert wide strings to ANSI strings and vice-versa by merely casting. That(LPCSTR)
cast is not going to work. If you're doing anything like that in some other parts of your code you're not showing us, then stop doing it as your program will be doomed for failure. – UpshotSetClipboardData()
? DId you see my comment below and link to API documentation? Try skipping EmptyClipboard() if you use a NULL window handle. – EnvyGMEM_DDESHARE
[...]". Besides, there's literally zero error checking in your code. Why do we have to guess, which API call fails?GlobalLock
should be called on movable memory only (while you request fixed memory). It is used to convert a handle into a pointer, but you pass it a valid memory pointer already. Not good. – Poff