vb2008如何得到外部Treeview的数据?
在vb6.0是可以实现的,但到了vs2008里就不行了,好像是申请进程内存没成功,VirtualAllocEx涵数总是返回0如下是vb2008的代码,高手帮看一下:
Option Explicit On
Imports System.Runtime.InteropServices
Public Class Form1
Private Declare Function EnumChildWindows Lib "user32" Alias "EnumChildWindows" (ByVal hWndParent As Integer, ByVal lpEnumFunc As DelegateEnumChildProc, ByVal lParam As Integer) As Integer
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Integer, ByVal lpClassName As String, ByVal nMaxCount As Integer) As Integer
Private Declare Function GetNextWindow Lib "user32" Alias "GetWindow" (ByVal hwnd As Integer, ByVal wFlag As Integer) As Integer
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Integer, ByVal lpString As String, ByVal cch As Integer) As Integer
Private Declare Function GetWindowThreadProcessId Lib "user32" Alias "GetWindowThreadProcessId" (ByVal hWnd As Integer, ByRef lpdwProcessId As Integer) As Integer
Private Declare Function OpenProcess Lib "kernel32" Alias "OpenProcess" (ByVal dwDesiredAccess As Integer, ByVal bInheritHandle As Integer, ByVal dwProcessId As Integer) As Integer
Private Declare Function CloseHandle Lib "kernel32" Alias "CloseHandle" (ByVal hObject As Integer) As Integer
Private Declare Function ReadProcessMemory Lib "kernel32" Alias "ReadProcessMemory" (ByVal hProcess As Integer, ByVal lpBaseAddress As Object, ByRef lpBuffer As Object, ByVal nSize As Integer, ByVal lpNumberOfBytesWritten As Integer) As Integer
Private Declare Function FindWindowa Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
Private Declare Function GetForegroundWindow Lib "user32" () As Integer
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Private Declare Function VirtualAllocEx Lib "kernel32" Alias "VirtualAllocEx" (ByVal hProcess As Integer, ByVal lpAddress As Object, ByVal dwSize As Integer, ByVal flAllocationType As Integer, ByVal flProtect As Integer) As Integer
Private Declare Function VirtualFreeEx Lib "kernel32" Alias "VirtualFreeEx" (ByVal hProcess As Integer, ByVal lpAddress As Object, ByVal dwSize As Integer, ByVal dwFreeType As Integer) As Integer
Public Declare Function WriteProcessMemoryF Lib "kernel32" Alias "WriteProcessMemory" (ByVal hProcess As Integer, ByVal lpBaseAddress As Object, ByRef lpBuffer As Object, ByVal nSize As Integer, ByRef lpNumberOfBytesWritten As Integer) As Integer
Public Declare Function WriteProcessMemoryV Lib "kernel32" Alias "WriteProcessMemory" (ByVal hProcess As Integer, ByVal lpBaseAddress As Object, ByVal lpBuffer As Object, ByVal nSize As Integer, ByVal lpNumberOfBytesWritten As Integer) As Integer
'記憶體型態
Private Const MEM_COMMIT = &H1000
Private Const MEM_RESERVE = &H2000
Private Const MEM_DECOMMIT = &H4000
Private Const MEM_RELEASE = &H8000
Private Const MEM_FREE = &H10000
Private Const MEM_PRIVATE = &H20000
Private Const MEM_MAPPED = &H40000
Private Const MEM_RESET = &H80000
Private Const MEM_TOP_DOWN = &H100000
Private Const MEM_4MB_PAGES = &H80000000
Private Const SEC_IMAGE = &H1000000
Private Const MEM_IMAGE = SEC_IMAGE
'記憶體保護狀態
Private Const PAGE_NOACCESS = &H1
Private Const PAGE_READONLY = &H2
Private Const PAGE_READWRITE = &H4
Private Const PAGE_WRITECOPY = &H8
Private Const PAGE_EXECUTE = &H10
Private Const PAGE_EXECUTE_READ = &H20
Private Const PAGE_EXECUTE_READWRITE = &H40
Private Const PAGE_EXECUTE_WRITECOPY = &H80
Private Const PAGE_GUARD = &H100
Private Const PAGE_NOCACHE = &H200
Public IsNt As Boolean
'============NT Shared memory staff======================
Public Const PROCESS_QUERY_INFORMATION = &H400
Public Const PROCESS_VM_OPERATION = &H8
Public Const PROCESS_VM_READ = &H10
Public Const PROCESS_VM_WRITE = &H20
Public Const PROCESS_ALL_ACCESS = 0
'public constants
Public Const TVIF_TEXT = 1
Public Const TVIF_HANDLE = &H10
Public Const TV_FIRST = &H1100&
Public Const TVM_GETCOUNT = TV_FIRST + 5
Public Const TVM_SELECTITEM = TV_FIRST + 11
Public Const TVM_GETITEM = TV_FIRST + 12
Public Const TVM_GETNEXTITEM = TV_FIRST + 10
Public Const TVGN_ROOT = 0
Public Const TVGN_NEXT = 1
Public Const TVGN_CHILD = 4
Public Const TVGN_CARET = 9
'Structures
Public Structure TVITEM
Public mask As Integer
Public hItem As Integer
Public state As Integer
Public stateMask As Integer
Public pszText As Integer
Public cchTextMax As Integer
Public iImage As Integer
Public iSelectedImage As Integer
Public cChildren As Integer
Public lParam As Integer
End Structure
Dim pmHwnd As Integer
Function EnumChildProc(ByVal hwnd As Integer, ByVal lParam As Integer) As Integer
Dim lpClassName As String
Dim retval As Integer
Dim pid As Integer
Dim hProcess As Integer
lpClassName = Space(256)
GetWindowText(hwnd, lpClassName, Len(lpClassName))
retval = GetClassName(hwnd, lpClassName, 256)
ListBox2.Items.Add(hwnd & "__" & lpClassName)
If lpClassName.IndexOf("SysTreeView32") <> -1 Then
Dim respm = GetWindowThreadProcessId(hwnd, pid)
Dim bSuccess As Integer
Dim i As Integer, s As String
Dim dwBytesRead As Int32, dwBytesWrite As Int32
Dim lpTreeItemRemote As Int32, lpTextRemote As Int32
Dim nMaxLen As Integer
nMaxLen = 1023
Dim szBuf() As Byte
ReDim szBuf(nMaxLen)
Dim lvItemLocal As TVITEM
Dim bWriteOK As Integer
hProcess = OpenProcess(PROCESS_VM_OPERATION Or PROCESS_VM_READ Or PROCESS_VM_WRITE, False, pid)
If hProcess <> 0 Then
lpTextRemote = VirtualAllocEx(hProcess, 0, nMaxLen + 1, MEM_COMMIT, PAGE_READWRITE)
bWriteOK = WriteProcessMemoryF(hProcess, lpTextRemote, szBuf(0), nMaxLen + 1, dwBytesWrite)
'write structure
dwBytesWrite = 0
lvItemLocal.hItem = SendMessage(hwnd, TVM_GETNEXTITEM, TVGN_ROOT, 0)
lvItemLocal.mask = TVIF_TEXT + TVIF_HANDLE
lvItemLocal.cchTextMax = nMaxLen
lvItemLocal.pszText = lpTextRemote
Dim nSizeTVITEM As Int32 = Marshal.SizeOf(lvItemLocal)
Dim lpTVITEM As IntPtr = Marshal.AllocHGlobal(nSizeTVITEM)
Marshal.StructureToPtr(lvItemLocal, lpTVITEM, True)
lpTreeItemRemote = VirtualAllocEx(hProcess, 0, nSizeTVITEM, MEM_COMMIT, PAGE_READWRITE)
bWriteOK = WriteProcessMemoryF(hProcess, lpTreeItemRemote, lpTVITEM, nSizeTVITEM, dwBytesWrite)
i = SendMessage(hwnd, TVM_GETITEM, 0, lpTreeItemRemote)
'read result
bSuccess = ReadProcessMemory(hProcess, lpTextRemote, szBuf(0), nMaxLen + 1, dwBytesRead)
Call VirtualFreeEx(hProcess, lpTreeItemRemote, 0, MEM_DECOMMIT)
Call VirtualFreeEx(hProcess, lpTextRemote, 0, MEM_DECOMMIT)
End If
CloseHandle(hProcess)
'*************** 显示结果
'sItemText = StrConv(LeftB(szBuf, InStr(szBuf, Chr(0))), System.Text.Encoding.Unicode)
'Label1.Text = sItemText
End If
EnumChildProc = 1
End Function
Private Delegate Function DelegateEnumChildProc(ByVal hwnd As Integer, ByVal lParam As Integer) As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
pmHwnd = GetForegroundWindow
Dim str1 As String, len1 As Integer
str1 = Space(255) '定义接收字串.
GetWindowText(pmHwnd, str1, 1024)
Dim count As Integer = 0
ListBox1.Items.Clear()
ListBox2.Items.Clear()
Do While pmHwnd <> 0
str1 = Space(255) '定义接收字串.
count += 1
pmHwnd = GetNextWindow(pmHwnd, 2) '只有2才表示找下一个窗口
len1 = GetWindowText(pmHwnd, str1, Len(str1))
If len1 <> 0 Then
ListBox1.Items.Add(str1)
End If
If str1.IndexOf("PowerMILL Pro") <> -1 Then '新建文本文档
Dim res
res = EnumChildWindows(pmHwnd, AddressOf EnumChildProc, 3)
End If
Loop
End Sub
End Class
补充:VB , API