SendMessage turn Off the monitor makes some system sleep in vb - vb.net

I have stuck at one place where I have to turn off the monitor and at background all my processes should work something like turn off the monitor only but cpu etc should work normally. Actually, my backend sends screenoFF command and accordingly I send a "SendMessage" command to off the monitor it is working fine on my laptop, but in production side system is going into sleep mode.
I would appreciate If anyone could help here thanks.
Private Const WM_SYSCOMMAND As Integer = &H112
Private Const SC_MONITORPOWER As Integer = &HF170
<DllImport("user32.dll")>
Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal hMsg As Integer,
ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
End Function
SendMessage(Me.Handle, WM_SYSCOMMAND, CType(SC_MONITORPOWER, IntPtr), CType(2, IntPtr))

Related

User32 sendmessage works only for some messages

I am looking to send key and mouse events to notepad.exe, however not necessarily have the window be active. In some cases the key events are actual typing operations and in other cases the key input represents shortcut key combinations.
Using SendKeys works, but obviously needs focus. I would prefer to have that operate in the background so am investivating SendMessage from user32.dll. I can see it has different signatures based on what the message code is, and this may be the origin of the problem.
Starting with
Dim notepad() As Process = Process.GetProcessesByName("notepad")
Dim actualWindow As IntPtr = FindWindowEx(notepad(0).MainWindowHandle, IntPtr.Zero, "Edit", Nothing)
and using
<System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint:="SendMessageA")>
Public Function SendMessage(ByVal hWnd As IntPtr, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As String) As IntPtr
End Function
if I send
SendMessage(actualWindow, KeyEventType.WM_SETTEXT, 0, "Text")
then the editor text is replaced with "Text".
If however I use
<System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint:="SendMessageA")>
Public Function SendMessage(ByVal hWnd As IntPtr, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As IntPtr
End Function
and send
SendMessage(actualWindow, KeyEventType.WM_KEYDOWN, &H4C, 0)
SendMessage(actualWindow, KeyEventType.WM_KEYUP, &H4C, 0)
or even
SendMessage(notepad(0).MainWindowHandle, KeyEventType.WM_KEYDOWN, &H4C, 0)
SendMessage(notepad(0).MainWindowHandle, KeyEventType.WM_KEYUP, &H4C, 0)
where &H4C corresponds to the 'L' key (1.), no letter 'l' appears in the editor.
I am moderately convinced that the function signatures are correct for both WM_KEYDOWN/WM_KEYUP (2.) and WM_SETTEXT (3.). I think the problem might be one of 'focus' but am not sure how to tackle that if I want the operations to occur in the background. How can keystrokes be sent to the text editor?
The enumerations are defined as:
Public Enum KeyEventType As Int32
WM_SETTEXT = &HC
WM_KEYDOWN = &H100
WM_KEYUP = &H101
End Enum

Mouse click in the background inside a running app

I’m making an VB application which runs another process (windowed game). My app acts as a macro to that game.
I have managed to write code that sends keyboard input into the process in the background without using appActivate, however I’m struggling to get mouse click working the same way.
I mean the program to click into specific location within the process in the background, that means I could be browsing internet or playing another game when that mouse click takes action.
Hope I make sense, can somebody bring me on right track?
This code I use to send Q letter into app in background:
Public Const WM_KEYDOWNLPARAM As System.UInt32 = &H320001
Public Const WM_KEYUPLPARAM As System.UInt32 = 3224502273
Public Const WM_KEYDOWN As System.Int32 = &H100
Public Const WM_KEYUP As System.Int32 = &H101
<System.Runtime.InteropServices.DllImport("user32.dll")>
Public Shared Function PostMessage(<System.Runtime.InteropServices.In()> ByVal hWnd As System.IntPtr, <System.Runtime.InteropServices.In()> ByVal Msg As System.UInt32, <System.Runtime.InteropServices.In()> ByVal wParam As System.IntPtr, <System.Runtime.InteropServices.In()> ByVal lParam As System.UIntPtr) As System.Boolean
End Function
Public Sub InputPressKey(ByVal Handle As System.IntPtr, ByVal Key As System.Windows.Forms.Keys)
PostMessage(Handle, WM_KEYDOWN, CType(Key, System.IntPtr), CType(WM_KEYDOWNLPARAM, System.UIntPtr))
End Sub
Public Sub InputReleaseKey(ByVal Handle As System.IntPtr, ByVal Key As System.Windows.Forms.Keys)
PostMessage(Handle, WM_KEYUP, CType(Key, System.IntPtr), CType(WM_KEYUPLPARAM, System.UIntPtr))
End Sub
InputPressKey(Process.MainWindowHandle, System.Windows.Forms.Keys.Q)

Whenever I want to turn off the monitor in Windows 8.1, this code turns my monitors off temporarily then turns back on to the lock screen

'send message implementation
SendMessage(Me.Handle.ToInt32, WM_SYSCOMMAND,
CType(SC_MONITORPOWER, IntPtr), CType(MonitorShutoff, IntPtr))
'declarations
Private Const MonitorShutoff As Integer = 2
Private Const WM_SYSCOMMAND As Integer = &H112
Private Const SC_MONITORPOWER As Integer = &HF170
<DllImport("user32.dll")> _
Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal hMsg As Integer, _
ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
End Function
Whenever I want to turn off the monitor in Windows 8.1, this code turns my monitors off temporarily then turns back on to the lock screen, how to i avoid the lock screen?
This is only an issue on Windows 8+ systems. I want my monitors to turn off and stay off. How do i do this?
I believe it is working correctly for you. It's just that your mouse is "moving" (or your keyboard is being "touched").

SensKeys on a window

i was programming an hack for a game in Visual basic, i want simulate a keypress with the code:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
SendKeys.Send("a")
End Sub
In my window game didn't work, but in other windows such as chrome or a test textbox work, why?
if this is not a flash game , you can get the process of the game by:
Dim TheGame as Process = Process.GetProcessesByName("YourGame")(0)
get the handle of the game window:
Dim Handle As IntPtr = TheGame.MainWindowHandle
and then use sendmessage function to send the keys to the window:
SendMessage(Handle, 258, PUT KEY HERE, 0)
for example - instead of the "PUT KEY HERE" type:
&H41 (is "A")
you can find the keys list here:
http://www.pinvoke.net/default.aspx/Enums/VirtualKeys.html
the user32.dll sendmessage function:
Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hWnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As IntPtr
Edit:
if the code is not working change the 258 (wm_char) in the sendmessage to 256(wm_keydown)
note:sendkeys sends keys to the current active window, and you should try to use pinvoke functions first and if you don't have any choice then use sendkeys.
Recommendation:
explore the functions: postmessage,sendmessage,setforegroundwindow and also explore spy++ tool :).

