VB 内存分配与流读写 代码

qizhicong2 2009-08-14 09:45:33
Option Explicit
Private Declare Function VirtualAlloc()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()Function VirtualFree Lib "kernel32" (ByVal lpAddress As Long, ByVal dwSize As Long, ByVal dwFreeType As Long) As Long
Private Declare Function VirtualLock()Function VirtualLock Lib "kernel32" (ByVal lpAddress As Long, ByVal dwSize As Long) As Long
Private Declare Function VirtualUnlock()Function VirtualUnlock Lib "kernel32" (ByVal lpAddress As Long, ByVal dwSize As Long) As Long
Private Declare Function IsBadReadPtr()Function IsBadReadPtr Lib "kernel32" (ByVal lp As Long, ByVal ucb As Long) As Long
Private Declare Function IsBadWritePtr()Function IsBadWritePtr Lib "kernel32" (ByVal lp As Long, ByVal ucb As Long) As Long
Private Declare Function IsBadStringPtr()Function IsBadStringPtr Lib "kernel32" Alias "IsBadStringPtrA" (ByVal lpsz As Long, ByVal ucchMax As Long) As Long
Private Declare Function lstrcpy()Function lstrcpy Lib "kernel32" Alias "lstrcpyA" (ByVal lpStringDest As String, ByVal lpStringSrc As Long) As Long
Private Declare Function lstrlen()Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As Long) As Long
Private Declare Sub CopyMemory()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()Property Get Handle() As Long
Handle = m_Handle
End Property

''分配内存
Public Sub Allocate()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()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()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()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()Sub Class_Terminate()
Call Release
End Sub






-
欢迎光临DIY部落,点击这里查看更多文章教程   
文章出处:http://www.diybl.com/course/3_program/vb/2008331/107844.html
...全文
127 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
嗷嗷叫的老马 2009-08-14
  • 打赏
  • 举报
回复
内存操作函数的基础用法,顶.

而且你复制时函数声明也出了问题.

不纠正就发上来,不知道的人会对你的站会有什么映象?
贝隆 2009-08-14
  • 打赏
  • 举报
回复
学习

2,461

社区成员

发帖
与我相关
我的任务
社区描述
VBA(Visual Basic for Applications)是Visual Basic的一种宏语言,是在其桌面应用程序中执行通用的自动化(OLE)任务的编程语言。
社区管理员
  • VBA
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