实用的内存块操作类(vb源码)

本文介绍了一种使用VB进行内存操作的方法,包括内存分配、读取、写入和释放等过程。通过调用kernel32.dll中的函数实现这些操作,如VirtualAlloc、VirtualFree等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

导读:
  (声明:魏滔序原创,转贴请注明出处。)
  Option Explicit
  Private Declare Function VirtualAlloc Lib "kernel32" (ByVal lpAddress As Long, ByVal dwSize As Long, ByVal flAllocationType As Long, ByVal flProtect As Long) As Long
  Private Declare Function VirtualFree Lib "kernel32" (ByVal lpAddress As Long, ByVal dwSize As Long, ByVal dwFreeType As Long) As Long
  Private Declare Function VirtualLock Lib "kernel32" (ByVal lpAddress As Long, ByVal dwSize As Long) As Long
  Private Declare Function VirtualUnlock Lib "kernel32" (ByVal lpAddress As Long, ByVal dwSize As Long) As Long
  Private Declare Function IsBadReadPtr Lib "kernel32" (ByVal lp As Long, ByVal ucb As Long) As Long
  Private Declare Function IsBadWritePtr Lib "kernel32" (ByVal lp As Long, ByVal ucb As Long) As Long
  Private Declare Function IsBadStringPtr Lib "kernel32" Alias "IsBadStringPtrA" (ByVal lpsz As Long, ByVal ucchMax As Long) As Long
  Private Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" (ByVal lpStringDest As String, ByVal lpStringSrc As Long) As Long
  Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As Long) As Long
  Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal pDest As Long, ByVal pSrc As Long, ByVal ByteLen As Long)
  Private Const MEM_DECOMMIT = &H4000
  Private Const MEM_RELEASE = &H8000
  Private Const MEM_COMMIT = &H1000
  Private Const PAGE_EXECUTE_READWRITE = &H40
  Private m_Handle As Long
  Public Property Get Handle() As Long
  Handle = m_Handle
  End Property
  '分配内存
  Public Sub Allocate(ByVal lCount As Long)
  Call Release
  m_Handle = VirtualAlloc(ByVal 0&, lCount, MEM_COMMIT, PAGE_EXECUTE_READWRITE)
  VirtualLock m_Handle, lCount
  End Sub
  '读取
  Public Sub ReadFromPointer(ByVal hWritePointer As Long, ByVal lLength As Long)
  If IsBadWritePtr(hWritePointer, lLength) = 0 And IsBadReadPtr(Handle, lLength) = 0 Then
  CopyMemory hWritePointer, Handle, lLength
  End If
  End Sub
  '写入
  Public Sub WriteToPointer(ByVal hReadPointer As Long, ByVal lLength As Long)
  If IsBadReadPtr(hReadPointer, lLength) = 0 And IsBadWritePtr(Handle, lLength) = 0 Then
  CopyMemory Handle, hReadPointer, lLength
  End If
  End Sub
  '释放内存
  Public Sub Release()
  Dim lLength As Long
  If m_Handle <>0 Then
  VirtualUnlock m_Handle, lLength
  VirtualFree m_Handle, lLength, MEM_DECOMMIT
  VirtualFree m_Handle, 0, MEM_RELEASE
  m_Handle = 0
  End If
  End Sub
  Private Sub Class_Terminate()
  Call Release
  End Sub
  Trackback: http://tb.blog.youkuaiyun.com/TrackBack.aspx?PostId=1342718

本文转自
http://blog.youkuaiyun.com/modest/archive/2006/10/20/1342718.aspx
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值