Hello, I am trying to create a program to read text from clipboard. It works great, but when I copy a file the program crashes. I am trying to make it to ignore any files and only output text. My code so far is the following:

#include "stdafx.h"
#include <iostream>
#include <windows.h>
using namespace std;

int main()
	HANDLE foo = GetClipboardData(CF_TEXT);
	LPVOID lptstr = GlobalLock(foo);
	cout << (char *)lptstr;
	return 0;

P.S. Sorry for my bad english...

Your program worked ok for me using vc++ 2010 express on Windows 7. But I don't understand why it contains a while(1) loop? One possible reason that it crashes on you is because the code never calls GlobalUnlock().

>>when I copy a file the program crashes

Copy a from to where? I don't understand that statement.

I put the while for testing purposes only, so I won't have to reopen program to read clipboard again.

About the file, I mean when I try to copy a file to clipboard, then program crashes. Probably because my program thinks the file is text and tries to read it this way. So, to put it simple, the solution is to make the program find if clipboard data is text and only then read them.

Sorry for confusing you... :(

You could make your program a little smarter and if the clipboard contains binary data (e.g. a file) read it that way. You really shouldn't put an entire file into the clipboard anyway.