How to send keystrokes to an application in C++ - c++

I'm trying to make a program to open Acrobat files using Adobe Acrobat Reader and save them in a text file, automatically.
What I want my program to do is:
open the pdf
send Alt + Tab //to move to the acrobat tab
send Alt + F //to open file
send Down Down Down Down (4 times) //to select 'save as text' option
send Enter // to save
I'm using Windows OS.
can someone please help me on how to do this?
Well my finel goal is to save the title and author of about 2500 pdf files in a database automatically, what are the better ways you suggested ? this was what i came up with.

Try AutoIt. From it's website:
"AutoIt is a freeware Windows automation language. It can be used to script most simple Windows-based tasks."

You're going to want to use Spy++ and watch the messages being passed to the window when you perform these actions (note that opening the PDF with Acrobat and grabbing the Window handle are different operations). From there, look into:
http://msdn.microsoft.com/en-us/library/ms644950(VS.85).aspx
Win32 messaging is difficult if you're not used to it and fragile, as mentioned by #Alf. I'd suggest you try another approach, but if you Google "win32 sendmessage" or "win32 sendkeys" that should get you started.

Couple of things,
1) Simulating keystrokes to interact with another application is a very very bad idea. You're better off finding API's that'll do the same thing.
If you still haven't changed your mind, read further...
2) For Saving, Why not use Ctrl+S to save, Ctrl+O to open. I'm Sure you'll find direct shortcuts for the others too.
Here's a Project that might help.

You can use SendMessage API to send mouse and keyboard messages to a window, or you can use sendinput which simulates actual hardware events. I will agree with another person, use AutoIT

Related

Trying to write a c++ console program to change a setting controlled by a windows checkbox

Is it possible to create a keyboard shortcut to switch between the monitor and portion selection of this wacom preferences window, via a c++ console program?
Sorry if this is poorly worded, I've had trouble trying to find the right words to search for ways to do it.
I think it should be possible, although a bit tedious. You should be able to use the Windows API, and try to EnumWindows/EnumDesktopWindows to identify the respective application Window, and its respective controls (which are also Windows).
You should identify the window title, and class ids, for the app window, and the checkbox button controls, then when you enumerate through all the desktop windows, you can identify the ones you are interested in.
Then you can use the SendMessage() API to send messages to the controls (Windows) of interest to manipulate them.
It's a bit tedious, but sounds possible.
An example of use here to get an idea:
http://www.cplusplus.com/forum/windows/25280/

How to get a user input on a MessageBox on C++?

I have an application on C++ (on Windows API) and I ask the user to approve a task using MessageBox. However, as it's a bit sensible task and nobody reads the message, I want to change it to have an input box and the user type "I agree".
Does anybody know a simple way to do that? I find DialogBoxParam() which can do it, but it's overkilling for my needs, can you think on something more simple (or a simple way to use it)?
I found Prompting a user with an input box? [C++] quite similar to my question, but there is no satisfactory answer for me (using another lib is not an option).
You would have to write your own dialog for that. The MessageBox and related APIs do not offer such functionality. You could use the task dialog API (introduced in Vista) to show a dialog box with a button having customised caption. That might be a little better than plain MessageBox with its limited set of buttons.
I'm a little cynical about what you are trying to achieve in any case. If you force the users to type I agree they will ignore the content of the dialog box and type what you ask them to type.
The difference in outcome between your typing dialog and a standard button press dialog is that the user will take longer to get past the dialog, and will dislike your software, but the still not have read the content of the dialog. In other words, the only thing you will achieve by doing this is to hold up the user.
At some point you have to accept that the user takes responsibility for their actions. If you give them a helpful message and they choose to ignore it, ultimately that is on them.

Is it possible to embed a command prompt in a win32 app?

