473,672 Members | 2,629 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

User32.dll

I'm trying to figure out the fastest/easiest way of sending text to another
application. I've got the SendInput and keybd_event methods working well
enough... is one better than the other? Is there something else I should try
out? Is there a way to send text to another window without giving that
window focus? Below is the code I'm using.

Thanks in advance.
*************** **************
[DllImport("user 32.dll")]
static extern uint SendInput(uint nInputs, ref INPUT pInputs, int
cbSize);

[StructLayout(La youtKind.Explic it)]
public struct INPUT
{
[FieldOffset(0)]
public int type;
[FieldOffset(4)]
MOUSEINPUT mi;
[FieldOffset(4)]
public KEYBDINPUT ki;
[FieldOffset(4)]
HARDWAREINPUT hi;
}

struct MOUSEINPUT
{
int dx;
int dy;
int mouseData;
int dwFlags;
int time;
IntPtr dwExtraInfo;
}

public struct KEYBDINPUT
{
public char wVk;
public short wScan;
public int dwFlags;
public int time;
public IntPtr dwExtraInfo;
}

struct HARDWAREINPUT
{
int uMsg;
short wParamL;
short wParamH;
}

[DllImport("user 32.dll")]
static extern short VkKeyScan(char ch);

[DllImport("user 32.dll")]
static extern void keybd_event(byt e bVk, byte bScan, uint dwFlags,
UIntPtr dwExtraInfo);

[DllImport("user 32.dll", EntryPoint = "FindWindow ")]
private static extern int FindWindow(stri ng _ClassName, string
_WindowName);

[DllImport("User 32.dll")]
public static extern Int32 SetForegroundWi ndow(int hWnd);

public const int KEYEVENTF_KEYUP = 0x2;

static void Main(string[] args)
{
int i = FindWindow("Not epad", null);
SetForegroundWi ndow(i);
string str1 = "CRAIG";
for (int j = 0; j < str1.Length-1; j++)
{
PressKey((char) str1[j]);
}
for (int j = 0; j < str1.Length; j++)
{
PressKey2(str2[j]);
}
}
}
}

public static void PressKey2(char keyCode)
{
INPUT mInput = new INPUT();
KEYBDINPUT ki = new KEYBDINPUT();
mInput.type = 1;
mInput.ki = ki;
mInput.ki.wVk = keyCode;
mInput.ki.dwFla gs = 0;
unsafe
{
SendInput(1, ref mInput, sizeof(INPUT));
}

mInput.type = 1;
mInput.ki.wVk = keyCode;
mInput.ki.dwFla gs = KEYEVENTF_KEYUP ;
unsafe
{
SendInput(1, ref mInput, sizeof(INPUT));
}
}

public static void PressKey(char keyCode)
{
const int KEYEVENTF_KEYUP = 0x2;
keybd_event((by te)VkKeyScan(ke yCode), 0x9e, (uint)0, (UIntPtr)0);
keybd_event((by te)VkKeyScan(ke yCode), 0x9e,
(uint)KEYEVENTF _KEYUP, (UIntPtr)0);
}
Nov 16 '05 #1
1 15062
Hi Craig,

You can also use the SendMessage API to send messages like WM_CHAR or
WM_KEYDOWN to the application. I am not sure though they will be correctly
processed without the target window having input focus.

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

"Craig" <Cr***@discussi ons.microsoft.c om> wrote in message
news:61******** *************** ***********@mic rosoft.com...
I'm trying to figure out the fastest/easiest way of sending text to another application. I've got the SendInput and keybd_event methods working well
enough... is one better than the other? Is there something else I should try out? Is there a way to send text to another window without giving that
window focus? Below is the code I'm using.

Thanks in advance.
*************** **************
[DllImport("user 32.dll")]
static extern uint SendInput(uint nInputs, ref INPUT pInputs, int
cbSize);

[StructLayout(La youtKind.Explic it)]
public struct INPUT
{
[FieldOffset(0)]
public int type;
[FieldOffset(4)]
MOUSEINPUT mi;
[FieldOffset(4)]
public KEYBDINPUT ki;
[FieldOffset(4)]
HARDWAREINPUT hi;
}

struct MOUSEINPUT
{
int dx;
int dy;
int mouseData;
int dwFlags;
int time;
IntPtr dwExtraInfo;
}

public struct KEYBDINPUT
{
public char wVk;
public short wScan;
public int dwFlags;
public int time;
public IntPtr dwExtraInfo;
}

struct HARDWAREINPUT
{
int uMsg;
short wParamL;
short wParamH;
}

[DllImport("user 32.dll")]
static extern short VkKeyScan(char ch);

[DllImport("user 32.dll")]
static extern void keybd_event(byt e bVk, byte bScan, uint dwFlags,
UIntPtr dwExtraInfo);

