1

Im new at programming.

im going to make a program wich can send inputs/text to a game called minecraft - its a game made in Java.

im trying to use the SendMessage API but i dont know how to use it..

this is my code so far:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace MinecraftTest2_Sendinput
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        [DllImportAttribute("User32.dll")]
        private static extern int FindWindow(String ClassName, String
        WindowName);

        [DllImportAttribute("User32.dll")]
        private static extern int SetForegroundWindow(int hWnd);

        [System.Runtime.InteropServices.PreserveSig]
        [DllImport("User32.dll", CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Ansi)]
        public static extern int SendMessage(int hWnd, uint Msg, int wParam, long lParam);


        private void button1_Click(object sender, EventArgs e)
        {
            int hWnd = FindWindow(null, "Minecraft");
            if (hWnd > 0)
            {
                SetForegroundWindow(hWnd);
                //I need to call the SendMessage here! but what should i type in the arguments? 
            }

        }
    }
}
Paul Bellora
  • 54,340
  • 18
  • 130
  • 181
Mathias
  • 65
  • 1
  • 2
  • 6
  • i really need to press Enter then afterwards send some text and press enter agin - how do i manage to do that? ive been looking and msdn but i can't find the right solution.. – Mathias Jun 12 '11 at 23:14
  • When Windows delivers mouse and keyboard messages to a window, they are *posted*, not *sent*. – Ben Voigt Jun 12 '11 at 23:32

1 Answers1

1

This depends on the message you want to send to the window. A complete list of Windows Messages is available here and here . the wParam and lParam are message dependent, they act as parameters for the message being sent to the window message queue.

Here is a small snippet to send Left Mouse Button click message to the window The params are both null.

 int WM_LBUTTONDOWN = &H201;
 int WM_LBUTTONUP = &H202;
 SendMessage(hWnd, WM_LBUTTONDOWN, IntPtr.Zero, IntPtr.Zero); // Mouse Down
 SendMessage(hWnd, WM_LBUTTONUP, IntPtr.Zero, IntPtr.Zero); // Mouse Up

if for example you want to inform the window that the Control key is pressed, just use MK_CONTROL for wParam, if you want to specify the coordinates, use the lParam like so

 int lParam = X + Y<<16;
 SendMessage(hWnd, WM_LBUTTONDOWN, IntPtr.Zero, lParam); // Mouse Down
Waleed
  • 3,105
  • 2
  • 24
  • 31
  • that sounds nice! the actual thing i would like to send is: /give name 100 100 {Enter} - how would i be able to do that then? – Mathias Jun 12 '11 at 23:58
  • take a look at this http://www.codeproject.com/KB/vb/Send_string_by_message.aspx, the code is VB but you can translate it to C# using available online converters – Waleed Jun 13 '11 at 00:00