473,382 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

C# API Sendmessage or PostMessage to replace SendKeys - Help Needed

Hello everyone.

I have what I believe to be a simple question, yet I can not find anything that is helpful to me.

Basically I have an application, that I want to use to control external applications. My program will run either embedded within another program or minimized to the system tray. So SendKeys is out because it only works if the controlled application has focus, which it will not.

So my conclusion is the windows API. I have never used the windows API, and from my experience the past few days I am not sure I want to!

So I am using user32.dll like this (these are not written in stone and are used by looking at other examples):
Expand|Select|Wrap|Line Numbers
  1. [DllImport("user32.dll")]
  2.         public static extern int FindWindow(
  3.         string lpClassName, // class name
  4.         string lpWindowName // window name
  5.         );
  6. [DllImport("user32.dll")]
  7.         public static extern int SendMessage(
  8.         int hWnd, // handle to destination window
  9.         uint Msg, // message
  10.         int wParam, // first message parameter
  11.         int lParam // second message parameter
  12.         );
  13.  
FindWindow() works perfectly. I can get the handle of the window. And the sendmessage function works as well with the example I pulled it from which calls the close command of the window like this:

Expand|Select|Wrap|Line Numbers
  1. const int WM_SYSCOMMAND = 0x018;
  2. const int SC_CLOSE = 0x053;
  3.  
  4. int WindowToFind = FindWindow(null,"Untitled - Notepad");
  5. int result = SendMessage(WindowToFind, WM_SYSCOMMAND, SC_CLOSE, 0);
  6.  
Now this is all fine and dandy, but the question becomes, how do I send the keypress "A" or "B" or "Ctrl+D" to some window?

I read that the command "WM_KEYDOWN = 0x0100" should be used, but what is the argument? Is it the character code in hex? Is it some random thing? I have not seen any working code or full samples of code anywhere. If someone could link me to some fully working code to do this, or just post a sample I would be highly appreciative.

Thankyou in advance...
Apr 22 '07 #1
11 47969
Anyone? Bueler, bueler?
Apr 23 '07 #2
Doe nobody know how to send keystrokes through the windows API?
Apr 23 '07 #3
RedSon
5,000 Expert 4TB
Anyone? Bueler, bueler?
Instead of trying to be "cute" by making a movie reference that probably only 10% of the people here have seen, why don't you try posting to the correct forum. The .NET forum is where the C# questions are answered not the c/c++ forum. But lucky for you I moderate both and can move this thread over. Next time pay better attention to what you are doing.
Apr 23 '07 #4
RedSon
5,000 Expert 4TB

Expand|Select|Wrap|Line Numbers
  1. const int WM_SYSCOMMAND = 0x018;
  2. const int SC_CLOSE = 0x053;
  3.  
  4. int WindowToFind = FindWindow(null,"Untitled - Notepad");
  5. int result = SendMessage(WindowToFind, WM_SYSCOMMAND, SC_CLOSE, 0);
  6.  
Now this is all fine and dandy, but the question becomes, how do I send the keypress "A" or "B" or "Ctrl+D" to some window?

I read that the command "WM_KEYDOWN = 0x0100" should be used, but what is the argument? Is it the character code in hex? Is it some random thing? I have not seen any working code or full samples of code anywhere. If someone could link me to some fully working code to do this, or just post a sample I would be highly appreciative.

Thankyou in advance...
You can use SendMessage the same way as before but instead of WM_SYSCOMMAND you make it WM_KEYDOWN and instead of SC_CLOSE you need to pass the hex value of the keypress or see if the VK_ key mappings will work for you. Also be aware how WM_KEYDOWN messages are handled you may need to make up special WPARAM and LPARAM parameters in order to process Shift+ Ctrl+ and Alt+ keys.
Apr 23 '07 #5
RedSon
5,000 Expert 4TB
Here you go:

The structure of a WM_KEYDOWN message: http://msdn2.microsoft.com/en-us/library/ms646280.aspx
Apr 23 '07 #6
RedSon
5,000 Expert 4TB
You might also want to consider sending a WM_KEYUP message since it is almost impossible to only press a key down and never up on a computer system. Some applications my not respond to input until they receive both a KEYDOWN and KEYUP message.
Apr 23 '07 #7
Instead of trying to be "cute" by making a movie reference that probably only 10% of the people here have seen, why don't you try posting to the correct forum. The .NET forum is where the C# questions are answered not the c/c++ forum. But lucky for you I moderate both and can move this thread over. Next time pay better attention to what you are doing.
I honestly didnt even see the .NET area. sorry. I figured C# fits into C++/C better than basic or java. sorry again.
Apr 24 '07 #8
You can use SendMessage the same way as before but instead of WM_SYSCOMMAND you make it WM_KEYDOWN and instead of SC_CLOSE you need to pass the hex value of the keypress or see if the VK_ key mappings will work for you. Also be aware how WM_KEYDOWN messages are handled you may need to make up special WPARAM and LPARAM parameters in order to process Shift+ Ctrl+ and Alt+ keys.
I thought that I had tried that, but ti didn't work...

