Problem in getting text from clipboard

prabhatia
prabhatia used Ask the Experts™
on
I have created a MFC application in which I select the text on notepad by double click on a word and copy it by sending WM_COPY message to notepad. After this I open clipboard and then take the data using GetClipBoardData() , store  that data in to a buffer and then close the Clipboard.
After this when I check the buffer , it is containing the value which was previously copied( not the current copied data).
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
How do you know that the copy data is successful? As per MSDN, if the recieving app processes the message, you should get a return value of TRUE.

Again, MSDN says that if you have to use COPYDATA, both apps should co-ordinate. I guess this method wouldnt allow you to make Notepad copy onto clipboard.

You need to check what message causes Notepad to make a selection and then copy onto clipboard.
Is your message selecting a text in Notepad?

Commented:
Get the window handle to the Notepad application. Use FindWindow(), giving the appname.

Then send the required messages to select text. To simulate Ctl+C, use WM_SETTEXT message with the appropriate arguments.

Commented:
Another sample using Keyup/keydown for sending Ctrl+V, PostMessage

PostMessage(FindWindowEx(ahwnd, NULL, "Edit", NULL), WM_KEYDOWN, 0x11, 0);//0x11 = Ctrl
PostMessage(FindWindowEx(ahwnd, NULL, "Edit", NULL), WM_KEYDOWN, 0x56, 0);//0x56 = v
PostMessage(FindWindowEx(ahwnd, NULL, "Edit", NULL), WM_KEYUP, 0x56, 0);
PostMessage(FindWindowEx(ahwnd, NULL, "Edit", NULL), WM_KEYUP, 0x11, 0);

Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

AndyAinscowFreelance programmer / Consultant

Commented:
From the help files:
An application sends the WM_COPY message to an edit control or combo  box to copy the current selection to the clipboard in CF_TEXT format.  

Did you send the message to the edit control wrapped by notepad or to the main window of notepad (or to some other window)?
AndyAinscowFreelance programmer / Consultant

Commented:
@xdomains - note WM_COPY not WM_COPYDATA as you are alluding to and is a totally different thing.

Commented:
Yes, AndyAinscow is right, I misread the question. WM_COPY copies into clipboard.

Then the issue could be either the selection is not set, or the right window handle is not selected.

Keep the Notepad open, and see what happens when you send the text selection and WM_COPY commands.

Author

Commented:
i used the following code
BOOL b=    ::SendMessage(hwnd1,WM_COPY,NULL,NULL);
but it does not work for notepad and microsoft word.
same operation work if i use post message but with post message above problem(mentioned in question ) comes.

Author

Commented:
please see my code:
    BOOL b=    ::SendMessage(hwnd1,WM_COPY,NULL,NULL);
    if(OpenClipboard( hwnd))
        {

        if( ::IsClipboardFormatAvailable(CF_TEXT) )
            {    //Sleep(500);
                HGLOBAL hMem = ::GetClipboardData(CF_TEXT);

                //
                if( hMem )
                {
                    if(strTextCopy!=NULL)
                    {
                        strTextCopy = (wchar_t *)::GlobalLock(hMem);
//problem:strTextCopy will contain the  previous data present on clipboard.
                            int err= GetLastError();

                            WriteThisFile(strTextCopy);

Commented:
SendMessage is not suitable for inter-thread communication. It can result in deadlock.

Use PostMessage only. When you use Postmessage(), the text gets selected, and it is copied into clipboard? Check with some clipboard viewers to ensure data exists in clipboard. Did you check this? For Office apps, there can be multiple clipboards which are managed internally, so check the application's clipboards for the data. Though, the last copied stuff should always go into system clipboard.

Maybe, after sending WM_COPY try sending the WM_KEYDOWN/WM_KEYUP commands as well, so that the default Ctrl+C behaviour of Notepad gets invoked.

Author

Commented:
YES, I CHECKED.WHEN I CLOSE MY APPLICATION CLIPBORAD SHOWS THE NEW DATA.
ACTUALLY I THINK PROBLEM COMING BACAUSE OF POST MESSAGE.AS IT ONLY POST THE MESSAGE AND RETURN EVEN WINDOW  PROCESSED THE MESSAGE OR NOT.I TRIED TO SIMULATE THE CTRL+C USING WM_KEYDOWN BUT IT DOES NOT WORK.IT SEND C TO MY EDITOR.IT IS TAKING IT AS CHAR .IGNORNING CONTROL KEY.
AndyAinscowFreelance programmer / Consultant

Commented:
There is a very big difference (and important but often misunderstood) between PostMessage and SendMessage.  PostMessage returns immediately and does not wait for the message to be processed, SendMessage waits for the message to be processed before returning.
Freelance programmer / Consultant
Commented:
   BOOL b=    ::SendMessage((HWND)0x0002061A,WM_COPY,NULL,NULL);
//    BOOL b=    ::SendMessage((HWND)0x0001061E,WM_COPY,NULL,NULL);
    HWND hwnd = GetSafeHwnd();
    if(::OpenClipboard( hwnd ))
    {
        if( ::IsClipboardFormatAvailable(CF_TEXT) )
        {  
            HGLOBAL hglb = ::GetClipboardData(CF_TEXT);

            if( hglb )
            {
                LPSTR lps = (LPSTR)::GlobalLock(hglb);

                if(lps)
                {
                    GetDlgItem(IDC_EDIT1)->SetWindowText(lps);
                }
                ::GlobalUnlock(hglb);
            }
        }
        ::CloseClipboard();
    }

-----------------------------------------------------------------------------------------------
The following is the main window of notepad and does NOT work.
 ::SendMessage((HWND)0x0002061A,WM_COPY,NULL,NULL);


The following is the edit window wrapped inside the main window of notepad and DOES work
 ::SendMessage((HWND)0x0001061E,WM_COPY,NULL,NULL);



Se my earlier comment about the behaviour of the WM_COPY message (which was straight out of the help files)
AndyAinscowFreelance programmer / Consultant

Commented:
As to getting the handle of the window containing the text you want to copy you could have a look at the following function.

The EnumChildWindows function enumerates the child windows that belong  to the specified parent window by passing the handle to each child window, in  turn, to an application-defined callback function. EnumChildWindows  continues until the last child window is enumerated or the callback function  returns FALSE.


I would suggest you ask another question about that task as it is nothing to do with the clipboard and may well require different code for different applications

Commented:
Prabhatia,
Is your issue resolved?
AndyAinscowFreelance programmer / Consultant

Commented:
comment #33210750 provides functional code that does what was asked for, along with a reason why the code from the asker did not work.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial