 |
Cheat Engine The Official Site of Cheat Engine
|
View previous topic :: View next topic |
Author |
Message |
lucidity Advanced Cheater
Reputation: 0
Joined: 16 Feb 2011 Posts: 91
|
Posted: Mon Jun 13, 2011 2:23 pm Post subject: VB/C# SendMessage? |
|
|
It is shameful to admit, but I've been trying to get SendMessage working for days. I've found a wide variety of suggested implementations all over the Internet but none which work, except some VB6 examples which I haven't been able to successfully port.
I want to be able to send input to a game with the game minimized, or unfocused. I do not want to foreground the game. If possible, I'd like to see it done with VB... but can't be picky at this point.
One point of confusion seems to be whether or not to use FindWindowEx to get the handle of child objects. To send input to a game, do I need any child handle? I've been trying to send to the parent window handle of the game.
Another thing no one really seem sure of is exactly HOW to send a keypress. Do you only send WM_CHAR? Do you send WM_KEYDOWN, WM_CHAR, then WM_KEYUP? Many people have said that WM_KEYDOWN sends down and up for them unexpectedly, but I haven't had any success sending -any- messages with SendMessage at all. Because SetForegroundWindow() works with the same handle, I believe my handle is good but something is happening with wParam, or privileges. Or I am a total idiot (don't want to rule anything out at this point).
My titlebar says "Microsoft Visual Basic 2010 Express (Administrator)" - does this meant the application is also running with administrator privileges at debug time? If not, could this be some kind of a system permission issue?
Notes: If I call SetForegroundWindow(WindowHandle) on my handle, it works. My problem really seems to be directly related to SendMessage.
When running Spy++ (or SpyXX?) I can't seem to send any messages to any window with either SendMessage or PostMessage. I want the functionality of SendMessage.
This is my best attempt at consolidating the code into one place, it may not be my exact current broken code; I have been testing with calculator because I thought it would be easier:
Code: |
Public Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As String) As Integer
Public Declare Function PostMessage Lib "user32.dll" Alias "PostMessageA" (ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Boolean
Public Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
Public Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" (ByVal hWnd1 As IntPtr, ByVal hWnd2 As IntPtr, ByVal lpsz1 As String, ByVal lpsz2 As String) As IntPtr
Public Declare Function GetWindowThreadProcessId Lib "user32.dll" (ByVal hwnd As IntPtr, ByRef lpdwProcessId As IntPtr) As IntPtr
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As IntPtr) As Long
Const WM_KEYDOWN As Integer = &H100
Const WM_KEYUP As Integer = &H101
Const WM_CHAR As Integer = &H102
Const WM_COMMAND As Integer = &H111
Const VK_SPACE = &H20
Const VK_RETURN = &H1C
Public Sub New(ByVal PIDHandle As IntPtr)
Dim hTMP As IntPtr = FindWindow(vbNullString, "Calculator")
Dim pID As IntPtr
Dim threadID As IntPtr = GetWindowThreadProcessId(hTMP, pID)
Do While hTMP <> IntPtr.Zero And PIDHandle <> pID
hTMP = FindWindow(vbNullString, "Calculator")
threadID = GetWindowThreadProcessId(hTMP, pID)
Loop
WindowHandle = hTMP
' Doesn't work with or without this line, but WindowHandle does match the
' appropriate Spy++ parent or child window depending on whether or not this is called.
WindowHandle = FindWindowEx(WindowHandle, IntPtr.Zero, vbNullString, vbNullString)
' These two lines *will* bring the window to the foreground and press enter
'Dim lngRetVal As Long = SetForegroundWindow(WindowHandle)
'SendKeys.Send("{ENTER}")
' Everything up to this point works. This is where the problem begins.
' None of the following 3 sets of commands will work
' VARIANT 1:
'SendMessage(WindowHandle, WM_CHAR, Keys.Return, 0)
' VARIANT 2:
'SendMessage(WindowHandle, WM_KEYDOWN, Keys.Return, Nothing)
'SendMessage(WindowHandle, WM_CHAR, Keys.Return, Nothing)
'SendMessage(WindowHandle, WM_KEYUP, Keys.Return, Nothing)
' VARIANT 3:
'SendMessage(WindowHandle, WM_COMMAND, Keys.Return, Nothing)
End Sub
|
Someone please either post an example of how this is done, or put me out of my misery. If a C# answer can be made available but not a VB solution, I would gladly jump ship to C#...
_________________
» Antec Twelve Hundred Full Tower » EVGA E760 CLASSIFIED » EVGA GeForce GTX 580 SuperClocked 1536MB » i7-980XE » CORSAIR DOMINATOR 6GB PC3 12800 DDR3 » OS: Intel X25-M SSD » Game Storage: Raid-0 2 x WD VelociRaptor 10000 RPM » CE Cache Drive: 500GB WD Caviar » Power Supply: OCZ Z Series Gold 1000W |
|
Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8585 Location: 127.0.0.1
|
Posted: Mon Jun 13, 2011 3:55 pm Post subject: |
|
|
Granted its just API it will work in either VB.NET or C#. Are you sure the program responds to the messages you are even trying to send? Does it use DirectInput or another method of handling keyboard input?
WM_KEYDOWN sends the WM_CHAR message to the focused window. Depending on the lParam passed in the call you can alter how the key is handled.
WM_CHAR:
http://msdn.microsoft.com/en-us/library/ms646276%28v=vs.85%29.aspx
WM_KEYDOWN:
http://msdn.microsoft.com/en-us/library/ms646280%28v=vs.85%29.aspx
You may want to look into SendInput though to attempt to inject keyboard activity. Keep in mind when you inject keys in userland, the LLKHF_INJECTED flag gets set in the hook structure which a process can use to determine if the input is faked or not.
_________________
- Retired. |
|
Back to top |
|
 |
lucidity Advanced Cheater
Reputation: 0
Joined: 16 Feb 2011 Posts: 91
|
|
Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8585 Location: 127.0.0.1
|
Posted: Mon Jun 13, 2011 8:26 pm Post subject: |
|
|
Typically looking at the imports gives away if the app uses DirectInput or not if it includes dinput.dll. Not to say this is 100% guaranteed to be used though.
_________________
- Retired. |
|
Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
|
|