VB读取线程、句柄及写入内存的API代码实例

 更新时间:2014年07月31日 10:47:12   转载 投稿:shichen2014  
这篇文章主要介绍了VB读取线程、句柄及写入内存的API代码实例,需要的朋友可以参考下

本文所述实例为VB读取内存、线程及句柄的一个API,对涉及系统底层操作的VB编程有一定的帮助,需要的读者可以参考使用。这个API可获取到线程ID,写内存,包括进程句柄,ByVal 内存区地址,数据,总长度,已经完成长度,读取进程,包括进程句柄,ByVal 内存区地址,读取来的数据存放处,要读取的长度,已经读取的长度,内存分配(进程柄,地址[好像只要丢个0进去就行],长度,权限1[MEM_COMMIT],权限2[PAGE_READWRITE])返回:分配到的内存起始地址等功能。

具体实现代码如下:

Attribute VB_Name = "API"
Option Explicit
Public Declare Function GetDesktopWindow Lib "User32.DLL" () As Long
Public Declare Function FindWindow Lib "User32.DLL" Alias "FindWindowA" (ByVal ClassName As String, ByVal Caption As String) As Long
Public Declare Function GetWindow Lib "User32.DLL" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Public Declare Function GetWindowText Lib "User32.DLL" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Public Const GW_CHILD = (5)
Public Const GW_HWNDNEXT = (2)

Public Declare Function GetWindowThreadProcessId Lib "User32.DLL" (ByVal hwnd As Long, ProcessId As Long) As Long
'取找线程ID(句柄,返回的线程ID)

Public Declare Function OpenProcess Lib "Kernel32.DLL" (ByVal 操作权限 As Long, ByVal 继承句柄 As Long, ByVal 线程ID As Long) As Long
Public Declare Function ReadProcessMemory Lib "Kernel32.DLL" (ByVal 进程柄 As Long, ByVal 内存位置 As Long, 缓冲区 As Any, ByVal 长度 As Long, lpNumberOfBytesWritten As Long) As Long
'读取进程(进程句柄,ByVal 内存区地址,读取来的数据存放处,要读取的长度,已经读取的长度[0])
Public Declare Function WriteProcessMemory Lib "Kernel32.DLL" (ByVal 进程柄 As Long, 内存位置 As Any, 缓冲区 As Any, ByVal 长度 As Long, lpNumberOfBytesWritten As Long) As Long
'写内存(进程句柄,ByVal 内存区地址,数据,总长度,已经完成长度[0])
Public Declare Function CloseHandle Lib "Kernel32.DLL" (ByVal 进程柄 As Long) As Long
'释放(进程句柄)'不释放会出错

Public Const STANDARD_RIGHTS_REQUIRED = &HF0000
Public Const SYNCHRONIZE = &H100000
Public Const RRAD_WRITE = &H1F0FFF
Public Const PROCESS_VM_OPERATION = &H8&
Public Const 读取 = &H10&
Public Const 写入 = &H20&

'---------变量转换API
Public Declare Sub MOV Lib "Kernel32.DLL" Alias "RtlMoveMemory" (变量1 As Any, 变量2 As Any, ByVal 长度 As Long)

'---------内存保护分配释放
Public Declare Function VPE Lib "Kernel32.DLL" Alias "VirtualProtectEx" (ByVal 进程柄 As Long, 地址 As Any, ByVal 长度 As Long, ByVal flNewProtect As Long, lpflOldProtect As Long) As Long
Public Declare Function VAE Lib "Kernel32.DLL" Alias "VirtualAllocEx" (ByVal 进程柄 As Long, ByVal 地址 As Long, ByVal 长度 As Long, ByVal flAllocationType As Long, ByVal flProtect As Long) As Long
'内存分配(进程柄,地址[好像只要丢个0进去就行],长度,权限1[MEM_COMMIT],权限2[PAGE_READWRITE])返回:分配到的内存起始地址
Public Declare Function VFE Lib "Kernel32.DLL" Alias "VirtualFreeEx" (ByVal 进程柄 As Long, ByVal 地址 As Long, ByVal 长度 As Long, ByVal 释放类型 As Long) As Long

Public Const MEM_COMMIT = &H1000
Public Const PAGE_READWRITE = &H4
Public Const STILL_ACTIVE = &H103&
Public Const INFINITE = &HFFFF

'---------取模块函数位置API
Public Declare Function GetModuleHandle Lib "Kernel32.DLL" Alias "GetModuleHandleA" (ByVal ModuleName As String) As Long
Public Declare Function LoadLibrary Lib "Kernel32.DLL" Alias "LoadLibraryA" (ByVal ModuleName As String) As Long
Public Declare Function GetProcAddress Lib "Kernel32.DLL" (ByVal hModule As Long, ByVal ProcName As String) As Long

Public Declare Function CreateRemoteThread Lib "Kernel32.DLL" (ByVal 进程柄 As Long, lpThreadAttributes As Any, ByVal dwStackSize As Long, ByVal lpStartAddress As Long, lpParameter As Any, ByVal dwCreationFlags As Long, lpThreadID As Long) As Long
Public Declare Function GetTickCount Lib "kernel32" () As Long

相关文章

  • 如何产生真正的随机函数?

    如何产生真正的随机函数?

    如何产生真正的随机函数?...
    2007-03-03
  • vb简易计算器源码

    vb简易计算器源码

    本人一觉醒来闲得无聊,正在学习VB,便用VB写个简易的计算器吧!巩固基础
    2012-09-09
  • VB中使用WMI获取系统硬件和软件有关信息

    VB中使用WMI获取系统硬件和软件有关信息

    WMI是英文Windows Management Instrumentation的简写,它的功能主要是:访问本地主机的一些信息和服务,可以管理远程计算机(当然你必须要拥有足够的权限),比如:重启,关机,关闭进程,创建进程等
    2019-04-04
  • VB XMLHTTP使用方法

    VB XMLHTTP使用方法

    VB判断地址是否存在的XMLHTTP方法
    2008-07-07
  • VB关机恶搞小程序

    VB关机恶搞小程序

    本文给大家汇总了一些使用VB实现的关机恶搞小程序,大家娱乐一下就行了,千万别做坏事哦
    2015-09-09
  • VB实现鼠标绘图实例代码

    VB实现鼠标绘图实例代码

    这篇文章主要介绍了VB实现鼠标绘图实例代码,很实用的一个功能,需要的朋友可以参考下
    2014-07-07
  • VB打开与保存txt文件的方法

    VB打开与保存txt文件的方法

    这篇文章主要介绍了VB打开与保存txt文件的方法,涉及使用VB操作文本文件的相关技巧,需要的朋友可以参考下
    2015-06-06
  • Basic求10000以内的完美数

    Basic求10000以内的完美数

    一个数的所有真因数加起来正好等于这个自然数本身,在数学上,具有这种性质的自然数叫做完全数。关键是求出该自然数k的所有因子。如果k%i为0,则i为k的因子,用循环语句求出k 的所有真因子(1<=i<=k/2),把它们求和,看是否等于k来判断。
    2015-05-05
  • VB简单实现防止文件被改写

    VB简单实现防止文件被改写

    这篇文章主要介绍了VB简单实现防止文件被改写的方法,十分的简单实用,有需要的小伙伴可以参考下。
    2015-06-06
  • VB实现屏蔽文本框右键菜单的复制、粘贴等功能的方法

    VB实现屏蔽文本框右键菜单的复制、粘贴等功能的方法

    这篇文章主要介绍了VB实现屏蔽文本框右键菜单的复制、粘贴等功能,是非常实用的一个功能,需要的朋友可以参考下
    2014-07-07

最新评论