Advertisement
BrU32

VB.NET Windows API Example

Mar 10th, 2017
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.26 KB | None | 0 0
  1. Imports System.Runtime.InteropServices
  2.  
  3. Public Class Form1
  4. 'SendMessage API Declaration:
  5.  
  6. Private Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As IntPtr, ByVal lParam As String) As IntPtr
  7. Private Declare Function ShowWindow Lib "user32.dll" (ByVal hwnd As IntPtr, ByVal nCmdShow As IntPtr) As IntPtr
  8. 'FindWindowEx API Declaration:
  9.  
  10. Private Const WM_GETTEXT As Integer = &HD
  11.  
  12.  
  13. <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
  14. Private Shared Function FindWindowEx(ByVal parentHandle As IntPtr, _
  15. ByVal childAfter As IntPtr, _
  16. ByVal lclassName As String, _
  17. ByVal windowTitle As String) As IntPtr
  18. End Function
  19.  
  20. 'FindWindow API Declaration:
  21.  
  22. <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
  23. Private Shared Function FindWindow( _
  24. ByVal lpClassName As String, _
  25. ByVal lpWindowName As String) As IntPtr
  26. End Function
  27.  
  28. Const WM_SETTEXT As Long = &HC 'WM_SETTEXT Constant. For setting text to objects in Windows applications.
  29. Const WS_MINIMIZE As Integer = &H20000000
  30. Const WM_CLOSE = &H10 'WM_CLOSE Constant. For closing any open Windows applications.
  31. Private Declare Function GetWindowPlacement Lib "user32" (ByVal hwnd As IntPtr, ByRef lpwndpl As WINDOWPLACEMENT) As Integer
  32.  
  33. Private Structure POINTAPI
  34. Public x As Integer
  35. Public y As Integer
  36. End Structure
  37.  
  38. Private Structure RECT
  39. Public Left As Integer
  40. Public Top As Integer
  41. Public Right As Integer
  42. Public Bottom As Integer
  43. End Structure
  44.  
  45. Private Structure WINDOWPLACEMENT
  46. Public Length As Integer
  47. Public flags As Integer
  48. Public showCmd As Integer
  49. Public ptMinPosition As POINTAPI
  50. Public ptMaxPosition As POINTAPI
  51. Public rcNormalPosition As RECT
  52. End Structure
  53.  
  54. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  55. Dim intRet As Integer
  56. Dim wpTemp As WINDOWPLACEMENT
  57.  
  58. Dim lpszParentClass As String = "MozillaWindowClass" 'The name of the Calc.exe parent class.
  59. 'IEFrame 'MozillaWindowClass
  60. Dim lpszParentWindow As String = "Google - Mozilla Firefox" 'The title of the Calc.exe window.
  61. 'MSN.com - Windows Internet Explorer 'Google - Mozilla Firefox
  62. Dim ParenthWnd As New IntPtr(0) 'New IntPtr variable.
  63.  
  64. Dim hWnd As New IntPtr(0) 'New IntPtr variable.
  65.  
  66. ParenthWnd = FindWindow(lpszParentClass, lpszParentWindow)
  67.  
  68. wpTemp.Length = System.Runtime.InteropServices.Marshal.SizeOf(wpTemp)
  69. intRet = GetWindowPlacement(Me.Handle.ToInt32, wpTemp)
  70. 'Me.Handle.ToInt32
  71. If wpTemp.showCmd = 1 Then
  72. ' ActiveForm.WindowState = FormWindowState.Minimized
  73. ElseIf wpTemp.showCmd = 2 Then
  74. ' ActiveForm.WindowState = FormWindowState.Maximized
  75. ElseIf wpTemp.showCmd = 3 Then
  76. ' ActiveForm.WindowState = FormWindowState.Normal
  77. End If
  78.  
  79. If Not ParenthWnd = (IntPtr.Zero) Then 'Make sure we can find Calc.exe.
  80. 'Dim NumText As New IntPtr
  81. 'NumText = SendMessage(ParenthWnd, WM_GETTEXT, 24, 0)
  82. 'Dim Text As String = (NumText)
  83.  
  84. 'Display the string using a label
  85. 'MessageBox.Show(Text)
  86. 'SendMessage(ParenthWnd, WM_SETTEXT, 5, "yo sup")
  87. ShowWindow(ParenthWnd, 3)
  88. 'System.Threading.Thread.Sleep(50000)
  89. 'SendMessage(ParenthWnd, &H10, 0, 0) '&H10=WM_CLOSE
  90. 'SendMessage(ParenthWnd, WM_CLOSE, 0, 0)
  91.  
  92. Else
  93.  
  94. MessageBox.Show("Application Not Running!!", "Firefox Win API Source Code", MessageBoxButtons.OK, MessageBoxIcon.Error)
  95.  
  96. Exit Sub
  97.  
  98. End If
  99.  
  100. End Sub
  101.  
  102. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  103. Dim intRet As Integer
  104. Dim wpTemp As WINDOWPLACEMENT
  105.  
  106. Dim lpszParentClass As String = "MozillaWindowClass" 'The name of the Calc.exe parent class.
  107. 'IEFrame 'MozillaWindowClass
  108. Dim lpszParentWindow As String = "Google - Mozilla Firefox" 'The title of the Calc.exe window.
  109. 'MSN.com - Windows Internet Explorer 'Google - Mozilla Firefox
  110. Dim ParenthWnd As New IntPtr(0) 'New IntPtr variable.
  111.  
  112. Dim hWnd As New IntPtr(0) 'New IntPtr variable.
  113.  
  114. ParenthWnd = FindWindow(lpszParentClass, lpszParentWindow)
  115.  
  116. wpTemp.Length = System.Runtime.InteropServices.Marshal.SizeOf(wpTemp)
  117. intRet = GetWindowPlacement(Me.Handle.ToInt32, wpTemp)
  118. 'Me.Handle.ToInt32
  119. If wpTemp.showCmd = 1 Then
  120. ' ActiveForm.WindowState = FormWindowState.Minimized
  121. ElseIf wpTemp.showCmd = 2 Then
  122. ' ActiveForm.WindowState = FormWindowState.Maximized
  123. ElseIf wpTemp.showCmd = 3 Then
  124. ' ActiveForm.WindowState = FormWindowState.Normal
  125. End If
  126.  
  127. If Not ParenthWnd = (IntPtr.Zero) Then 'Make sure we can find Calc.exe.
  128.  
  129. 'SendMessage(ParenthWnd, WM_SETTEXT, 5, "yo sup")
  130.  
  131. 'System.Threading.Thread.Sleep(50000)
  132.  
  133. 'SendMessage(ParenthWnd, WM_CLOSE, 0, 0)
  134.  
  135. ShowWindow(ParenthWnd, 2)
  136.  
  137. Else
  138.  
  139. MessageBox.Show("Application Not Running!!", "Firefox Win API Source Code", MessageBoxButtons.OK, MessageBoxIcon.Error)
  140.  
  141. Exit Sub
  142.  
  143. End If
  144. End Sub
  145.  
  146. Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
  147. Dim intRet As Integer
  148. Dim wpTemp As WINDOWPLACEMENT
  149.  
  150. Dim lpszParentClass As String = "MozillaWindowClass" 'The name of the Calc.exe parent class.
  151. 'IEFrame 'MozillaWindowClass
  152. Dim lpszParentWindow As String = "Google - Mozilla Firefox" 'The title of the Calc.exe window.
  153. 'MSN.com - Windows Internet Explorer 'Google - Mozilla Firefox
  154. Dim ParenthWnd As New IntPtr(0) 'New IntPtr variable.
  155.  
  156. Dim hWnd As New IntPtr(0) 'New IntPtr variable.
  157.  
  158. ParenthWnd = FindWindow(lpszParentClass, lpszParentWindow)
  159.  
  160. wpTemp.Length = System.Runtime.InteropServices.Marshal.SizeOf(wpTemp)
  161. intRet = GetWindowPlacement(Me.Handle.ToInt32, wpTemp)
  162. 'Me.Handle.ToInt32
  163. If wpTemp.showCmd = 1 Then
  164. ' ActiveForm.WindowState = FormWindowState.Minimized
  165. ElseIf wpTemp.showCmd = 2 Then
  166. ' ActiveForm.WindowState = FormWindowState.Maximized
  167. ElseIf wpTemp.showCmd = 3 Then
  168. ' ActiveForm.WindowState = FormWindowState.Normal
  169. End If
  170.  
  171. If Not ParenthWnd = (IntPtr.Zero) Then 'Make sure we can find Calc.exe.
  172.  
  173. 'SendMessage(ParenthWnd, WM_SETTEXT, 5, "yo sup")
  174.  
  175. 'System.Threading.Thread.Sleep(50000)
  176.  
  177. SendMessage(ParenthWnd, WM_CLOSE, 0, 0)
  178.  
  179. 'ShowWindow(ParenthWnd, 2)
  180.  
  181. Else
  182.  
  183. MessageBox.Show("Application Not Running!!", "Firefox Win API Source Code", MessageBoxButtons.OK, MessageBoxIcon.Error)
  184.  
  185. Exit Sub
  186.  
  187. End If
  188. End Sub
  189. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement