Adding text to the Windows Clipboard (API)..

This is a discussion on Adding text to the Windows Clipboard (API).. within the Windows Programming forums, part of the Platform Specific Boards category; I've tried several things.. but i keep getting errors in the SetClipboardData() line. The problem I have is when trying ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    18

    Adding text to the Windows Clipboard (API)..

    I've tried several things.. but i keep getting errors in the SetClipboardData() line.
    The problem I have is when trying to add Strings to it.

    SetClipboardData(CF_Text,"Hi");

    does not work obviously
    I suppose I have to cast the string or something, or maybe do something with pointers.. Please help..
    ~void

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789

    Re: Adding text to the Windows Clipboard (API)..

    Originally posted by void
    I've tried several things.. but i keep getting errors in the SetClipboardData() line.
    The problem I have is when trying to add Strings to it.

    SetClipboardData(CF_Text,"Hi");

    does not work obviously
    I suppose I have to cast the string or something, or maybe do something with pointers.. Please help..
    SetClipboardData needs the memory set to exist on the heap (and in my experience - often best to use the default process heap as opposed to one of the runtime heaps used with new or malloc)

    Code:
    #include <windows.h>
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    int main()
    {
    	string strBuff = "Hello World";
    	
    	if(!OpenClipboard(0)){
    		cout << "Error opening clipboard";
    		return 1;
    	}
    
    	LPVOID lpv = 
    		HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,
    		strBuff.length()+1);
    
    	if(!lpv){
    		cout << "Error getting memory";
    		return 1;
    	}
    
    	CopyMemory(lpv,strBuff.c_str(),strBuff.length());
    	EmptyClipboard();
    
    	if(!SetClipboardData(CF_TEXT,lpv)){
    		cout << "Error Setting data";
    		return 1;
    	}
    
    	CloseClipboard();
    
    	HeapFree(GetProcessHeap(),0,lpv);
    
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. adding text to top
    By kippwinger in forum C++ Programming
    Replies: 5
    Last Post: 07-26-2005, 02:25 AM
  2. Programming Windows API in C++
    By JoshR in forum C++ Programming
    Replies: 5
    Last Post: 03-29-2005, 05:40 AM
  3. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21