We help IT Professionals succeed at work.

Please, urgent : sending text to another windows application

Galilea
Galilea asked
on
How can i send a string of text to another windows application such as Word (i.e. Matlab). I want that everytime I
press a button a certain string, i.e. "String (return)" appears on the word screen. Anyway, some information
on the subject will be greatly appreciated 'cause it is like walking in the dark. Thank you.

jhance, I pin this question because I would greatly appreciate some help and want to get rid of all my question points. I promise you that after this I will disappear forever. All in all, thank you very much for your help, Experts Exchange has really been of great help to me, and has always guided me in the right direction.  
Comment
Watch Question

if you can get a handle to the window you could use SetWindowText - however that is only suitable to plain edit boxes - other windows eg word has an automation interface that you could use whether that could be DDE or COM - a good place to put text would be in to the clipboard and then you can use the paste option of the application involved to place it into your window - do yu have aparticular target in mind?

Author

Commented:
The target is the command window itself. I just want the text to appear on the Matlab window and a return to be implemented. Can you give me some guidance on how to use the clipboard, I wil try SetWindowText and let you know how it goes. Thank you.
MATLAB may have an automation interface but I haven't used it (MATLAB) for years so I don't know how it has advanced since

clipboard code


void AddToClipboard(LPCTSTR szClip)
{
    // Open the clipboard...
    if(OpenClipboard(0)) {
       
        // Empty what's in there...
        EmptyClipboard();
       
        // Allocate global memory for transfer...
        HGLOBAL hText = GlobalAlloc(GMEM_MOVEABLE |GMEM_DDESHARE, strlen(szClip)+4);
       
        // Put your string in the global memory...
        char *ptr = (char *)GlobalLock(hText);
        strcpy(ptr, szClip);
        GlobalUnlock(hText);
       
        ::SetClipboardData(CF_TEXT, hText);
       
        CloseClipboard();
        // Free memory...
        GlobalFree(hText);
       
    }

}

Author

Commented:
ShaunWilde,

Thank you very much. How can I call the paste command on the other application so that it appears? Is there any way to do this? How can I send a "Return" keystroke?
you could send a Ctrl+V key stroke to a window if you have its handle

Author

Commented:
Why can't I use
GetClipboardData(CF_OEMTEXT)?

Wouldn't I be able to produce the return keystroke as well?

How can I send a Ctrl+V stroke?

Author

Commented:
I can get its hadle using:
::FindWindow(NULL, "Matlab").
using SendMessage (or PostMessage) you could use WM_COMMAND and the ID of the paste command (can be got from the resources of matlab) or you could use WM_CHAR and build up the keystroke -

Explore More ContentExplore courses, solutions, and other research materials related to this topic.