Clipboard error

I'm trying to write a Windows editor using c/c++. Everything I've done so far works well. Except the clipboard! I can select and copy text to the clipboard and copy it to Notepad or Wordpad, and copy it back to my editor. When I copy from my editor to the clipboard and try to paste in in another location in the same document, I get a bad handle from the GetCliboardData() function according to the results of GetLastError().
Has anyone else had this difficulty? I'd appreciate any info on this. I'm at a loss for any corections.
I hope you are opening the clipboard before doing getclipboarddata()!!!

this might be the casue , else there is not way it should give error. because it looks the data is getting copied to the clipboard correctly but you are not able to fetch it.
I'm certain the clipboard is open. Here is the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
    case IDM_PASTE:
        if(fOpen == false) //Check for open file
	return 0;
        if (!IsClipboardFormatAvailable(CF_TEXT)){
            MessageBox(hwnd, "Format not available", "Paste Error", MB_OK);
            return 0;
       }
       if(!OpenClipboard(hwnd)){
            FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
    		   FORMAT_MESSAGE_FROM_SYSTEM |
		   FORMAT_MESSAGE_IGNORE_INSERTS, NULL,
                                  GetLastError(),  MAKELANGID(LANG_NEUTRAL, 
                                  SUBLANG_DEFAULT),  (LPTSTR) &lpMsgBuf, 0, NULL);
            MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Paste Error",
		 		MB_OK | MB_ICONINFORMATION );
            LocalFree( lpMsgBuf );
            break;
     }
     hClpData = GetClipboardData(CF_TEXT);  // This is where I get an error
     if (hClpData != NULL){
            MemSize = GlobalSize(hClpData); // Get size of global memory
     if(MemSize == 0){   // Size is 0, and last error is Invalid Handle.
	    FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
  	    	FORMAT_MESSAGE_FROM_SYSTEM |
	    	FORMAT_MESSAGE_IGNORE_INSERTS, NULL,
                                GetLastError(), MAKELANGID(LANG_NEUTRAL,
                               SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL);
        MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Memory Error",
		MB_OK | MB_ICONINFORMATION );
        LocalFree( lpMsgBuf );
        return 0;
    }
    ClpBdDataPtr = (CHAR*)GlobalLock(hClpData);
    if (ClpBdDataPtr == NULL){   // I get the same error here
        FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
    	          FORMAT_MESSAGE_FROM_SYSTEM |
                          FORMAT_MESSAGE_IGNORE_INSERTS,
	         NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL,
    	        SUBLANG_DEFAULT),   (LPTSTR) &lpMsgBuf, 0, NULL);
        MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Paste Error",
	 		MB_OK | MB_ICONINFORMATION );
        LocalFree( lpMsgBuf );
        return 0;
    }
    char *PasteBuff = new char [MemSize + 1]; // create a new 
    memcpy(PasteBuff, ClpBdDataPtr, MemSize); // buffer and copy data.
    GlobalUnlock(hClpData);
        	// Call function to insert the text and repaint the window.
     PasteHex(PasteBuff, nCaretPos);
     delete [] PasteBuff;   //delete the buffer;
     }
     CloseClipboard();
     return 0;


Unless there is a problem with the way Windows uses global memory, I haven't found any problem with this code. I can also copy something to the clipboard, exit my editor, restart it and paste from the cipboard to my editor with no error. It's Wierd!!
Last edited on
hahahaha.. looks really wierd what you said.. let me see the code.. will reply soon.
code looks fine.. not able to find anything wrong.. :(
do you have any success?
Topic archived. No new replies allowed.