Sending commands to other programs

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
deanh22a
Posts: 23
Joined: Thu Feb 08, 2007 11:08 pm

Sending commands to other programs

Post by deanh22a »

I know this isn't an irrlicht question, but there's not a general programming thread in here and I don't know where else to ask this.

I want my program to interact with other programs, and i'm trying to find out how to send commands such as keystrokes to another program, similar to how a macro program works. Anyone know? thanks :)
roxaz
Posts: 575
Joined: Tue Jan 23, 2007 8:35 pm
Location: LT

Post by roxaz »

short answer: hooks
long answer: http://www.codeproject.com/system/hooksys.asp

good luck with long answer :lol:
Luben
Posts: 568
Joined: Sun Oct 09, 2005 10:12 am
Location: #irrlicht @freenode

Post by Luben »

Do you 'know' the other window in some way?
You can obtain the window handle with this function
http://msdn.microsoft.com/library/en-us ... frame=true

Then you can send messages to it using this function
http://msdn.microsoft.com/library/en-us ... frame=true

You have to figure out what messages to send for yourself. They typically begin with WM_. WM_CHAR just sends a translated char.

Code: Select all

HWND Window;
Window=FindWindow(NULL, "mIRC");
PostMessage(Window, WM_CHAR, 'g', 0);
The parameters for a WM_CHAR message are described here.
http://msdn.microsoft.com/library/en-us ... frame=true


Is this enough to solve your problem?
If you don't have anything nice to say, don't say anything at all.
deanh22a
Posts: 23
Joined: Thu Feb 08, 2007 11:08 pm

Post by deanh22a »

Luben wrote:Do you 'know' the other window in some way?
You can obtain the window handle with this function
http://msdn.microsoft.com/library/en-us ... frame=true

Then you can send messages to it using this function
http://msdn.microsoft.com/library/en-us ... frame=true

You have to figure out what messages to send for yourself. They typically begin with WM_. WM_CHAR just sends a translated char.

Code: Select all

HWND Window;
Window=FindWindow(NULL, "mIRC");
PostMessage(Window, WM_CHAR, 'g', 0);
The parameters for a WM_CHAR message are described here.
http://msdn.microsoft.com/library/en-us ... frame=true


Is this enough to solve your problem?

Nice. I was expecting it to be something like that but I didnt know which functions to use. I'll try it when I get back home. :)
deanh22a
Posts: 23
Joined: Thu Feb 08, 2007 11:08 pm

Post by deanh22a »

Luben wrote:

Code: Select all

HWND Window;
Window=FindWindow(NULL, "mIRC");
PostMessage(Window, WM_CHAR, 'g', 0);
I must be missing something because it doesnt seem to be sending anything. I tried the above code and I've tried things like

PostMessage(HWND_BROADCAST, WM_CHAR, 'g', 5);

in case it wasnt getting the hwnd, I just broadcasted to all. To test it, I opened up notepad and tried getting my program to send it a bunch of g's, but nothing was happening. maybe it needs to be initialized somehow? any ideas?
Luben
Posts: 568
Joined: Sun Oct 09, 2005 10:12 am
Location: #irrlicht @freenode

Post by Luben »

Yeah, seems like the 'main' window doesn't forward those messages to the active window, sortof. You could try to check which window is active if you need dynamic input.

But if you need fixed input, first need to identify which child-window you are searching for. You can use this tool to identify window parameters etc.
http://www.bestsoftware4download.com/do ... mlbum.html

For notepad, we need to get the child window which is an edit-type window-class. Luckily it is also the first/only child of the notepad window, so we just have to do this.

Code: Select all

	HWND Window=NULL;

	while(!Window)
		Window=FindWindow("Notepad", NULL);

	Sleep(25);

	//This jsut happens to be the edit window in notepad.
	Window=GetWindow(Window, GW_CHILD);

	//Just for easy debugging
	printf("%x\n",Window);
	bool Run=true;
	while(Run)
	{
		//Run until we fail, when the window is closed or something else happened
		Run=false;
		Run|=PostMessage(Window, WM_KEYDOWN, 'G', 0);
		Run|=PostMessage(Window, WM_KEYUP, 'G', 0);
		Sleep(1);
	}
But if you are just looking for ways to simulate input, independant of the target window, check out http://msdn2.microsoft.com/en-us/library/ms646310.aspx
Some programs, like most games, do not 'accept' input such as that, because they poll the keyboard themselves instead of waiting for a keyboard message.
If you don't have anything nice to say, don't say anything at all.
deanh22a
Posts: 23
Joined: Thu Feb 08, 2007 11:08 pm

Post by deanh22a »

I'm having trouble using INPUT (http://msdn2.microsoft.com/en-us/library/ms909626.aspx)

I found some examples using sendinput but I can't do anything with it because my compiler keeps saying INPUT is not defined even after I added #include <winuser.h> to my code.
Luben
Posts: 568
Joined: Sun Oct 09, 2005 10:12 am
Location: #irrlicht @freenode

Post by Luben »

I've run into that problem too. It has something to do with the numerous #defines in the windows headers. To make it work, put #define WINVER 0x0403 somewhere before #include <windows.h>, or copy the structs to your own file.
If you don't have anything nice to say, don't say anything at all.
deanh22a
Posts: 23
Joined: Thu Feb 08, 2007 11:08 pm

Post by deanh22a »

well, that last one didn't work for me but i found another way around it. Looks like everything's working the way I wanted. Thanks! 8)
Post Reply