help "Copy String to Clipboard"

This is a discussion on help "Copy String to Clipboard" within the Windows Programming forums, part of the Platform Specific Boards category; Can you fix it for me with error.( i'm using quincy to compile) Code: #include <windows.h> #include <string.h> #include <direct.h> ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    5

    help "Copy String to Clipboard"

    Can you fix it for me with error.( i'm using quincy to compile)
    Code:
    #include <windows.h>
    #include <string.h>
    #include <direct.h>
    #include <stdlib.h>
    
    int main()
    {
        /* const char* output=(char*)malloc(10000*sizeof(char));*/
        const char* output;
        output=(char*)malloc(10000*sizeof(char));
        output="http://cboard.cprogramming.com/http://cboard.cprogramming.com/http://cboard.cprogramming.com/http://cboard.cprogramming.com/http://cboard.cprogramming.com/http://cboard.cprogramming.com/http://cboard.cprogramming.com/http://cboard.cprogramming.com/http://cboard.cprogramming.com/http://cboard.cprogramming.com/http://cboard.cprogramming.com/http://cboard.cprogramming.com/http://cboard.cprogramming.com/http://cboard.cprogramming.com/http://cboard.cprogramming.com/";
        const size_t len = strlen(output) + 1;
        HGLOBAL hMem =  GlobalAlloc(GMEM_MOVEABLE, len);
        memcpy(GlobalLock(hMem), output, len);
        GlobalUnlock(hMem);
        OpenClipboard(0);
        EmptyClipboard();
        SetClipboardData(CF_TEXT, hMem);
        CloseClipboard();
        return 0;
    }

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    I don't think you can use a NULL HWND. Try this:
    Code:
    OpenClipboard(GetDesktopWindow());
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    5
    Quote Originally Posted by oogabooga View Post
    I don't think you can use a NULL HWND. Try this:
    Code:
    OpenClipboard(GetDesktopWindow());
    Still not work. i think my problem is string "output" too long. Can you fix it with long string.
    Code work OK with output="test" !
    Code:
    #include <windows.h>
    #include <string.h>
    #include <direct.h>
    #include <stdlib.h>
    
    int main()
    {
        const char* output="test";
        const size_t len = strlen(output) + 1;
        HGLOBAL hMem =  GlobalAlloc(GMEM_MOVEABLE, len);
        memcpy(GlobalLock(hMem), output, len);
        GlobalUnlock(hMem);
        OpenClipboard(0);
        EmptyClipboard();
        SetClipboardData(CF_TEXT, hMem);
        CloseClipboard();
        return 0;
    }
    Last edited by haibuithe; 10-02-2012 at 11:29 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 46
    Last Post: 08-24-2007, 05:52 PM
  2. Write a copy constructor for "String" class...
    By zaracattle in forum C++ Programming
    Replies: 14
    Last Post: 10-12-2006, 09:39 AM
  3. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM

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