[DllImport("user 32.dll", EntryPoint = "FindWindow ")]
private static extern int FindWindow(stri ng _ClassName, string
_WindowName);

[DllImport("User 32.dll")]
public static extern Int32 SetForegroundWi ndow(int hWnd);

public const int KEYEVENTF_KEYUP = 0x2;

static void Main(string[] args)
{
int i = FindWindow("Not epad", null);
SetForegroundWi ndow(i);
string str1 = "CRAIG";
for (int j = 0; j < str1.Length-1; j++)
{
PressKey((char) str1[j]);
}
for (int j = 0; j < str1.Length; j++)
{
PressKey2(str2[j]);
}
}
}
}

public static void PressKey2(char keyCode)
{
INPUT mInput = new INPUT();
KEYBDINPUT ki = new KEYBDINPUT();
mInput.type = 1;
mInput.ki = ki;
mInput.ki.wVk = keyCode;
mInput.ki.dwFla gs = 0;
unsafe
{
SendInput(1, ref mInput, sizeof(INPUT));
}

mInput.type = 1;
mInput.ki.wVk = keyCode;
mInput.ki.dwFla gs = KEYEVENTF_KEYUP ;
unsafe
{
SendInput(1, ref mInput, sizeof(INPUT));
}
}

public static void PressKey(char keyCode)
{
const int KEYEVENTF_KEYUP = 0x2;
keybd_event((by te)VkKeyScan(ke yCode), 0x9e, (uint)0, (UIntPtr)0); keybd_event((by te)VkKeyScan(ke yCode), 0x9e,
(uint)KEYEVENTF _KEYUP, (UIntPtr)0);
}


Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
6678
by: S. van Beek | last post by:
Drear reader, I can't find the library Lib "user32" in my VBA available references. What can be wrong or which library has to be selected to run API code properly.
1
6972
by: Richard A. Lowe | last post by:
I'm successfully using SendInput to emulate mouse events for a legacy app, delcared like so: public extern static int SendInput( int theCount, ref INPUT pInputs, int theSize ); From the MSDN docs, SendInput appears to take an array of inputs:
5
1491
by: Will Pittenger | last post by:
The constants.windowLongIndices type is an enum. That is there so I can ensure the appropriate values are always used. Since I had to replace the GWL macros with new values, I decided to prevent passing 7 -- or whatever the author of the caller felt like. I tried both ExactSpelling=true and setting the entry point manually. I also tried forcing the Unicode and ASCII versions. (According to the Win32 documentation, both functions have...
1
2853
by: TusharP | last post by:
Hi Friends, Is there any similar framework dlls for gdi32.dll, used32.dll. I want to use following functions 1) GetPixel 2)SetWindowRgn 3)CreateRectRgn 4)CombineRgn 5)SendMessage 6)ReleaseCapture 7)DeleteObject
1
2002
by: r | last post by:
Hi, Could someone please direct me in the right direction? I would like to p/invoke few functions from user32: DrawAnimatedRects FindWindow, FindWindowEx , GetWindowRect struct RECT how do you usually get the functions headers, how do you know how to
0
1112
by: Robin Day | last post by:
Hi, I am looking to (hopefully quite simply) populate a drop down list with a list of the currently open windows (applications) and their respective HWND's I have found various snippets using GetWindow and FindWindow from user32.dll but have been unable to get it all working. Any help / code examples would be much appreciated. Preferably in C# but any examples would be useful.
2
26581
by: Juan Martinez | last post by:
In a form i have a drawing control called VectorDraw and a textbox. The text box is disable, when i am using the drawing area i can write some text, what i want to do is that when i press any key that key show in the textbox, or if other control has the focus and i press other key also show in the textbox. I saw some info about this using the user32.dll like this: static extern bool TranslateMessage( ref Message lpMsg);
2
12141
by: Dan | last post by:
Sorry if this post may sound a bit off-topic, but I could not find a specialized newsgroup. I have created a dotnet 2.0 library containing some user controls and written in C#. One of the controls (an extension of RichTextControl) needs to programmatically change the keyboard layout, so it imports some user32.dll API functions (see below). This control works fine in Windows XP, but it crashes under Win64 systems when trying to call the...
2
3305
by: =?Utf-8?B?U3RldmVTNjA=?= | last post by:
I apparently made an unknown error. Now when I attempt to install two software packages I receive error messages. One package results in the message "The procedure entry point CallWindowProMF could not be located in the dynamic link library user32.dll. Attempting to install the other package results in exactly the same error message, but the procedure entry point is DefWindowProMF. In Safe Mode I renamed user32.dll, then attempted to...
0
8506
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
8423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8852
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...
0
7482
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
6261
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
4447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2847
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
2098
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1843
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.