0

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()
{
	while(true)
	{
	Sleep(100);
	OpenClipboard(NULL);
	HANDLE foo = GetClipboardData(CF_TEXT);
	CloseClipboard();
	LPVOID lptstr = GlobalLock(foo);
	cout << (char *)lptstr;
	}
	return 0;
}

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

Edited by SpecialForce: n/a

2
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by Ancient Dragon
0

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.

Edited by Ancient Dragon: n/a

0

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... :(

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.