Vb.net: Send text to window via windows messages

I'm trying to make a program with vb.net (VS2008) for studio shooting with my Nikon D600.
I'm using a program called ControlMyNikon for tethered shooting and it's working perfectly.
It has this external control-feature with following instructions: http://i.jjj.fi/a9dAQ7z.png
Could someone give me hint what does 'send string to 'ControlMyNikon v4.1' window with windows messages' actually mean?
I tried with SendMessages.
I was able to change window title with WM_SETTEXT but that's all.
I'm able to get the window handle but don't know how to send any string to it.
Help? :)
Ugh, Nikon is pretty infamous for writing truly crappy shovelware. This does not disappoint. Here are some declarations that ought to work. Try the Unicode version first. If that produces Chinese text then use the Ansi version:
Imports System.Runtime.InteropServices
Class NativeMethods
Friend Const WM_SETTEXT As Integer = 12
<DllImport("user32.dll", EntryPoint:="SendMessageW", CharSet:=CharSet.Unicode)> _
Friend Shared Function SetWindowTextUnicode(ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As IntPtr, ByVal lParam As String) As IntPtr
End Function
<DllImport("user32.dll", EntryPoint:="SendMessageA", CharSet:=CharSet.Ansi)> _
Friend Shared Function SetWindowTextAnsi(ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As IntPtr, ByVal lParam As String) As IntPtr
End Function
End Class
Usage:
NativeMethods.SetWindowTextUnicode(handleYouFound, WM_SETTTEXT, IntPtr.Zero, "shoot")
If neither works then you are probably using the wrong window handle. Use Spy++ to double check that you located the window properly.