Juha
Juha

Reputation: 2115

how do I use find window and sendmessage in empty visual c++ project (or console app)

I want to make the most simplest application that can communicate via windows send messages (and parse json). I have found a sample code:

CWnd* pWnd = FindWindow("old title");
pWnd->SendMessage(WM_SETTEXT,0,(LPARAM)"New title");

That works... but only if I use MS Visual Studios "create new MFC form application" wizard. How can I make a console application that sends messages to my program? Or can I? What do I need to include/link if I start an empty project or console application?

The goal in pseudocode:

a = ""
while !EOF
  a += read(stdin)

commandArray = jsonToArray(a)

CWnd* pWnd = FindWindow("program");
pWnd->SendMessage(WM_COPYDATASTRUCT,0,commandArrayWrappedInCOPYDATASTRUCT);

exit

The annoyance is that the effective part of the code is roughly 20 lines (above), but the wizard generated part is hundreds of lines. And most of them is stuff that I don't understand. Plus, I get a window that I don't need.

EDIT

Final main.cpp (without the json stuff):

/*
This closes calculator
*/

#include <Windows.h>
#include <atlstr.h>

int main (void)
{
    HWND HWnd = FindWindow(NULL, CStringW("Calculator"));
    SendMessage(HWnd, WM_CLOSE, 0, 0);

    return 0;
}

br,

Juha

Upvotes: 1

Views: 5413

Answers (1)

David Heffernan
David Heffernan

Reputation: 613302

If you want something so simple, then I'd just forget all about MFC and start with a basic console app from the New Project Wizard. MFC seems rather heavy duty for something so simple.

Upvotes: 1

Related Questions