EQuestionAnswers.com Computer/Electronics Questions and Answers
C, C++, VC++, COM/DCOM, DLL and more
#Login #Sign up  Facebook Twitter TGoogle+

#Previous question #Index of VC++/WIN32/MFC Questions #Next question

Windows clipboard is shared place for exchanging data between applications. An application can read data from clipboard as well as it can set the data to it. Data can be in the form of plain text, rich formatted text, HTML or any other OLE object format. A good example of clipboard data exhange is opening notepad and office word and type some text, select it and copy with CTRL+C and press CTRL+V on office word. We see the same text appeared in word. Now we can type some other text in word and select it and do the same to copy that text to notepad. The data is copied to windows clipboard in one application and again readback from clipboard to other application. CRTL+C and CTRL+V are shortcut keys however applications use Windows Clipboard functions to do these backend process.

We are writing a small demonstration application to show the utility of basic clipboard functions. Clipboard can supports many type of data formats. However to make our demo application simple we will mostly deal with plain text format. Before we start we are listing all clipboard functions and discussing their utilities.

OpenClipboard function

Opens the clipboard for reading or modification. This prevents other applications from modifying the clipboard content.


BOOL WINAPI OpenClipboard(HWND hWndNewOwner);


  • hWndNewOwner - A handle to the window to be associated with the open clipboard. If this parameter is NULL, the open clipboard is associated with the current task.

Return value

  • If the function succeeds, the return value is nonzero.
  • If the function fails, the return value is zero. To get extended error information, call GetLastError.


OpenClipboard fails if another window has the clipboard open. An application should call the CloseClipboard function after every successful call to OpenClipboard. The window identified by the hWndNewOwner parameter does not become the clipboard owner unless the EmptyClipboard function is called. If an application calls OpenClipboard with hwnd set to NULL, EmptyClipboard sets the clipboard owner to NULL; this causes SetClipboardData to fail.


The GetClipboardData function retrieves data from the clipboard in a specified format. The clipboard must have been opened previously.

HANDLE GetClipboardData(UINT uFormat);


    uFormat - Specifies a clipboard format. For a description of the standard clipboard formats, see Standard Clipboard Formats.

Return Values

If the function succeeds, the return value is the handle of a clipboard object in the specified format. If the function fails, the return value is NULL. To get extended error information, call GetLastError.


