VB.net实现从ListView控件中异地获取文本内容源代码

对于一些VB6工程代码来说,需要完成从VB到VB.net的转换,在转换过程中需要注意很多内容,利用VB.net的直接转换功能很少能完全成功,需要我们付出很大的努力。

下面的VB.net代码实现从其他应用程序中的ListView控件中异地获取文本内容,是从VB6的代码中转换而来,包括一些旧类型的转换和数据类型声明的变化。

view plaincopy to clipboardprint?

1 Option Explicit On

2 Module Module1

3 Public Structure LV_ITEMA

4 Dim mask As Integer

5 Dim iItem As Integer

6 Dim iSubItem As Integer

7 Dim state As Integer

8 Dim stateMask As Integer

9 Dim pszText As Integer

10 Dim cchTextMax As Integer

11 Dim iImage As Integer

12 Dim lParam As Integer

13 Dim iIndent As Integer

14 End Structure

15 'Constants

16 Private Const LVFI_PARAM = 1

17 Private Const LVM_FIRST = &H1000

18 Private Const LVM_FINDITEM = LVM_FIRST + 13

19 Private Const LVM_GETITEMTEXT = LVM_FIRST + 45

20 Private Const LVM_SORTITEMS = LVM_FIRST + 48

21 Private Const LVM_GETHEADER = LVM_FIRST + 31

22 Private Const LVM_GETITEMCOUNT = (LVM_FIRST + 4)

23 Private Const HDM_FIRST = &H1200 '// Header messages

24 Private Const HDM_GETITEMCOUNT = (HDM_FIRST + 0)

25 Private Const HDM_ORDERTOINDEX = (HDM_FIRST + 15)

26 Private Const PROCESS_QUERY_INFORMATION = 1024

27 Private Const PROCESS_VM_OPERATION = &H8

28 Private Const PROCESS_VM_READ = &H10

29 Private Const PROCESS_VM_WRITE = &H20

30 Private Const PROCESS_ALL_ACCESS As Long = &H1F0FFF

31 Private Const STANDARD_RIGHTS_REQUIRED = &HF0000

32 Private Const MAX_LVMSTRING As Integer = 255 '可根椐读取数据长度设置适当的数值

33 Private Const MEM_COMMIT = &H1000

34 Private Const MEM_RELEASE = &H8000

35 Private Const PAGE_READWRITE = &H4

36 Private Const LVIF_TEXT As Integer = &H1

37 'API declarations

38 Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Integer) As Integer

39 Private Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Integer, ByVal lpBaseAddress As Integer, ByRef lpBuffer As Byte, ByVal nSize As Integer, ByVal lpNumberOfBytesWritten As Integer) As Integer

40 Private Declare Function VirtualFreeEx Lib "kernel32" (ByVal hProcess As Integer, ByVal lpAddress As Integer, ByVal dwSize As Integer, ByVal dwFreeType As Integer) As Integer

41 Private Declare Function WriteProcessMemory Lib "kernel32" (ByVal hProcess As Integer, ByVal lpBaseAddress As Integer, ByRef lpBuffer As LV_ITEMA, ByVal nSize As Integer, ByVal lpNumberOfBytesWritten As Integer) As Integer

42 Private Declare Function VirtualAllocEx Lib "kernel32" (ByVal hProcess As Integer, ByVal lpAddress As Integer, ByVal dwSize As Integer, ByVal flAllocationType As Integer, ByVal flProtect As Integer) As Integer

43 Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Integer, ByVal bInheritHandle As Integer, ByVal dwProcId As Integer) As Integer

44 Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Integer, ByRef lpdwProcessId As Integer) As Integer

45 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

46 Public Function GetListviewItem(ByVal hWindow As Integer, ByVal ProcessID As Integer, ByVal pColumn As Integer, ByVal pRow As Integer) As String

47 Dim Result As Integer

48 Dim myItem As LV_ITEMA

49 Dim pHandle As Integer

50 Dim pStrBufferMemory As Integer

51 Dim pMyItemMemory As Integer

52 Dim strBuffer() As Byte

53 Dim Index As Integer

54 Dim tmpString As String

55 Dim strLength As Integer

56 '******************************

57 '为动态数组变量重新分配存储空间

58 '******************************

59 ReDim strBuffer(MAX_LVMSTRING)

60 '*****************************************************************************************************

61 '打开一个现有进程的句柄,返回值Long,如执行成功,返回进程句柄;零表示失败。会设置GetLastError

62 'Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long

63 '参数 类型及说明

64 'dwDesiredAccess Long,指定这个句柄要求的访问方法。指定API32.TXT文件中以PROCESS_???开头的一个或多个常数