In linux and when installing packages etc. There are some installers that have a progress bar and a dos window which shows the files being extracted etc. How can i add this window to my C++ Win32 programs so that i can have it showing the tasks im doing? I cannot find any documentation on MSDN.
Question: How can i add a console window (if that's what its called, sure looks like one) in my program to show the details of the task at hand being done?
Here is a window with what i am asking.. (personal info so I erased the details. :]
You cannot embed a real console window inside another window (although a windowed process can have a separate console window). While it looks like a console window / command prompt, it is just a matter of appearances. What you want to do is create a sub-window/control with similar characteristics as a console window and then redirect the console output from the application(s) being run to append to that sub-window. For more information on how to do redirect the console output in Windows, see http://support.microsoft.com/kb/190351.
That "dos window" is a regular edit control: CreateWindow(ES_MULTILINE, EDIT, ...
However, it has the font set to a fixed-width one (Looks like courier). This is done by sending WM_SETFONT to the edit control.
#user995048 says "You cannot embed a real console window inside another window". But "cannot" is a strong word! I can run an entire virtualized computer in a window if I wish. :) So one can quite reasonably intuit that there are ways of doing what you say.
Sure, it is true that what you've seen are almost certainly cases of output redirection into a custom widget, designed to mimic the simple appearance of a terminal. However...if you want to embed one application's window inside another, there are things you can look into which might fit. Cooperative methods exist like GtkPlug, for instance:
http://developer.gnome.org/gtk/2.24/GtkPlug.html
To actually capture a not-designed-to-cooperate app's window and throw it in your app would be trickier. But possible, just as screen captures and virtual machines are possible. Probably best to avoid that sort of thing unless there's really a cause for it, though...
Try this
http://www.codeguru.com/cpp/misc/misc/article.php/c277/
link. I think the solution provided is what you need.
I tried it many years ago and it worked. I have not tried it in newer versions of windows though.

C++ -- Win32 API, GUI stuff

I've been messing with Win32 API for abit, and I've got a question regarding the GUI functions.
How does one handle user input that is not managed through popup windows? I've been reading http://www.winprog.org/ but right when the interesting features comes -- lesson9 -- it becomes more abstract and I'm not sure how to do it.
Basically what I am after is the user to write input in two windows and then press a button to send a message that the content of the input is to be processed.
I think the input windows would be some EDIT-class windows and the input BUTTON-class but that's about it.
Any ideas? I'm sure it's simple, it's just makes me want to rip my hair of in native code :p
Cheers
You're correct, you want and EDIT control which is more commonly known as a TextBox and a BUTTON class which is a command button.
To get the the input the Button will send a WM_COMMAND message to its parent window with a BN_CLICKED in the wParam high word. You can identify the particular button from the hWnd you get in that message.
After that you'll need to post a WM_GETTEXT to the edit control to retrieve the users input.
This is all from memory so I highly recommend looking up the msdn pages before you code.
I'm not sure I follow 100%. Yes, you would use EDIT and BUTTON-class controls for that. Where are you getting stuck?

want to open text file in some text editor programmatically in c/c++

I want to open a text file into some text editor say notepad programmatically in c/c++.
Also i want to see real time updation of text into that text file while opening in a editor.
Please suggest.
Most editors accepts the path of the file-to-be-opened as 1st argument. E.g.
notepad.exe c:/foo.txt
Just execute it as a shell/runtime command in your program.
If you're doing something as simple as monitoring a log file, you want a unix program called "tail", or its Windows equivalent.
It will give you a simple Notepad-like Window which displays the contents of a log file in (more or less) real time.
Having the editor (notepad, tail, whatever) continually monitor the file for changes isn't your job as the C++ developer, it's up to the program.
To get the file to open in the default application specified, try this (C#):
System.Diagnostics.Process.Start("text.txt");
To specify an application, try this:
System.Diagnostics.Process.Start( "notepad.exe", "text.txt");
I'm not aware of any way to handle real time updation of the file in the editor however.
After you have it running, see above. you can possibly keep the file updated using SendMessage from the Windows API and sending Ctrl+S(save) to the notepad window.
To see real time update in your editor window try to find editor's window handle (you may use EnumWindows() function). Insert text in the editor's textfield or reread it from the file and call RedrawWindow() after that. However calling it after each letter could give some nasty flickering if the text come from a program.