-
Member
Originally Posted by
xzidez
SendMessage works just fine. However you must be sending it till the handle of the window. Not the wow process itself.
In other words the handle you will get if you use FindWindow(null, "World of Warcraft");
When I start WoW process using the class Process from the .NET framework, I have an attribute in this class named MainWindowHandle. I can handle the window with this pointer I guess. I don't think I need to use FindWindow.
So, after the WoW process starts, I use WaitForInputIdle and I start to send messages through PostMessage. I tried using SendMessage with no success, so I stick with PostMessage. If I use FindWindow, will I have another result? Is it the same handle that I get through MainWindowHandle?
Last edited by schifers; 10-06-2009 at 11:45 AM.
-
Member
AFAIK, the only real difference between SendMessage() and PostMessage() is that PostMessage is asynchronous and SendMessage is synchronous. Have you tried keybd_event() or SendInput()?
-
Member
Originally Posted by
schifers
When I start WoW process using the class Process from the .NET framework, I have an attribute in this class named MainWindowHandle. I can handle the window with this pointer I guess. I don't think I need to use FindWindow.
So, after the WoW process starts, I use WaitForInputIdle and I start to send messages through PostMessage. I tried using SendMessage with no success, so I stick with PostMessage. If I use FindWindow, will I have another result? Is it the same handle that I get through MainWindowHandle?
Code:
[DllImport("user32.dll", SetLastError = true)]
static extern int FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int SendMessage(int hWnd, int msg, int wParam, int lParam);
private const int WM_KEYDOWN = 0x100;
private const int WM_KEYUP = 0x101;
and then use
Code:
int hWnd = FindWindow(null, "World of Warcraft");
SendMessage(hWnd, WM_KEYDOWN, (int)Keys.J, 0);
SendMessage(hWnd, WM_KEYUP, (int)Keys.J, 0);
That FindWindow is really ugly. However I just tried this code so it should work. If the hWnd you get here is the same hWnd you have in your MainWindowHandle it should work just aswel with that one :P
-
Member
Originally Posted by
RiotInferno
AFAIK, the only real difference between SendMessage() and PostMessage() is that PostMessage is asynchronous and SendMessage is synchronous. Have you tried keybd_event() or SendInput()?
I didn't, because you need to have the focus on the window. I'm starting to program a multi-box bot, so I can't depend on that limitation. I need to send keys to the background windows too.
Originally Posted by
xzidez
That FindWindow is really ugly. However I just tried this code so it should work. If the hWnd you get here is the same hWnd you have in your MainWindowHandle it should work just aswel with that one :P
It's the same handle, I just made a test. When I use FindWindow, it brings the same window handle if I use the MainWindowHandle from the Process class from .NET framework.
Then I tried again to use SendMessage and one funny thing just happened. I sent some keys as I was typing my username. Between these keypresses, I made a random sleep from 0-100 ms. I made it just to simulate the lag we have between the key presses that human beings normally have. My window didn't receive the chars when I used SendMessage, but after the username, I sent a "\t" (VK_TAB), and the cursor changed from the username box to the password box. Then I sent the password and no success again, the chars didn't appeared as dots in the password field, but just after the password, I sent the char "\r" (VK_RETURN) and the program received it and it tried to login. So, the WoW main window got the tab and the return, but it didn't get the chars from username and password. I still stick with PostMessage.
-----------------
I MADE IT!!!
I was able to send the "#" key to WoW login window using the WM_CHAR message and sending AscII code in the third parameter.
Code:
PostMessage(hWnd, (Int32)WM_CHAR, key, IntPtr.Zero);
Thanks everybody... Next step I will try to choose one of the chars in the chars list. Cya all!!!
Last edited by schifers; 10-07-2009 at 08:42 AM.
-
Post Thanks / Like - 1 Thanks
LogicWin (1 members gave Thanks to schifers for this useful post)
-
Active Member
Here you go.
Code:
static class Messaging
{
#region Imports
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
private static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam,
IntPtr lParam);
#endregion
private static bool PostMessageSafe(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
{
bool returnValue = PostMessage(hWnd, msg, wParam, lParam);
if (!returnValue)
{
return false;
}
return true;
}
public static bool SendKey(char key, IntPtr hWnd)
{
PostMessageSafe(hWnd, (uint)WM.WM_Keydown, (IntPtr)key, IntPtr.Zero);
PostMessageSafe(hWnd, (uint)WM.WM_Keyup, (IntPtr)key, IntPtr.Zero);
return true;
}
public static bool SendKey(VK key, bool upordown, IntPtr hWnd)
{
if (upordown)
return PostMessageSafe(hWnd, (uint)WM.WM_Keydown, (IntPtr)key, IntPtr.Zero);
else
return PostMessageSafe(hWnd, (uint)WM.WM_Keyup, (IntPtr)key, IntPtr.Zero);
}
public static bool SendString(string message, IntPtr hWnd)
{
try
{
char[] keys = message.ToCharArray();
foreach (char c in keys)
{
SendKey(c, hWnd);
}
}
catch (Exception)
{ return false; }
return true;
}
private enum WM
{
WM_Keydown = 0x100,
WM_Keyup = 0x101,
WM_Char = 0x102
}
public enum VK
{
VK_LEFT = 0x25,
VK_RIGHT = 0x27,
VK_UP = 0x26,
VK_DOWN = 0x28
}
}
Have some basic VK codes in there since this was mainly for movement.
-
Member
Originally Posted by
schifers
I MADE IT!!!
I was able to send the "#" key to WoW login window using the WM_CHAR message and sending AscII code in the third parameter.
Code:
PostMessage(hWnd, (Int32)WM_CHAR, key, IntPtr.Zero);
Thanks everybody... Next step I will try to choose one of the chars in the chars list. Cya all!!!
Glad to see it worked out!