Repeat character is getting skipped while dumping path programmatically in Powershell - c++

I am dumping characters one by one of a script file path onto powershell console using SendMessage API.
At the first execution of program, powershell skips the repeated character from a path which creates an issue.
For e.g. "C:\myFolder\abbc\test.ps1"
From above path when I dump a characters onto powershell window, it skips 1 b from "abbc" so the final path that gets dumped on console is "C:\myFolder\abc\test.ps1".
This happens only first execution of application, on subsequent executions it works fine and accepts repeat characters as well.

SendMessage is not the right way to do it. You should use SendInput. See also how to use sendinput function C++ for more information.

Related

How to get printed characters of Linux console at given position in C++ (or C)?

Suppose my whole screen (console) is filled with some text (e.g. printed through std::cout). How can I get some characters of this console output, e.g. in the center of screen?
In Windows I can get characters of console through ReadConsoleOutputCharacter function.
But how to do same thing in Linux (inside C++ (or C) code)?
Is there any easy way to do this without using extra huge libraries like ncurses? Maybe there are some standard Linux kernel sys-call functions to achieve this task?
I need to get console text of current running program. I.e. if inside my program I printed some text, later inside this same program I want to read parts of this printed to console text.

Reading the output streams from a C/C++ coded application

First of all, i'm on Ubuntu 14.04
So, here's my problem: I'm dealing with a C++ coded application that has a graphical interface (games/music players/etc). This application constantly sends strings to a logger whenever something happens, but those are only visible inside the client.
What I've tried to do already (failures):
strace the application and filter the results (let's say if the application showed the message "Hello, user", i would log all the outputs to a test file and search for "Hello")
ltrace the application
debug the application with dbg
search for debug methods on C/C++ apps
What I've got from this last method is that programs usually log errors and messages through a clog stream. What could I do to retrieve that information?
Resuming, I have a graphical C/C++ coded application that constant inputs strings on a window inside the client; I want to read those strings or any other strings/inputs this application does. Any debugging/memory reading information may also be helpful!
Thanks
std::cout corresponds to stdout stream;
std::cerr and std::clog corresponds to stderr stream.
By default, content "sent" to stdout and stderr is displayed in terminal.
To see it, simply open terminal emulator (or, alternatively, terminal), type path where the program is and confirm using Enter. You're going to see content sent to stdout and stderr.
stdout is represented by number 1, stderr by 2.
Next hints are designed to work in bash shell. (they may work on another shells, but they don't have to). If you're not sure you're using bash, type bash in terminal and confirm using Enter.
When you want to send stream's content to file, after path (but before pressing Enter) write space and n>filename, where n is number of stream you want redirect (when skipped, 1=stdout is going to be redirected). By default you won't see redirected content in terminal.
When you want simply completely ignore stream's content, redirect stream to /dev/null.
When you want send stdout to another program (if second program is console program, it's going to see it like entered by user using keyboard), after path (but before pressing Enter) type |program_name and_possible_parameters. You can in example redirect stdout to grep.
grep is going to write only lines containing string passed as argument (after grep, type space and a string, if it contain a space, delimit argument using "" or '', if it contain ' " or \ precede it with backslash (\). If it's result is going to appear in terminal, I recommend to write --color=auto between grep and argument to tell grep to write every occurrence of argument using different color.
Finally, your command can look like
path_to_your_program |grep --color=auto "argument".
You can use more than one redirection in single command.
Redirections are processed from left to right.
When you want to redirect stderr to input of another program, you can redirect it's content to stdout by typing 2>&1 and then use |.

How to capture the result of DOS Commands in C++

I am creating a windows application that must capture results of several DOS Commands without the command prompt opening and save them into a string. I am using the borland libraries.
system("dir") therefore is not good. The result of each command needs to be written to a string variable so I can write it to a log and a separate file.
I have an XML file where they are defined.
The thing i'm struggling on the most is actually capturing the output into a string variable. I have heard of _popen but having problems trying to use it.
I think what you really want is freopen() that allows you to redirect the usual streams to a file (either temporary or the separate file you mentioned), and when the process closes, read the file into your log.
I assume the 'result' is the output from the command, and not its return value.

ASCII user interface in C++ w/ Unix PuTTY terminal using escape sequences

I'm trying to make a simple ASCII user interface for a simple internet chat program. I'm planning it to look like this:
(name): message
(name): message
---------------------------------------------
(you): message |(cursor)
I was going to use ASCII (ANSI?) control characters to accomplish this.
Whenever the chat client receives a message from the server, it should update so that the message appears as the first message above the dash-line, then return the cursor to its previous position so the user can continue typing where they left off.
My initial plan was:
1. save the current cursor position (\e7)
2. move the cursor up 1 line (to the dash-line) and to the beginning of that line (\e[1F])
3. move the dash line down (\n)
4. move the cursor up one line again (to the now empty line) (\e[1A)
5. print the message from the server
6. restore previous cursor position (\e8)
all together: "\e7\e[1F\n\e[1A" << message << "\e8";
Where I'm having trouble is that the newline character seems only to move the cursor to the next line, and not actually insert a blank line. How can I accomplish this behavior?
This is for a homework assignment, but this is just an extra bit of flair i wanted to add on for myself. The actual assignment is already completed.
note: algorithm for handling user's input on their own screen is handled correctly already.
Look into something like pdcurses. It's cross platform. That will make all of those manipulations a lot easier. You can also check into curses on *nix and the ancient conio library on Windows if you don't mind your code not being portable.
If you are using bash, you can do it with special commands using characters escapes.
See http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x361.html
Seriously consider using a curses library (see Wikipedia for more information).

Async Console Output

I have a problem with my application win32 console.
The console is used to give commands to my application. However, at the same time it is used to output log messages which mostly comes from asynchronous threads. This becomes a problem when the user tries to write some input and simultaneously an async log message is printed, thus thrashing the display of the users input.
I would like to have some advice in regards to how to handle such a situtation?
Is it possible for example to dedicate the last line in the console to input, similarly to how it looks in the in-game consoles for some games?
You can use SetConsoleMode to disable input echo and line editing mode. You can then echo back input whenever your program is ready to do so. Note that this means you will need to implement things like backspace manually. And don't forget to reset the mode back when you're done with the console!
This is possible using the Console API, but it involves quite a bit of work and all the threads that use the console will have to cooperate by calling your output method rather than directly calling the Console API functions or the runtime library output functions.
The basic idea is to have your common output function write to the console screen buffer, and scroll the buffer in code rather than letting the text flow onto the last line and scroll automatically. As I recall, you'll have to parse the output for newlines and other control characters, and handle them correctly.
You might be able to get away with using "cooked" console input on the last line, although in doing so you risk problems if the user enters more text than will fit on a single line. Also, the user hitting Enter at the end of the line might cause it to scroll up. Probably best in this situation to use raw console input.
You'll want to become very familiar with Windows consoles.
Any time you have asyncronous threads trying to update the same device at once, you are going to have issues like this unless something synchronizes them.
If you have access to everyone's source code, the thing to do would probably be to create some kind of sync object that every task must use to access the console (semaphore, etc).