Ok, I'm a relatively novice programmer, but I designed an app for work that lets you enter data in a much more efficient way, then it needs to paste this data into the very curmudgeonly interface we use for work.
I designed this app a long time ago while at a different company, and I used VC++ 6. I'm still using VC++ 6, but in a vbox with windows XP.

The code is being called as part of a bigger function that determines what exactly needs to be pasted into the work app spreadsheet. I have to call each item separately because the work app does not take copied tabs the way Excel would or whatever, it just ignores them. And it also pops up boxes and such that need to be handled in other ways.

The result works just fine under Windows XP, but when I do it under my regular Win7 box or on the Win7 boxes at work, it does not work properly.

(forgive the code for being a bit wonky, but I've spent so much time trying to test different things to get this to work)
This sets the data up, Text being the data to be pasted:
Code:
while (1) {
	if (OpenClipboard() == FALSE) {
		::Sleep(1);
		continue;
	}
	HGLOBAL clipbuffer;
	char * buffer;
	EmptyClipboard();
	clipbuffer = GlobalAlloc(GMEM_DDESHARE, Text.GetLength()+1);
	buffer = (char*)GlobalLock(clipbuffer);
	strcpy(buffer, LPCSTR(Text));
	GlobalUnlock(clipbuffer);
	SetClipboardData(CF_TEXT,clipbuffer);
	CloseClipboard();
	break;
}
No infinite loops yet, again was just testing if it was OpenClipboard failing because it was being called again too quickly or something (and it did happen occasionally).

Then testing that the clipboard data is correct (this is always true), and pasting with keybd_event. SendInput is not available with VC++ 6.

Code:
HGLOBAL hglb;
LPTSTR lptstr;
LPTSTR buf = Text.GetBuffer(0);

while (OpenClipboard() == FALSE)
	::Sleep(1);
hglb = GetClipboardData(CF_TEXT);
lptstr = (LPTSTR)GlobalLock(hglb);

if (strcmp(lptstr,buf) != 0)
	AfxMessageBox("$");

Text.ReleaseBuffer();
GlobalUnlock(hglb);
CloseClipboard();

//::Sleep(150);
keybd_event(VK_CONTROL, 0x0, KEYEVENTF_EXTENDEDKEY | 0, 0);
keybd_event(0x56, 0x56, 0, 0); //V
keybd_event(0x56, 0x56, KEYEVENTF_KEYUP, 0);
keybd_event(VK_CONTROL, 0x0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
//::Sleep(150);
If the ::Sleeps are commented out, it will paste the wrong data almost every time under win7. It seems like it pretty much uses the last thing to be copied in the function and pastes it over and over (but not *always*). If I uncomment them, it will work as expected, but it obviously takes longer. With ::Sleep(50), it works about 80-90% of the time. Obviously I can't have the wrong data being pasted, and I'd prefer not to have to wait like this--and perhaps on a slower system or a system that is doing other things it may take longer and paste the wrong thing. It works fine on XP even without the sleeps.

Did keybd_event become a separate thread or something? Am I doing something wrong with the buffers? (I've tried using CSharedFile too with GMEM_MOVEABLE | GMEM_SHARE | GMEM_ZEROINIT settings, exact same thing.) Why does it work fine under XP but not under 7?