Questions regarding postmessage and keystroke.

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1765 Views - Last Post: 21 March 2013 - 07:04 AM Rate Topic: -----

#1 necronox189  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 14-March 13

Questions regarding postmessage and keystroke.

Posted 14 March 2013 - 10:08 PM

Hi, well, please excuse me if this is the wrong place to post this...

i'm just trying to familiarize my self with C, anyhow, i am trying to create a program which will automatically login for me into the swinburne site (for example). except, i have absolutely no idea how to use the postmessage function in "windows.h". please note, although i am doing programming (undergraduate), this is neither related to homework or assignment/project, this is something i set for myself, since i really don't understand this postmessage function.

alright, here is the code i have so far:

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

int main ()
{
        ShellExecute(NULL, "open", "https://ilearn.swin.edu.au/webapps/portal/frameset.jsp", NULL, NULL, SW_SHOWNORMAL);
		Sleep (20000);
                postmessage (???) /* i have inserted the '???' since i have no idea what to do. */
 
        return 0;
 }



without the 'postmessage' it works fine.
now, i have looked at this website, but, honestly, i do not understand a word of it.

ps: my default browser is firefox, and i'm on win7 (home)
so here is my two questions:

1) could anyone help me with this postmessage function? (or provide me with a link to something that will help me)
2) this is still related, a function to send a keystroke, for instant, in vb, there is sendkeys.keystroke (....), is there such a similar thing in C? (again, a link will suffice, but i did not find any link to help me here)

thank you in advance =)

Is This A Good Question/Topic? 0
  • +

Replies To: Questions regarding postmessage and keystroke.

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1348
  • View blog
  • Posts: 4,643
  • Joined: 19-February 09

Re: Questions regarding postmessage and keystroke.

Posted 15 March 2013 - 02:23 PM

Here is an example using Notepad. Characters can be sent to a window, also a key down and key up message can be sent allowing key combinations.

PostMessage not working
Was This Post Helpful? 0
  • +
  • -

#3 necronox189  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 14-March 13

Re: Questions regarding postmessage and keystroke.

Posted 15 March 2013 - 07:18 PM

thankx i couldn't find any like that =)
Was This Post Helpful? 0
  • +
  • -

#4 necronox189  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 14-March 13

Re: Questions regarding postmessage and keystroke.

Posted 15 March 2013 - 10:14 PM

alright, i have worked on it a bit, in i have done this:

// ConsoleApplication2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <iostream>


using namespace std;


int main ()

{
	HWND HWND;
	cin.get();
	ShellExecute(NULL, L"open", L"https://ilearn.swin.edu.au/webapps/portal/frameset.jsp", NULL, NULL, SW_SHOWNORMAL);
	printf ("opening firefox at swinburne website\n");
	printf ("Waiting for page to open\n")	 	 ;
	Sleep (6000);
	printf ("Please maximize firefox");

			//STUDENT IDENTIFICATION NUMBER (ID)
				Sleep (300);
				PostMessage(HWND, WM_CHAR, '9', 1);
				PostMessage(HWND, WM_CHAR, '8', 1);
				PostMessage(HWND, WM_CHAR, '5', 1);
				PostMessage(HWND, WM_CHAR, '6', 1);
				PostMessage(HWND, WM_CHAR, '2', 1);
				PostMessage(HWND, WM_CHAR, '4', 1);
				PostMessage(HWND, WM_CHAR, '0', 1);
				
			//CHANGING TEXTBOX
				Sleep (300);
				PostMessage(HWND, WM_KEYDOWN, VK_TAB , 0);
				PostMessage(HWND, WM_KEYUP,   VK_TAB, 1);
				
			//PASSWORD
			//i have changed the characters to 'X' in PASSWORD for security reasons, in my actual program, this isn't the case, please, excuse any problems this may cause	
                                Sleep (300);
				PostMessage(HWND, WM_CHAR, 'X', 1); 
				PostMessage(HWND, WM_CHAR, 'X', 1);
				PostMessage(HWND, WM_CHAR, 'X', 1);
				PostMessage(HWND, WM_CHAR, 'X', 1);
				PostMessage(HWND, WM_CHAR, 'X', 1);
				PostMessage(HWND, WM_CHAR, 'X', 1);
				PostMessage(HWND, WM_CHAR, 'X', 1);
				PostMessage(HWND, WM_CHAR, 'X', 1);
				PostMessage(HWND, WM_CHAR, 'X', 1);
				PostMessage(HWND, WM_CHAR, 'X', 1);
				PostMessage(HWND, WM_CHAR, 'X', 1);
				
			//FINISHING
				Sleep (300);
				PostMessage(HWND, WM_KEYDOWN, VK_RETURN, 0);
				PostMessage(HWND, WM_KEYUP, VK_RETURN, 1);
		
	
	cout << "Goodbye";
	cin.get();
	return 0;
}



only when i run it, i get the error that HWND has not been initialized...

and, what i am suppose to do to fix this/what have i done to cause this..

again, thanks for the help
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3579
  • View blog
  • Posts: 11,130
  • Joined: 05-May 12

Re: Questions regarding postmessage and keystroke.

Posted 16 March 2013 - 05:35 AM