65 'bInheritHandle Long,如句柄能够由子进程继承,则为TRUE

66 'dwProcessId Long,要打开那个进程的进程标识符

67 '*****************************************************************************************************

68 pHandle = OpenProcess(PROCESS_ALL_ACCESS, False, ProcessID)

69 '*****************************************************************************************************

70 'VirtualAllocEx(目标进程的句柄,0,内存区域的大小,分配类型,新分配内存的存取保护类型) 返回所分配页面的基址

71 '*****************************************************************************************************

72 pStrBufferMemory = VirtualAllocEx(pHandle, 0, MAX_LVMSTRING, MEM_COMMIT, PAGE_READWRITE)

73 '*************************************************

74 '初始化LV_ITEM 结构

75 'MyItem.iSubItem 列的索引号

76 'myItem.pszText 数据内容(此处是一个分配的内存地址)

77 '*************************************************

78 myItem.mask = LVIF_TEXT

79 myItem.iSubItem = pColumn

80 myItem.pszText = pStrBufferMemory

81 myItem.cchTextMax = MAX_LVMSTRING

82 '***********************************************************

83 '把这个结构写入远程进程process's 存储量

84 'WriteProcessMemory(目标进程的句柄,地址,写入的数据,字节数,0)

85 '***********************************************************

86 pMyItemMemory = VirtualAllocEx(pHandle, 0, Len(myItem), MEM_COMMIT, PAGE_READWRITE)

87 Result = WriteProcessMemory(pHandle, pMyItemMemory, (myItem), Len(myItem), 0&)

88 '********************************

89 '发送消息,得到项目信息和写入内存

90 '********************************

91 strLength = SendMessage(hWindow, LVM_GETITEMTEXT, pRow, pMyItemMemory)

92 Result = ReadProcessMemory(pHandle, pStrBufferMemory, strBuffer(0), MAX_LVMSTRING, 0)

93 tmpString = System.Text.Encoding.Default.GetString(strBuffer).Trim

94 Result = ReadProcessMemory(pHandle, pMyItemMemory, myItem, Len(myItem), 0)

95 '****************************

96 '释放分配的内存和关闭进程句柄

97 '****************************

98 Result = VirtualFreeEx(pHandle, pStrBufferMemory, 0, MEM_RELEASE)

99 Result = VirtualFreeEx(pHandle, pMyItemMemory, 0, MEM_RELEASE)

100 Result = CloseHandle(pHandle)

101 If Len(tmpString) > 0 Then GetListviewItem = tmpString

102 End Function

103 End Module

104 Option Explicit On

105 Module Module1

106 Public Structure LV_ITEMA

107 Dim mask As Integer

108 Dim iItem As Integer

109 Dim iSubItem As Integer

110 Dim state As Integer

111 Dim stateMask As Integer

112 Dim pszText As Integer

113 Dim cchTextMax As Integer

114 Dim iImage As Integer

115 Dim lParam As Integer

116 Dim iIndent As Integer

117 End Structure

118 'Constants

119 Private Const LVFI_PARAM = 1

120 Private Const LVM_FIRST = &H1000

121 Private Const LVM_FINDITEM = LVM_FIRST + 13

122 Private Const LVM_GETITEMTEXT = LVM_FIRST + 45

123 Private Const LVM_SORTITEMS = LVM_FIRST + 48

124 Private Const LVM_GETHEADER = LVM_FIRST + 31

125 Private Const LVM_GETITEMCOUNT = (LVM_FIRST + 4)

126 Private Const HDM_FIRST = &H1200 '// Header messages

127 Private Const HDM_GETITEMCOUNT = (HDM_FIRST + 0)

128 Private Const HDM_ORDERTOINDEX = (HDM_FIRST + 15)

129 Private Const PROCESS_QUERY_INFORMATION = 1024

130 Private Const PROCESS_VM_OPERATION = &H8

131 Private Const PROCESS_VM_READ = &H10

132 Private Const PROCESS_VM_WRITE = &H20

133 Private Const PROCESS_ALL_ACCESS As Long = &H1F0FFF

134 Private Const STANDARD_RIGHTS_REQUIRED = &HF0000

135 Private Const MAX_LVMSTRING As Integer = 255 '可根椐读取数据长度设置适当的数值

136 Private Const MEM_COMMIT = &H1000

137 Private Const MEM_RELEASE = &H8000

138 Private Const PAGE_READWRITE = &H4

139 Private Const LVIF_TEXT As Integer = &H1

140 'API declarations

141 Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Integer) As Integer

142 Private Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Integer, ByVal lpBaseAddress As Integer, ByRef lpBuffer As Byte, ByVal nSize As Integer, ByVal lpNumberOfBytesWritten As Integer) As Integer

