我知道这是旧的,但我尝试了很多方法让它在 VB.net 中工作。我需要为 RECT 创建 2 个不同的结构,因为它使用 Long 参数为 RECT 位置返回错误的值。
这是示例,您可以轻松转换为 C#
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim SelNodeRectangle As RECT = GetTreeViewSelectedNodeRECT(TreeViewPtr)
End Sub
Private Const TV_FIRST As Integer = &H1100
Private Const TVGN_CARET As Integer = &H9
Private Const TVM_GETNEXTITEM As Integer = (TV_FIRST + 10)
Private Const TVM_GETITEMRECT As Integer = (TV_FIRST + 4)
Private Const PROCESS_VM_OPERATION As Integer = &H8
Private Const PROCESS_VM_READ As Integer = &H10
Private Const PROCESS_VM_WRITE = (&H20)
Private Const PAGE_READWRITE As Long = &H4
Private Const MEM_COMMIT As Long = &H1000&
Private Const MEM_RELEASE As Long = &H8000&
Private Structure RECTPointer
Public Left As IntPtr
End Structure
Private Structure RECT
Public Left As Integer
Public Top As Integer
Public Right As Integer
Public Bottom As Integer
End Structure
<Flags>
Public Enum AllocationType
Commit = &H1000
Reserve = &H2000
Decommit = &H4000
Release = &H8000
Reset = &H80000
Physical = &H400000
TopDown = &H100000
WriteWatch = &H200000
LargePages = &H20000000
End Enum
Private Declare Function SendMessageA Lib "user32" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As Long) As IntPtr
Private Declare Function WriteProcessMemory Lib "kernel32" (ByVal hProcess As IntPtr, ByVal lpBaseAddress As IntPtr, ByRef buffer As RECTPointer, ByVal dwSize As Integer, ByVal lpNumberOfBytesWritten As IntPtr) As Boolean
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As IntPtr, ByRef lpdwProcessID As Integer) As Integer
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Integer, ByVal bInheritHandle As Boolean, ByVal dwProcessId As UInteger) As IntPtr
Private Declare Function VirtualAllocEx Lib "kernel32" (ByVal hProcess As IntPtr, ByVal lpAddress As IntPtr, ByVal dwSize As UInteger, ByVal flAllocationType As UInteger, ByVal flProtect As UInteger) As IntPtr
Private Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Integer, ByVal lpBaseAddress As Long, ByVal lpBuffer As Long, ByVal nSize As Integer, ByRef lpNumberOfBytesWritten As Integer) As Integer
Private Declare Function VirtualFreeEx Lib "kernel32" (ByVal hProcess As IntPtr,
ByVal lpAddress As IntPtr,
ByVal dwSize As Integer,
ByVal dwFreeType As AllocationType) As Boolean
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As IntPtr) As <Runtime.InteropServices.MarshalAs(Runtime.InteropServices.UnmanagedType.Bool)> Boolean
Private Function GetTreeViewSelectedNodeRECT(TreeViewPTR As IntPtr) As RECT
Const dwBufferSize As Integer = 1024
Dim dwProcessID As Integer
Dim processHandle As IntPtr = IntPtr.Zero
Dim remotePointerBuffer As IntPtr = IntPtr.Zero
Dim localPointerBuffer As IntPtr = IntPtr.Zero
Dim threadId As IntPtr = IntPtr.Zero
localPointerBuffer = Runtime.InteropServices.Marshal.AllocHGlobal(dwBufferSize)
threadId = GetWindowThreadProcessId(TreeViewPTR, dwProcessID)
processHandle = OpenProcess(PROCESS_VM_OPERATION + PROCESS_VM_WRITE + PROCESS_VM_READ, False, dwProcessID)
remotePointerBuffer = VirtualAllocEx(processHandle, IntPtr.Zero, dwBufferSize, MEM_COMMIT, PAGE_READWRITE)
Dim bSuccess As Boolean
Dim tvItem As New RECTPointer()
Dim SelectedNodePTR As IntPtr = SendMessageA(TreeViewPTR, TVM_GETNEXTITEM, TVGN_CARET, 0)
tvItem.Left = SelectedNodePTR
bSuccess = WriteProcessMemory(processHandle, remotePointerBuffer, tvItem, Runtime.InteropServices.Marshal.SizeOf(GetType(RECTPointer)), IntPtr.Zero)
If Not bSuccess Then
Throw New SystemException("Fail writing to process memory!")
End If
SendMessageA(TreeViewPTR, TVM_GETITEMRECT, 0, remotePointerBuffer)
bSuccess = ReadProcessMemory(processHandle, remotePointerBuffer, localPointerBuffer, dwBufferSize, IntPtr.Zero)
If Not bSuccess Then
Throw New SystemException("Fail reading from process memory!")
End If
Dim returnRECT As RECT = Runtime.InteropServices.Marshal.PtrToStructure(localPointerBuffer, GetType(RECT))
If localPointerBuffer <> IntPtr.Zero Then
Runtime.InteropServices.Marshal.FreeHGlobal(localPointerBuffer)
End If
If remotePointerBuffer <> IntPtr.Zero Then
VirtualFreeEx(processHandle, remotePointerBuffer, 0, MEM_RELEASE)
End If
If processHandle <> IntPtr.Zero Then
CloseHandle(processHandle)
End If
Return returnRECT
End Function