473,654 Members | 3,114 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

18 New Member
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 48031
NVergunst
18 New Member
Anyone? Bueler, bueler?
Apr 23 '07 #2
NVergunst
18 New Member
Doe nobody know how to send keystrokes through the windows API?
Apr 23 '07 #3
RedSon
5,000 Recognized Expert Expert
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 Recognized Expert Expert

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 Recognized Expert Expert
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 Recognized Expert Expert
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
NVergunst
18 New Member
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
NVergunst
18 New Member
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(win dow, 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
NVergunst
18 New Member
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 <00000000000000 1>
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 <00000000010000 010000000000000 001> for the byte to send. That in decimal form is: 4259841 and hex would be 0x410001

so the total message would be SendMessage(Win dow, 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

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

Similar topics

0
5791
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 say "roger" it would then send f1 f1 to bf1942 for it to send out the roger commun..I'm finding out that directinput is a bitch, uses a low level, so it ignores Sendkeys and sendmessage/postmessage. I've completed everything except sending the...
6
38908
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 for. It's driving me nuts. Everyone who asks the question about Sendkeys gets told to use the Sendmessage API but nobody tells them actually how to do it!! Any help will be gratefully received. Thanks in advance
8
6677
by: g.franzkowiak | last post by:
Hello everybody, I've tryed to use an interprocess communication via SendMessage on Windows. Unfortunately, nothing goes on ######################################################################### #! /usr/bin/env python import win32api, win32ui, win32con
3
4465
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 below Private Declare Auto Function PostMessage Lib "user32" ( _ ByVal hWnd As IntPtr, _ ByVal Msg As Integer, _ ByVal wParam As Long, _
3
5083
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. I've got a C# class library project with two classes: Class 1 is derives from : System.Windows.Forms.Form and overrides the base WndProc method for the purpose of receiving and handeling user defined messages:
15
10365
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 within a vb.net application. Can someone give me a decent example of the code I would need to create to process this using the sample data below. Windows hwnd = 0xB0484 String text = "NM*14*ACCTNO*MMDDYY"
3
9146
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 combinations of WM_KEYDOWN, WM_SYSKEYDOWN, WM_CHAR, VK_MENU and VK_F4 but couldn't get this. I'm stuck on this one since two days and I really appreciate any help. I can't use sendkeys since this window is not always in focus. I can't also use...
8
1714
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 to send a mouseclick to the game at the correct position
2
5509
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) { switch (m.Msg)
0
8379
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8816
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8709
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8494
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7309
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2719
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1924
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.