Click to See Complete Forum and Search --> : Copying file contents directly to clipboard.


mrcr
March 25th, 2006, 09:13 PM
Yes another question on the same project. Can you tell I'm new to programming?

Seems that I have everything working now except one thing.. getting the actualy file contents onto the clipboard. Like I said in the other thread, I can copy the file contents and parse it to another file. As in I can get it to search for the data I need and then copy it and paste it into another file. And I am able to now send data correctly to the focused window.

Now I need to take that text I'm copying from the file and actually copy it directly to the clipboard so I can paste it to the active window, instead of copying it to a string and outputting it to another file.

I looked at some of the clipboard info and it's confusing me to no end. I'm not sure if I have this right, but it looks like I have to:
- Get the clipboard id
- open the clipboard
- empty the clipboard
- mem allocation?
- copy string to clipboard
- close clipboard

I got this by searching through MSDN, but now I'm just more confused :/

I'm pretty sure I can get the opening, emptying, and closing part right, but I am not sure about getting the clipboard ID and what the memory allocation is for. And of course actually copying the string i get from the file to the clipboard.

golanshahar
March 26th, 2006, 02:37 AM
Read/Write text on clipboard (http://www.codeguru.com/cpp/w-p/clipboard/article.php/c3011/)
Clipboard (http://www.codeguru.com/cpp/w-p/clipboard/)



Cheers

mrcr
March 29th, 2006, 05:05 PM
Okay, so I figured out the clipboard now, except I can't get it to copy the string to the clipboard. The only thing I can copy to the clipboard is a const char. And I can't convert the string to a const char.

Deleted old topic from here since it's no longer an issue, but here is the code I have now. I noted where I'm getting an error in the code.


int readLog()
{
string c;
char * fName;
ifstream file;

cout << "Enter the name of log to read from: ";
cin >> fName;

file.open (fName);

if (file.is_open())
{
while (! file.eof() )
{
getline (file,c);
cout << c;
}
file.close();
}
int ok = OpenClipboard(NULL);

if (!ok) return -1;

HGLOBAL clipbuffer;
char * buffer;
EmptyClipboard();
clipbuffer = GlobalAlloc(GMEM_MOVEABLE, strlen(c)); // - ERROR
buffer = (char*)GlobalLock(clipbuffer);
strcpy(buffer, LPCSTR(c)); // - ERROR
GlobalUnlock(clipbuffer);
SetClipboardData(CF_TEXT,clipbuffer);
CloseClipboard();
}