[Help] C# SendMessage()

05/09/2010 06:41 edwin_j77#1
I'm currenty trying to use the SendMessage Function with CO. According to msdn I need to use this:
Code:
[DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
My question is: How do I find the hWnd? What are the wParam and lParam? And what can I use as wParam and lParam? Any help will be greatly appreciated.
05/09/2010 08:54 Ian*#2
hWnd is the handle, you need to get this, here's how you go about doing that, you can change the variable names obviously.
First you have to import FindWindow and SendMessage
Code:
 [DllImport("user32.dll")]
         public static extern int FindWindow(
         string lpClassName, // class name
         string lpWindowName // window name
         );
 [DllImport("user32.dll")]
         public static extern int SendMessage(
         int hWnd, // handle to destination window
         uint Msg, // message
         int wParam, // first message parameter
         int lParam // second message parameter
         );
then actually declaring your handle and implementing it, probably would be using an if statement if you're trying to accomplish an auto hunter or something like that.
Code:
 if (condition) {
 int coHandle = FindWindow(null,"[Conquer] Legends Return"); //It might not be legends return can't  remember off the top of my head lol
 int msgParam = 0; //Message to send, like the function
 int smParam1 = 0; //First parameter
 int smParam2 = 0; //Second parameter
 SendMessage(coHandle, msgParam, smParam1, smParam2);
}
Not sure what you're trying to accomplish exactly but that should answer your question.
and if you keep the smParam1/2 set to 0 it'll work fine as long as the msgParam performs everything needed.
05/10/2010 00:52 SigmaD#3
First of all thanks for replying. I'm edwin_j77(the one who created the thread), I was using my old computer and didn't realize my old account was logged in =p Anyways, as my signature says, I'm trying to do a skill leveler, so I need to send right clicks and some other keys.

For the handle I made a loop that searches processes for the string "Conquer" and puts them in a listbox so I can see them. Then I set the handle to whatever value is selected on the listbox. Your method might work too(I haven't tried it), however, if I have more than one Conquer open at once then I can't choose which one I'll send the message to.

As for the lParam and wParam, I saw on an example I found they used 0 too, however I also read on another page that you should never use int as lParam or wParam because that will cause the program to crash on 64-bit windows. It said to use IntPtr but 0 is an int so It won't let me use it. I don't use 64-bit windows so this isn't a big problem for now. The problem I have now is that I can't send the message. I'm currently using this:
Code:
 public const UInt32 VK_F10 = 0x79;
and using VK_F10 as the message. So far I haven't been successful :( Any ideas as to what I'm doing wrong?

EDIT: Found out I can use IntPtr.zero to set the lParam and wParam to 0.
05/10/2010 03:35 Ian*#4
Hm, it should work.

Are you positive that you're getting the correct handle?
Give my method a try
05/10/2010 03:46 SigmaD#5
Quote:
Originally Posted by Ian* View Post
Hm, it should work.

Are you positive that you're getting the correct handle?
Give my method a try
Well I tried your method and it didn't work either =\ also I sent the value of the handle using your method to a label and compared it to the value I got using my method and they were exactly the same so the handle isn't the problem.
This is what I'm using:
Code:
 [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);

        public const UInt32 VK_F10 = 0x79; //0x79 is the value of the F10 key according to MSDN
Code:
SendMessage(coHandle, VK_F10, IntPtr.Zero, IntPtr.Zero);
05/10/2010 06:33 gabrola#6
Background keyboard clicks won't work in CO, get the coordinates of the F10 box and right click it instead
05/10/2010 06:41 Ian*#7
Or hook the f key function :p
05/10/2010 16:25 SigmaD#8
That would explain why it didn't work... lol so how do I choose what coordinates I want it to send the click? I tried using wParam and lParam as x and y but it didn't work.
I'm using these as the messages:
Code:
public const UInt32 WM_RBUTTONDOWN = 0x0204;
public const UInt32 WM_RBUTTONUP = 0x0205;
Also, should I use hex or decimal for the message values?
05/10/2010 17:42 Nullable#9
1- Hex or decimal doesn't matter, they are all numbers after all.
2- The messages that you included, use WPARAM as a virtual key included with click:
Code:
MK_CONTROL

    The CTRL key is down.

MK_LBUTTON

    The left mouse button is down.

MK_MBUTTON

    The middle mouse button is down.

MK_RBUTTON

    The SHIFT key is down.

MK_XBUTTON1

    The first X button is down.

MK_XBUTTON2

    The second X button is down.
3- LPARAM contains the x-coord and y-coord as low-order and high-order, where x is the low-order value and y is the high-order value.
05/11/2010 09:05 SigmaD#10
How do I assign two values to a single parameter?
05/11/2010 09:25 Warlax#11
Quote:
Originally Posted by SigmaD View Post
How do I assign two values to a single parameter?
use a dictonary or a list or an array or a or a string with a seperator

but then ask urself, are u sure u want 2 values on a single parameter?
05/11/2010 13:02 shimo diaz#12
LOL I am trying to make a skill leveler too but I am using a different function SendInput I can make clicks and keystrokes but I can't specify a window.
05/11/2010 14:36 Nullable#13
Quote:
Originally Posted by Warlax View Post
use a dictonary or a list or an array or a or a string with a seperator

but then ask urself, are u sure u want 2 values on a single parameter?
Lol, he meant how to set the hibyte and lobyte of the LPARAM.
Anyways, there is this macro
Quote:
#define MAKELONG(a, b) ((LONG)(((WORD)(((DWORD_PTR)(a)) & 0xffff)) | ((DWORD)((WORD)(((DWORD_PTR)(b)) & 0xffff))) << 16))
05/11/2010 15:28 shimo diaz#14
@SigmaD
Try MSDN forums
[Only registered and activated users can see links. Click Here To Register...]
05/11/2010 15:59 SigmaD#15
Quote:
Originally Posted by Nullable View Post
Lol, he meant how to set the hibyte and lobyte of the LPARAM.
Anyways, there is this macro
I have no idea how to use that lol but thanks anyways. I'll look for info on it =p
Quote:
Originally Posted by shimo diaz View Post
@SigmaD
Try MSDN forums
[Only registered and activated users can see links. Click Here To Register...]
I didn't know they had forums lol thanks