Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have been programming in C++11 on a 32 bit Windows operating system. When I run the program it is difficult for me to follow what it is doing. I am now attempting to have my program open up Wordpad seperately and write to Wordpad as my program executes a command or does something that I want to record there.

I have tried this with NotePad and that works OK. But, I want to be able to use Wordpad since it holds a lot more.

Here is where I am:

I added some, but I am lost in what is going on. Suggestions please.

What I have tried:

<pre>    STARTUPINFO si = {};
    si.cb = sizeof si;

    PROCESS_INFORMATION pi = {};
    LPCWSTR target = L"C:\\Program Files\\Windows NT\\Accessories\\wordpad.exe";

    if ( !CreateProcess(target, nullptr, nullptr, nullptr, false, 0, nullptr, nullptr, &si, &pi) )
    {
        MessageBox(nullptr, L"CreateProcess failed for notepad .", L"Error!", MB_ICONEXCLAMATION | MB_OK);
    }
    else
    {
        WaitForSingleObject(pi.hProcess, 1);
        cout << L"\nYou have entered an empty reply, please try again...";

    }

<pre>	STARTUPINFO si2;
	PROCESS_INFORMATION pi2;

	ZeroMemory( &si2, sizeof(si2) );
	si2.cb = sizeof(si2);
	ZeroMemory( &pi2, sizeof(pi2) );
//	if (!
        CreateProcess
			(
                TEXT("C:\\Program Files\\Windows NT\\Accessories\\wordpad.exe"),
                NULL,NULL,NULL,FALSE,
                CREATE_NEW_CONSOLE,
                NULL,NULL,
                &si2,
                &pi2
			);
//        )
    cout << "Unable to execute.";


//LPCWSTR CStringW;
HWND handle = FindWindow(NULL,L"Untitled - Notepad");

HWND edit = FindWindowEx(handle, NULL, L"Edit", NULL);
PostMessage(edit, WM_CHAR, 'a', 0 );    // How do I send a wide string instead of a single character?

And how do I do this in Wordpad instead of Notepad?
Posted
Updated 20-May-22 14:16pm

What you are trying to do is a very old fashioned way of debugging a program - and it's called "logging" and should probably be sent to a file directly instead of messing around with external apps, as that adds significant overhead and may make problems worse rather than better.

Instead, why not join the twentieth century and use a debugger? Visual Studio includes an integrated debugger which will let you follow what code is doing while it runs, inspect (or change) variable content, and single step code to follow exactly what it is doing, without having to change your code in any way (just compile for debug info instead of release). In some cases debuggers can even let you change code while it is running!

It may take you a short time to learn how to use one, but you will save so much time that it is seriously worth while ...
 
Share this answer
 
Comments
Member 15078716 20-May-22 18:41pm    
Thank you OriginalGriff for your opinion, but you did not even slightly answer my need. Not even close. You gave a biased assumption as to what I am trying to do. You attempted to insult my activity choice by assuming that I did not understand the use of a debugger. Wrong. And wrong. Sending to a file is the wrong answer. I did not ask how to send to a file for later viewing. I am trying to code a semi-realtime viewer of some of the chosen activities of my program. I know what a key logger, etc. is from a Visual Studio perspective. You assumed that I might want to go from C++ to a crippling almost anti-coding platform like Visual Studio. Your assumption was wrong. I have placed together Visual Studio's pre-cast blocks in the past many times due to it's Rapid Application Development (RAD) environment. But, I am tired of that and I am attempting to program like a "real" programmer with C or C++. I came here reading your posts (OrininalGiff) and considering you a valuable resource for logical code, but you let me down a long way on this one response. Please stick to the subject and if you are willing to help me leave the confines of the Visual Studio cage, address my request. Thank you.
Rick York 20-May-22 20:00pm    
"You assumed that I might want to go from C++ to a crippling almost anti-coding platform like Visual Studio."

That is the most ridiculous statement I have read in a long, long time.

He was exactly right - using an external program in this manner will add in an incredible amount of overhead and is an absurd thing to even attempt.
Member 15078716 20-May-22 18:45pm    
edittted
Member 15078716 20-May-22 20:22pm    
Thank you Rick York for your opinion, but you sound like you might have told Ford years ago a similar thing if you lived back then. "an absurd thing to even attempt." Yet billions of people today can thank Ford for not accepting the advice of the weak minded.
To answer a specific question asked in your code : "
Quote:
How do I send a wide string instead of a single character?
You can't. Windows SDK calls that accept strings require it to be passed by address and that address must be of memory accessible by the target process. The address of a string in your program is not valid to another program unless it is an memory shared by both processes and it takes quite a bit of gyration to make that happen unless you have control over the code of both of them. The source code for WordPad is available so if you really want to, you could modify it to use shared memory and respond to a message that tells it to read strings from that shared memory.