Of course, because nowhere in your code have you initialized HWND. You only declared it on line 17. You need to initialize it to the window handle of your browser after it starts up.

Let me also suggest creating a helper function that sends a string (using a loop over the characters) and the appropriate Enter key, instead of having all the duplicate code there.

I highly recommend picking up a copy of Petzold'sProgramming Windows. It looks like you are missing some key concepts about how to work with Windows and its APIs. Instead of bumbling around and picking up tidbits on the Internet, you would use your time more efficiently by learning things The Right WayTM.
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is online

  • member icon


Reputation: 4076
  • View blog
  • Posts: 12,584
  • Joined: 25-December 09

Re: Questions regarding postmessage and keystroke.

Posted 16 March 2013 - 05:37 AM

Quote

you would use your time more efficiently by learning things The Right WayTM.

You mean the Windows way, right?

Jim
Was This Post Helpful? 1
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3579
  • View blog
  • Posts: 11,130
  • Joined: 05-May 12

Re: Questions regarding postmessage and keystroke.

Posted 16 March 2013 - 05:40 AM

LOL! Yes. And the Windows way will teach you some ways of abusing the C and C++ languages by doing some crazy casts and making assumptions about data type sizes. :)
Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is online

  • member icon


Reputation: 4076
  • View blog
  • Posts: 12,584
  • Joined: 25-December 09

Re: Questions regarding postmessage and keystroke.

Posted 16 March 2013 - 05:51 AM

That and the excellent use Hungarian notation will surely drive people crazy. But I will say the Petzold book will help to bring some order to the chaos of the Windows API. But if you want to use C/C++ be careful, I believe the latest editions of this book are more geared to the Microsoft .Net languages.

Jim
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3579
  • View blog
  • Posts: 11,130
  • Joined: 05-May 12

Re: Questions regarding postmessage and keystroke.

Posted 16 March 2013 - 06:06 AM

Jim makes a good point about looking closely on the edition. Petzold's got an edition that is out that is in C# and targets using the .NET Framework, and he is currently working on a Windows 8 version of the book since Win8 has completely changed the Windows UI paradigm.
Was This Post Helpful? 0
  • +
  • -

#10 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 778
  • View blog
  • Posts: 2,311
  • Joined: 20-March 10

Re: Questions regarding postmessage and keystroke.

Posted 17 March 2013 - 04:54 AM

Hmm,

The best way to this would be to

use EnumWindows to find Firefox

then use EnumChildWindows to find the relevant textbox in Firefox.

then once you have the handle of the relevant child window ie its HWND

then use PostMessage or better still SendMessage.

Petzold will give you a good grounding but wont help you with

either EnumWindows or EnumChildWindows.

Overall what seems as an easy task is actually quite involved to make it work.

Just giving you a heads up on what is surely ahead if you want this to work.

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#11 necronox189  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 14-March 13

Re: Questions regarding postmessage and keystroke.

Posted 17 March 2013 - 05:33 AM

i shall experiment, i have never used EnumWindows or EnumChildWindows, (in fact, i didn't know they existed until now). anything i should know about that before i get started?
Was This Post Helpful? 0
  • +
  • -

#12 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 778
  • View blog
  • Posts: 2,311
  • Joined: 20-March 10

Re: Questions regarding postmessage and keystroke.

Posted 17 March 2013 - 05:53 AM

View Postnecronox189, on 17 March 2013 - 12:33 PM, said:

i shall experiment, i have never used EnumWindows or EnumChildWindows, (in fact, i didn't know they existed until now). anything i should know about that before i get started?


They both employ a function callback system...

which may be a steep learning curve depending on your ability..

if you get stuck ask for help..

I will keep an eye on the thread

Snoopy.
Was This Post Helpful? 1
  • +
  • -

#13 necronox189  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 14-March 13

Re: Questions regarding postmessage and keystroke.

Posted 17 March 2013 - 05:59 AM

Okay =) I will work onit tomorrow (it's midnight here) after my 10:30 class.

thanks for the help (this goes to all those who helped here) ;)
Was This Post Helpful? 0
  • +
  • -

#14 necronox189  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 14-March 13

Re: Questions regarding postmessage and keystroke.

Posted 21 March 2013 - 04:39 AM

alright, i don't want to go reviving dead posts or anything but i am just letting anyone that may be still watching this know:

i have, i suppose to some extent, made postmessage work with notepad, so that point has been somewhat resolved.

however, after a few days, i have gotten no-where with enumwindows or enumchildwindows, not even my professor new, so. that was not so helpful. so, if someone is willing to provide the code/snippet/example, than that would help me, but otherwise, i am clueless on this. (if the statement above is against rules then please excuse me and ignore it)

so thank you guys (and girls if applicable) about the help given.
Was This Post Helpful? 0
  • +
  • -

#15 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3579
  • View blog
  • Posts: 11,130
  • Joined: 05-May 12

Re: Questions regarding postmessage and keystroke.

Posted 21 March 2013 - 05:35 AM

See stackoverflow: http://stackoverflow...nfinitely-win32

I really suggest you pick up Petzold that I recommended above. You'll need basic information that is covered in the book.

This post has been edited by Skydiver: 21 March 2013 - 05:35 AM

Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2