143 Private Declare Function VirtualFreeEx Lib "kernel32" (ByVal hProcess As Integer, ByVal lpAddress As Integer, ByVal dwSize As Integer, ByVal dwFreeType As Integer) As Integer

144 Private Declare Function WriteProcessMemory Lib "kernel32" (ByVal hProcess As Integer, ByVal lpBaseAddress As Integer, ByRef lpBuffer As LV_ITEMA, ByVal nSize As Integer, ByVal lpNumberOfBytesWritten As Integer) As Integer

145 Private Declare Function VirtualAllocEx Lib "kernel32" (ByVal hProcess As Integer, ByVal lpAddress As Integer, ByVal dwSize As Integer, ByVal flAllocationType As Integer, ByVal flProtect As Integer) As Integer

146 Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Integer, ByVal bInheritHandle As Integer, ByVal dwProcId As Integer) As Integer

147 Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Integer, ByRef lpdwProcessId As Integer) As Integer

148 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

149 Public Function GetListviewItem(ByVal hWindow As Integer, ByVal ProcessID As Integer, ByVal pColumn As Integer, ByVal pRow As Integer) As String

150 Dim Result As Integer

151 Dim myItem As LV_ITEMA

152 Dim pHandle As Integer

153 Dim pStrBufferMemory As Integer

154 Dim pMyItemMemory As Integer

155 Dim strBuffer() As Byte

156 Dim Index As Integer

157 Dim tmpString As String

158 Dim strLength As Integer

159 '******************************

160 '为动态数组变量重新分配存储空间

161 '******************************

162 ReDim strBuffer(MAX_LVMSTRING)

163 '*****************************************************************************************************

164 '打开一个现有进程的句柄,返回值Long,如执行成功,返回进程句柄;零表示失败。会设置GetLastError

165 'Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long

166 '参数 类型及说明

167 'dwDesiredAccess Long,指定这个句柄要求的访问方法。指定API32.TXT文件中以PROCESS_???开头的一个或多个常数

168 'bInheritHandle Long,如句柄能够由子进程继承,则为TRUE

169 'dwProcessId Long,要打开那个进程的进程标识符

170 '*****************************************************************************************************

171 pHandle = OpenProcess(PROCESS_ALL_ACCESS, False, ProcessID)

172 '*****************************************************************************************************

173 'VirtualAllocEx(目标进程的句柄,0,内存区域的大小,分配类型,新分配内存的存取保护类型) 返回所分配页面的基址

174 '*****************************************************************************************************

175 pStrBufferMemory = VirtualAllocEx(pHandle, 0, MAX_LVMSTRING, MEM_COMMIT, PAGE_READWRITE)

176 '*************************************************

177 '初始化LV_ITEM 结构

178 'MyItem.iSubItem 列的索引号

179 'myItem.pszText 数据内容(此处是一个分配的内存地址)

180 '*************************************************

181 myItem.mask = LVIF_TEXT

182 myItem.iSubItem = pColumn

183 myItem.pszText = pStrBufferMemory

184 myItem.cchTextMax = MAX_LVMSTRING

185 '***********************************************************

186 '把这个结构写入远程进程process's 存储量

187 'WriteProcessMemory(目标进程的句柄,地址,写入的数据,字节数,0)

188 '***********************************************************

189 pMyItemMemory = VirtualAllocEx(pHandle, 0, Len(myItem), MEM_COMMIT, PAGE_READWRITE)

190 Result = WriteProcessMemory(pHandle, pMyItemMemory, (myItem), Len(myItem), 0&)

191 '********************************

192 '发送消息,得到项目信息和写入内存

193 '********************************

194 strLength = SendMessage(hWindow, LVM_GETITEMTEXT, pRow, pMyItemMemory)

195 Result = ReadProcessMemory(pHandle, pStrBufferMemory, strBuffer(0), MAX_LVMSTRING, 0)

196 tmpString = System.Text.Encoding.Default.GetString(strBuffer).Trim

197 Result = ReadProcessMemory(pHandle, pMyItemMemory, myItem, Len(myItem), 0)

198 '****************************

199 '释放分配的内存和关闭进程句柄

200 '****************************

201 Result = VirtualFreeEx(pHandle, pStrBufferMemory, 0, MEM_RELEASE)

202 Result = VirtualFreeEx(pHandle, pMyItemMemory, 0, MEM_RELEASE)

203 Result = CloseHandle(pHandle)

204 If Len(tmpString) > 0 Then GetListviewItem = tmpString

205 End Function

206 End Module

实现转换的过程其实就是一个按图索骥的过程,按照VB.net丰富的运行错误信息提示,一步步的来完成。