Originally posted by void
I've tried several things.. but i keep getting errors in the SetClipboardData() line.
The problem I have is when trying to add Strings to it.
SetClipboardData(CF_Text,"Hi");
does not work obviously
I suppose I have to cast the string or something, or maybe do something with pointers.. Please help..
SetClipboardData needs the memory set to exist on the heap (and in my experience - often best to use the default process heap as opposed to one of the runtime heaps used with new or malloc)
Code:
#include <windows.h>
#include <iostream>
#include <string>
using namespace std;
int main()
{
string strBuff = "Hello World";
if(!OpenClipboard(0)){
cout << "Error opening clipboard";
return 1;
}
LPVOID lpv =
HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,
strBuff.length()+1);
if(!lpv){
cout << "Error getting memory";
return 1;
}
CopyMemory(lpv,strBuff.c_str(),strBuff.length());
EmptyClipboard();
if(!SetClipboardData(CF_TEXT,lpv)){
cout << "Error Setting data";
return 1;
}
CloseClipboard();
HeapFree(GetProcessHeap(),0,lpv);
return 0;
}