I have read the other post about this topic but I don't fully understand it. I tried to make my own and I get "No overload for method 'SendMessage' takes '4' arguments" error. I have 1 button on my program and I want it to put some text into note pad. I am just trying this cause I'm making a program that holds my cd-keys for my games and other programs and places them in the boxes when I'm installing the game. What am I doing wrong?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsApplication4
{
public partial class Form1 : Form
{
[DllImport("User32.dll")]
public static extern Int32 SendMessage(String um,String textToSend);
private void button1_Click(object sender, EventArgs e)
{
string z = "TEXT";
const int WM_SETTEXT = 0x0C;
SendMessage(Handle, WM_SETTEXT, 4, z);
}
}
}
I tried to read some stuff about the arugment erros but I coulden't find anything that would help.
Loading
Cork KaliszewskiPosted Dec 8, 2007, 11:59 AM
public Form1()
{
InitializeComponent();
}
What would be another example? I have tried to put some text into WordPad but it just puts it into notepad, I have:
private void button1_Click_1(object sender, EventArgs e)
{
string lpClassName = "WordPad";
string lpWindowName = "Document - WordPad";
IntPtr hWnd = FindWindow(lpClassName, lpWindowName);
SetForegroundWindow(hWnd);
string z = "TEXT";
const int WM_SETTEXT = 0x0C;
SendMessage(hWnd, WM_SETTEXT, IntPtr.Zero, z);
SendKeys.Send("TEXT");
}
and sometimes it just sends it to notepad and sometimes it does nothing at all. What do I have to put for the class name? Does it have to be the name of the exe that is running or just what the default name is?
AlanPosted Dec 8, 2007, 8:06 AM
OK, let's start from scratch:
1. Create a new Windows Forms application and drag a button onto it (which will be called button1 by default) from the ToolBox.
2. In the form designer, double click on the button and the button1_Click eventhandler will open up. Now paste the appropriate code into that. Next scroll up a bit and add the 3 DllImport declarations (these should be within the Form class but outside of any methods). Then add this line to your using directives:
using System.Runtime.InteropServices;
3. Now build the application - you shouldn't get any errors - and open up a new blank instance of notepad outside Visual Studio.
5. Finally go back to Visual Studio, run the program and click the button. With luck, you should now see some activity in notepad.
Cork KaliszewskiPosted Dec 7, 2007, 10:43 PM
AlanPosted Dec 7, 2007, 7:00 PM
To use this code from a Windows Forms application all the code needs to be in the Form class itself, not the Main() method. You should also be able to use SendKeys.Send() rather than SendKeys.SendWait() in such an application:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsApplication4
{
public partial class Form1 : Form
{
[DllImport("User32.dll")]
static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, string lParam);
[DllImport("user32.dll")]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);
private void button1_Click(object sender, EventArgs e)
{
string lpClassName = "Notepad";
string lpWindowName = "Untitled - Notepad";
IntPtr hWnd = FindWindow(lpClassName, lpWindowName);
SetForegroundWindow(hWnd);
string z = "TEXT";
const int WM_SETTEXT = 0x0C;
SendMessage(hWnd, WM_SETTEXT, IntPtr.Zero, z);
SendKeys.Send("Hello{ENTER}World");
}
// other code
}
}
Cork KaliszewskiPosted Dec 7, 2007, 4:49 PM
"
AlanPosted Dec 7, 2007, 1:44 PM
As Ryan said, the Win32 API function SendMessage() takes 4 parameters, though the types of these parameters depends on which Windows message you're using.
You also need to get the window handle and force focus onto it before you can send the WM_SETTEXT message which actually prints text to the window header. To print text within the window body, you can use the managed SendKeys class which sends keys to the application just as if they'd been typed at the keyboard.
If you start a new blank instance of Notepad and run the following console application, you'll see what I mean:
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
class Test
{
[DllImport("User32.dll")]
static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, string lParam);
[DllImport("user32.dll")]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);
static void Main()
{
string lpClassName = "Notepad";
string lpWindowName = "Untitled - Notepad";
IntPtr hWnd = FindWindow(lpClassName, lpWindowName);
SetForegroundWindow(hWnd);
string z = "TEXT";
const int WM_SETTEXT = 0x0C;
SendMessage(hWnd, WM_SETTEXT, IntPtr.Zero, z);
SendKeys.SendWait("Hello{ENTER}World");
}
}
Ryan AlfordPosted Dec 7, 2007, 10:08 AM
[DllImport("User32.dll")]
public static extern Int32 SendMessage(String um,String textToSend);
private void button1_Click(object sender, EventArgs e)
{
string z = "TEXT";
const int WM_SETTEXT = 0x0C;
SendMessage(Handle, WM_SETTEXT, 4, z);
}
}
in the RED statement, you specify a method that accepts 2 parameters. but in the GREEN statement, you are trying to send 4 parameters to that method.
to fix, either change the declaration of the method to accept 4 parameters, or change the code where you call the method and send only 2 parameters.