Results 1 to 5 of 5

Thread: How can I write into and read from clipboard?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2000
    Location
    Auckland, NZ
    Posts
    182

    How can I write into and read from clipboard?

    Suppose I want to pass a complex structure from one function to another, but I can't use pointers (or references).
    How it can be done just using clipboard?

  2. #2
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    To put some data in the clipboard use the SetClipboardData API. Unfortunately i don't have an example right now, but here is some info:
    Code:
    SetClipboardData
    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
    );
     
    Parameters
    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. 
    
    Return Values
    If the function succeeds, the return value is the handle of the data.
    
    If the function fails, the return value is NULL. To get extended error information, call GetLastError. 
    
    Remarks
    The uFormat parameter can identify a registered clipboard format, or it can be one of the standard clipboard formats. For more information, see Registered Clipboard Formats and Standard Clipboard Formats. 
    
    The system performs implicit data format conversions between certain clipboard formats when an application calls the GetClipboardData function. For example, if the CF_OEMTEXT format is on the clipboard, a window can retrieve data in the CF_TEXT format. The format on the clipboard is converted to the requested format on demand. For more information, see Synthesized Clipboard Formats.
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  3. #3
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    There is another way to pass complex structures
    Code:
    WM_COPYDATA
    The WM_COPYDATA message is sent when an application passes data to another application. 
    
    WM_COPYDATA 
    wParam = (WPARAM) (HWND) hwnd;            // handle of sending window 
    lParam = (LPARAM) (PCOPYDATASTRUCT) pcds; // pointer to structure with data 
     
    Parameters
    hwnd 
    Handle to the window passing the data. 
    pcds 
    Pointer to a COPYDATASTRUCT structure that contains the data to be passed. 
    Return Values
    If the receiving application processes this message, it should return TRUE; otherwise, it should return FALSE. 
    
    Remarks
    An application must use theSendMessage function to send this message, not thePostMessage function. 
    
    The data being passed must not contain pointers or other references to objects not accessible to the application receiving the data. 
    
    While this message is being sent, the referenced data must not be changed by another thread of the sending process. 
    
    The receiving application should consider the data read-only. The pcds parameter is valid only during the processing of the message. The receiving application should not free the memory referenced by pcds. If the receiving application must access the data after SendMessage returns, it must copy the data into a local buffer.
    Code:
    COPYDATASTRUCT
    The COPYDATASTRUCT structure contains data to be passed to another application by the WM_COPYDATA message. 
    
    typedef struct tagCOPYDATASTRUCT {  // cds 
        DWORD dwData; 
        DWORD cbData; 
        PVOID lpData; 
    } COPYDATASTRUCT; 
     
    Members
    dwData 
    Specifies up to 32 bits of data to be passed to the receiving application. 
    cbData 
    Specifies the size, in bytes, of the data pointed to by the lpData member. 
    lpData 
    Pointer to data to be passed to the receiving application. This member can be NULL.
    This message can pass larger ammount of data to another app. It is not wise to use the clipboard because in the meantime another app can rewrite the content. I had an example of transfering data between apps (will have a look and post it).
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  4. #4
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    Here is the example. It uses three different methods to pass data between apps.

    Note: The comments are not in English, it uses MFC ('cos it is just an experiment).
    Attached Files Attached Files
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Nov 2000
    Location
    Auckland, NZ
    Posts
    182
    Thanks Vlatko,
    Now I will have a look more careful at it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width