The SetClipboardData function places data on the clipboard in a specified clipboard format. The window must be the current clipboard owner, and the application must have called the OpenClipboard function. (When responding to the WM_RENDERFORMAT and WM_RENDERALLFORMATS messages, the clipboard owner must not call OpenClipboard before calling SetClipboardData.)
HANDLE SetClipboardData(
  UINT uFormat, /* clipboard format */
  HANDLE hMem   /* data handle */


  • uFormat - Specifies a clipboard format. This parameter can be a registered format or any of the standard clipboard formats. For more information, see Registered Clipboard Formats and Standard Clipboard Formats.
  • hMem - Handle to the data in the specified format. This parameter can be NULL, indicating that the window provides data in the specified clipboard format (renders the format) upon request. If a window delays rendering, it must process the WM_RENDERFORMAT and WM_RENDERALLFORMATS messages.
After SetClipboardData is called, the system owns the object identified by the hMem parameter. The application can read the data, but must not free the handle or leave it locked. If the hMem parameter identifies a memory object, the object must have been allocated using the GlobalAlloc function with the GMEM_MOVEABLE and GMEM_DDESHARE flags.

EmptyClipboard function

Empties the clipboard and frees handles to data in the clipboard. The function then assigns ownership of the clipboard to the window that currently has the clipboard open.


BOOL WINAPI EmptyClipboard(void);


This function has no parameters.

Return value

Type: BOOL If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, call GetLastError.


Before calling EmptyClipboard, an application must open the clipboard by using the OpenClipboard function. If the application specifies a NULL window handle when opening the clipboard, EmptyClipboard succeeds but sets the clipboard owner to NULL. Note that this causes SetClipboardData to fail.

CloseClipboard function

CloseClipboard Closes the clipboard provided it was opened with OpenClipboard earlier.


BOOL WINAPI CloseClipboard(void);


This function has no parameters.

Return value

Type: BOOL If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, call GetLastError.


When the window has finished examining or changing the clipboard, close the clipboard by calling CloseClipboard. This enables other windows to access the clipboard.

Application example code

class CClipBoardDlg : public CDialog
public :
  CClipBoardDlg(CWnd* pParent = NULL);
  CString  m_szGetClipText;
  CString  m_szSetClipText;
10  protected :
11    virtual void DoDataExchange(CDataExchange* pDX);
13  protected:
15    virtual BOOL OnInitDialog();
17    afx_msg void OnGetClipboardData();
18    afx_msg void OnSetClipboardData();
21  };
23  CClipBoardDlg::CClipBoardDlg(CWnd* pParent /*=NULL*/)
24    : CDialog(CClipBoardDlg::IDD, pParent)
25  {
27    m_szGetClipText = _T("");
28    m_szSetClipText = _T("");
29  }
31  void CClipBoardDlg::DoDataExchange(CDataExchange* pDX)
32  {
33    CDialog::DoDataExchange(pDX);
34    DDX_Text(pDX, IDC_EDIT_GETCLIPBOARD, m_szGetClipText);
35    DDX_Text(pDX, IDC_EDIT_SETCLIPBOARD, m_szSetClipText);
36  }
38  BEGIN_MESSAGE_MAP(CClipBoardDlg, CDialog)
43  /* CClipBoardDlg message handlers */
45  BOOL CClipBoardDlg::OnInitDialog()
46  {
47    CDialog::OnInitDialog();
49    return TRUE; 
50  }
52  /* GetClipboardData as text format (steps) */
53  void CClipBoardDlg::OnGetClipboardData() 
54  {
55    if(OpenClipboard() == 0) {
56      AfxMessageBox("Unable to open clipboard!");
57      return;
58    }
60    /* Get handle of clipboard object for ANSI text */
61    HANDLE hData = GetClipboardData(CF_TEXT);
64    /* Lock the handle to get the actual text pointer */
65    char * pszText = static_cast<char*>( GlobalLock(hData) );
68    this->m_szGetClipText = pszText;
69    UpdateData(FALSE);
71    /* Release the lock */
72    GlobalUnlock( hData );
74    /* Release the clipboard */
75    CloseClipboard();
78  }
80  /* SetClipboardData as text format (steps) */
81  void CClipBoardDlg::OnSetClipboardData() 
82  {
83    int len ;
84    HGLOBAL hMem;
85    LPTSTR  lptstrCopy;
86    if(OpenClipboard() == 0) {
87      AfxMessageBox("Unable to open clipboard!");
88      return;
89    }
91    UpdateData(TRUE);
93    len = m_szSetClipText.GetLength();
94    hMem =  GlobalAlloc(GMEM_MOVEABLE, len +1);
95    lptstrCopy = (LPTSTR)GlobalLock(hMem);
96    strcpy(lptstrCopy, (LPCTSTR) m_szSetClipText);
97    GlobalUnlock(hMem);
98    EmptyClipboard();
99    /* Get handle of clipboard object for ANSI text */
100    SetClipboardData(CF_TEXT, hMem);
101    /* Release the clipboard */
102    CloseClipboard();  
103  }
105  /* CClipBoardApp.h - Application Class header */
106  class CClipBoardApp : public CWinApp
107  {
108  public:
109    CClipBoardApp();
110    virtual BOOL InitInstance();
112  };
114  /* CClipBoardApp.cpp - Application Class implementation */
116  CClipBoardApp::CClipBoardApp()
117  {
118  }
120  /* The one and only CClipBoardApp object */
122  CClipBoardApp theApp;
124  /* CClipBoardApp initialization */
126  BOOL CClipBoardApp::InitInstance()
127  {
128    AfxEnableControlContainer();
130    /* Standard initialization */
132    CClipBoardDlg dlg;
133    m_pMainWnd = &dlg;
134    int nResponse = dlg.DoModal();
135    if (nResponse == IDOK)
136    {
137    }
138    else if (nResponse == IDCANCEL)
139    {
140    }
142    /* Since the dialog has been closed, return FALSE so that we exit the application, rather than start the application's message pump. */
143    return FALSE;
144  }


Get/SetClipboardData example demo application

You have viewed 1 page out of 109. Your VC++ learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

#Programming Windows, Fifth Edition (Developer Reference)
#Programming Applications for Microsoft Windows (Microsoft Programming Series)
#Programming Windows with MFC, Second Edition
#Visual C++ Programming
#Programming Microsoft Visual C++, Fifth Edition

* #1 webmaster Sun 27 Apr/2014 18:06:58(GMT)  Like 0 Unlike 0

Dear Users,

We are pleased to inform you that a forum/blog has been incorporated with www.mybestnotes.co.in. You are welcomed to add your comments, requests, codes, solutions and feedback to it. Please login(if already a member) or signup(for free) to avail to this facility.


Your message goes here:

My Post:*
Secutiry Code: ******  *
Preview this compose before posting this in discussion forum.
  1. Use [Sxx] code to show smiles. Example [S02]
  2. Use [URL ], example [URL http://www.google.com/]
  3. To display code or un formatted text use [CODE] [/CODE]. Example: [CODE] printf("Hello world"); [/CODE]
 [S01]   [S02]   [S03]   [S04]   [S05]   [S06]   [S07]   [S08] 
 [S09]   [S10]   [S11]   [S12]   [S13]   [S14]   [S15]   [S16] 
 [S17]   [S18]   [S19]   [S20]   [S21]   [S22]   [S23]   [S24] 

Note: Only members are allowed, * fields are mandatory.