CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Sep 2009
    Posts
    2

    Text/number input in other programs

    Hi!

    First of all I would like to tell you, that I'm not a profressional programmer nor I want to be one, just have to deal with some stuff at work. And also, I have learned a bit C programming language, but now I'm dealing with C++ (i'm using Microsoft Visual C++ 2008 Express edition).

    The main task was to write a code that inputs text in other softwares within the same PC. I started with inputing text in notepad and right now I'm about this far with the code, which works. It's good for a start but not really what I need. So, here is what I have

    Code:
    #include <windows.h>
    #include <iostream>
    #include "stdafx.h"
    #include <string>
    
    using namespace std;
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        HWND hWndNotepad = FindWindowA((LPCSTR)"Notepad", NULL);
        if(!hWndNotepad)
        {
            cout << "Notepad not found!" << endl;
            system("PAUSE");
    		return 0;
        }
    
        HWND hWndEdit = FindWindowExA(hWndNotepad, NULL, "Edit", NULL);
        if(!hWndEdit)
        {
            cout << "Notepad not found!" << endl;
            system("PAUSE");
    		return 0;
        }
    	SetForegroundWindow(hWndEdit);
    	    
    	wchar_t *int = L"Hello!";
        int size = wcslen(int);
    
        for(int i=0; i<size; i++)
            SendMessage(hWndEdit, WM_CHAR, int[i], 0);
    	
    	system("PAUSE");
        return 0;
    }
    What it does is that it checks whether notepad is running (i don't need to launch it from the code of my program), and ends if notepad isn't running. If notepad is running, then I get text "Hello!" entered in notepad. Good for start.

    But I have several questions about how to modify the code so that I get this program to do what I need to do.

    First thing I need to know is how do I enter text to be entered in notepad from my application (i'm making this a console application). For instance, a text appears in my console that is asking me to enter text and then after entering it, that text is typed in notepad.

    Second, is there a possibility to send any keystrokes to notepad? I have read about SendKeys (Send, SendWait) but didn't find anything useful about how to use it. Here, for instance, I need to send ENTER after the text I send to notepad. Is it even possible in MS Visual C++?

    Also, if I need to send only numbers (integers) to notepad, will the structure of a code be simplier? Because I will need to make that software to send numbers (integers or floating point) in a cycle (i know how to make those in C), for example send integers from 400 to 800 with a step 5 and press enter after each cycle (not to make it a new line, but to press ENTER. i believe there is a difference)

    And finally, is there a possibility to make program to access sub-menus and to enter numbers there? For example, in notepad it could be like moving through menus "Edit" then choose "Go To ..." and enter number (integer) there and press ENTER. It is possible

    Hope I'm not making too much questions, but just can't find much help about this stuff which I really need.
    Also, if it's not possible to make SendKeys work in MS Visual C++ (i've read something about it), maybe I need to write this program in some other language? Suggestions welcome about which language to use to make this program as easy as possible.

  2. #2
    Join Date
    Apr 2009
    Posts
    598

    Re: Text/number input in other programs

    I have tried to do the same things.
    Then, I have discovered a tool, named autoit. See http://www.autoitscript.com/autoit3/ . This tool is quite popular. It has been extended and now it might look complex for beginners, but it still is much easier to use it than to write the same things in C/C++.

  3. #3
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    894

    Re: Text/number input in other programs

    Just FYI, what you're trying to do has been "outlawed" in Windows Vista and will be probably enforced even more in Windows 7. The main reason for that is a potential for a big security risk if one application could write into another one without certain permissions and user privileges.

    I don't know the operating system you're coding it on, so it may work on XP but you're definitely facing an uphill battle with later OS's, even with some third-party solutions (like the one suggested above).

    What do you need all this for?

  4. #4
    Join Date
    Sep 2009
    Posts
    2

    Re: Text/number input in other programs

    Quote Originally Posted by dc_2000 View Post
    What do you need all this for?
    I was a bit aware that it is "outlawed" or so in Vista/Windows 7, but I need it to work in Win XP.

    I need this all to make some spectroscopic measurements automated through measurement software (which don't have such functions) with the help of coded C++ program.
    Without going into details, I'll explain a bit about how that software works - there is, a measuring software. Each measurement is trigered by pressing a button in a software or by pressing (for example) F5. Because they are spectral measurements they are made on a scale. And the task is to move the central point of the scale with the help of another software (to make it automated, for example, set a cycle where scale is changed from 200 to 2000 with a step of 20). The scale can be changed from sub-menus, for example in that software "Set" -> "Change middle point of scale", enter a number and press ENTER.
    So, a cycle could go like this - we set a scale to 200, then with the help of my made program in C++, F5 ir pressed, the main software takes measurement (some delay is set in program after pressing F5), afterwards it is saved (whether by simulating Ctrl+S or some other way) and new center point is set and so on.
    So the main idea is to make the software check whether the software is running, go to sub-menus to set new middle point of scale and take measurement.
    I believe it involves only finding windows (which I already did), simulation of keystrokes (which changes in a cycle), and guiding through submenus.
    It could easily be done with a macro if I didn't had to change the numbers I enter.

  5. #5
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    894

    Re: Text/number input in other programs

    OK, in that case you will hardcode it not only to a specific OS but also to a specific version of your software. Here's what you can use:

    Keep in mind that "none" of these methods are recommended for your everyday use.

    1. Your safest bet would be to send keyboard strokes directly to windows in your software. In that case you'll need to simulate most of these messages.

    2. You can also use the following APIs to simulate keystrokes: SendInput(), or keybd_event() for keyboard. The downside of this approach is that at the time you call these methods the window of interest must have a keyboard focus, which by itself is a challenge. You can switch window/keyboard focus with the following APIs: SetForegroundWindow() (read additional notes on MSDN page), and SetFocus(), but there's no real guarantee that after these APIs return success that the focus will remain there at the time when keyboard simulation APIs are called, so if the focus switches to some other window in the meantime, there'll be no way for you to know if your simulated input was received.

    3. In case of menu commands, I'd first look for shortcut keys (something like Ctrl+S, etc), and if you find one, use it as a simulated sequence of keystrokes. Also keep in mind that you'll need to send those to the main/top window of the program. If there are no shortcuts involved, your second bet could be simulating a mouse click.

    4. To simulate a mouse click, again, your best bet would be to send mouse messages directly to the windows of interest. You'll be using these notifications.

    5. The same as in item 2, you can use SendInput(), or mouse_event, but again in this case you'll have to deal with the window focus and position on the screen. For that you will also need to change the z-order of windows to be able to click them. Use the SetWindowPos() API and hWndInsertAfter parameter for the z-order operations. The downside of this method is again no way of knowing if your input will actually be received by the window of interest since the focus can be switched at any time after you do it in your program and before the mouse simulation API is called.

    6. To find out window handles to work with, use FindWindow() and FindWindowEx() if a window is a child of another window. It'd be nice to also specify a window class along with it's name for those APIs to make it more reliable, or if a window name may change in the process. One thing that you should remember is that window handles change from each load of your software of interest, so you cannot hardcode them.

    7. Lastly, to find out window names and corresponding class names, as well as to see what window messages/notifications should be simulated, use the Spy++ tool from the MS Visual Studio toolset. If you don't have it you can Google for any of the third-party free tools like WinID, WinSpy, etc. Also keep in mind that while simulating window messages/notifications for keyboard and mouse input you have to send them in the same succession as you see it happen in "real" situation during a regular mouse click or a keyboard stroke. For that you will have to set Spy++ to trap messages and then do a keystroke or a mouse click and see what messages are sent to the window of interest.


    So as you see you need to do some research before you can begin with all this stuff.

  6. #6
    Join Date
    Sep 2009
    Posts
    5

    Re: Text/number input in other programs

    All of this is very classic and has been posted for ages on IRC and Usenet (since 1990...)
    You can ask on old Advanced Win32 api group http://tinyurl.com/cmhb5g (C code, from Win32 gurus (from XP and Vista source code (// OS)....)

  7. #7
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Text/number input in other programs

    Quote Originally Posted by kalendara View Post
    First of all I would like to tell you, that I'm not a profressional programmer nor I want to be one, just have to deal with some stuff at work.
    I always wonder what is the intention behind such kind of statements? Should it mean that some way Windows or Visual C++ must be more merciful to non-professional programmers just because they don't want to be professional?
    Last edited by Igor Vartanov; October 11th, 2009 at 02:42 PM.
    Best regards,
    Igor

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

Featured