In my opinion a better option would be to open a console window from your program and send strings to it. There are lots of code samples that show how to do this.
 
Share this answer
 
Comments
Member 15078716 20-May-22 20:28pm    
A console window. Not a bad idea, except that it would not be able to handle the volume of text as I want in the manner that I want and report it as I want. So, maybe you might have told Ford years ago, if you lived back then, that hitching up teams of horses to boxes on wheels would be a better way to mass-produce. You have done very nice elsewhere in your posts, but you failed in this one. And, Windows SDK is certainly not a very viable option even with the mindset of Visual Studio's cripples.
Rick York 21-May-22 13:48pm    
A console WILL handle a large volume of text. I have done it myself with dozens of process and four or five consoles, each having a buffer with 9999 lines of text and the systems runs non-stop.

Since you are running on a Windows OS, (32-bit FFS), the Windows SDK is your ONLY option if you are going to do something so hair-brained as sending strings to WordPad. Its source code is available so have at it. Who are we to dissuade you from that?
Member 15078716 23-May-22 12:24pm    
I did not say that a console will not handle a large volume of text. You need to learn to read. You need to pay attention to the conversation. I said "except that it would not be able to handle the volume of text as I want in the manner that I want and report it as I want." There are no commas in that sentence. There are no delimators in that sentence. It is a complete multi-conditional. Pay attention to what you are reading. The desired result is better in WordPad.

No. Windows SDK is not my only option. Are you so mentally challanged that you can not see past Microsoft holding your hand with Windows SDK? Do you fear falling down the rat-hole that your ignorant bias has dug? And, it is not a "we" that are answering from your childish mind. You are not a "we". No matter how your imagination entertains that misappropriated thought.

Get back to reality, leave Microsoft's cripples, and make useful suggestions.

Thank you.
Member 15078716 10-Jun-22 22:55pm    
Reference: http://www.charlespetzold.com/etc/DoesVisualStudioRotTheMind.html

A quote that might help you to understand, and you might enjoy reading the entire page after this:

" The Pure Pleasures of Pure Coding

A couple months ago — perhaps as an antidote to all this highfalutin Windows Forms and Avalon programming I’ve been doing — I started coding in C again. Just a little bit. A magazine I read — the British weekly New Scientist — runs a tiny regular feature called “Enigma,” which presents a little math puzzle. Here’s a particularly short one from this past June: “What is the largest integer whose digits are all different (and do not include 0) that is divisible by each of its individual digits?”8 If you solve one of the Enigma problems, you can send in your solution, and one is chosen at random on a particular date, and if yours is picked you get 15 pounds and a mention in the magazine.

These Enigma problems have actually annoyed me for years because they always seemed solvable by writing a short program and testing out the possibilities, and for some reason that seemed absurd to me.

A few months ago I decided it might actually be interesting solving the problems with code, and then posting the programs and solutions on my web site the day after the deadline, but about a week before the answer appears in print.

I decided to use plain old ANSI C, and to edit the source code in Notepad — which has no IntelliSense and no sense of any other kind — and to compile on the command line using both the Microsoft C compiler and the Gnu C compiler.

What’s appealing about this project is that I don’t have to look anything up. I’ve been coding in C for 20 years. It was my favorite language before C# came along. This stuff is just pure algorithmic coding with simple text output. It’s all content.

I also discovered is that the problems do require some thought before you code them up. Very often, the total number of combinations is prohibitive. You really have to whittle them down. You don’t want to write a program that runs for a week before getting to the printf statement. For example, take the problem I quoted: “What is the largest integer whose digits are all different (and do not include 0) that is divisible by each of its individual digits?” It really helps to realize that this integer cannot contain the digit 5 if it also contains any even digit, because then the number would be divisible by 10, and the last digit would be 0, which isn’t allowed. So, the number we’re looking for probably doesn’t include 5. That immediately reduces the possibilities by an order of magnitude.

Even after this preliminary process, there’s still coding to do, but there’s no APIs, there’s no classes, there’s no properties, there’s no forms, there’s no controls, there’s no event handlers, and there’s definitely no Visual Studio.

It’s just me and the code, and for awhile, I feel like a real programmer again. "
Member 15078716 20-May-22 22:26pm    
Rick York, I know from lots of your other posts that you and OriginalGriff are really smart at code. What do you think about if I were to subclass Wordpad and use it that way? Don't be so politically correct. Would subclassing do it? Suggestions, please.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900