WM_KEYDOWN = 0x0100

or so I believe.

Then for instance to send the 'A' key, it would be: 0x0041 isnt it?

So then SendMessage(window, 0x0100, 0x0041, 0) should do it right? Well I cant seem to get it to work with Notepad for example.

Could you post a full example please?
Apr 24 '07 #9
Here you go:

The structure of a WM_KEYDOWN message: http://msdn2.microsoft.com/en-us/library/ms646280.aspx
Yes I have seen that, but honestly makes no sense whatsoever to me.

Is that the bit makeup of the 32bit integer?

so if I want to send it once, then bits 0-15 are <000000000000001>
and for the letter A it is for bits 16 through 23 <01000001>
and then not extended key so bit 24 is <0>
and 25-31 are all zero because some are reserved and the others must be down for a WM_KEYDOWN message.

So total would be <00000000010000010000000000000001> for the byte to send. That in decimal form is: 4259841 and hex would be 0x410001

so the total message would be SendMessage(Window, 0x100, 0x410001, 0)

or did I do something completely wrong?

I learn via examples, and I cant seem to find any full examples. Maybe code that take input from some text box and outputs it to notepad... Something simple for me to digest.

thanks for the responses so far.
Apr 24 '07 #10
RedSon
5,000 Expert 4TB
Actually its better to use PostMessage since it returns right away.

Expand|Select|Wrap|Line Numbers
  1. LRESULT PostMessage( HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
  2.  
  3. WM_KEYDOWN
  4.        WPARAM wParam
  5.         LPARAM lParam;
  6. Parameters
  7.     wParam
  8.         Specifies the virtual-key code of the nonsystem key. 
  9.     lParam
  10.         Specifies the repeat count, scan code, extended-key flag, 
  11.         context code, previous key-state flag, and transition-state flag, 
  12.         as shown in this table.
  13.  
  14. ......So......
  15.  
  16. LRESULT debugvalue = PostMessage( Handle_to_where_the_message_is_going,
  17.                                   WM_KEYDOWN, (WPARAM)VK_ENTER, (LPARAM)0)
  18.  
Apr 24 '07 #11
RedSon
5,000 Expert 4TB
Keep in mind that you might also want to send a WM_KEYUP message too.
Apr 24 '07 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: newsgroups.bellsouth.net | last post by:
I was checking out SAPI, ms's speech recognition api, and thought it would be cool to write a quick app for bf1942 that would recognise voice commands and pass them to the game's commun. So if you...
6
by: Daniel Kaffee | last post by:
Please, please, please can somebody post here how to use the Sendmessage API call to send the keystrokes Alt B, "Y", Tab (*7), "Danny" to an application that I have the windows handles and PID's...
8
by: g.franzkowiak | last post by:
Hello everybody, I've tryed to use an interprocess communication via SendMessage on Windows. Unfortunately, nothing goes on ...
3
by: Paul | last post by:
Hi all. Probably a quick one, I'm using PostMessage to send keys to an applicatoin but how do I convert the key string to a long values so that I can pass this to the wParam. The definition is...
3
by: Max M. Power | last post by:
When I use the SendMessage API I can sucessfully send and receive a user defined message. When I use the PostMessage API I can NOT sucessfully send and receive the same user defined message. ...
15
by: Necromis | last post by:
Ok, I am really losing it. I cannot seem to wrap my head around the SendMessage function/method. What I am trying to do is send text/data to an instance of a window that I know the handle of from...
3
by: deepthi82 | last post by:
Hi All, I'm trying to close a browser window either by sending Alt+f4 or Alt+f to open the file menu and then 'x' to exit and am trying to achieve this using win32api.SendMessage(). I tried all...
8
by: Dr1ZZ | last post by:
Hi, I'm currently working on a bot for a game. It works like this: 1: Take a picture of the current playing field 2: Do the calculations on what i gotta do (best move) 3: Use sendmessage...
2
by: alag20 | last post by:
Hi Guys, I need to send a string from c# application to another c# Application. On receiving application side the code i have is below. protected override void WndProc(ref Message m) ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.