VB 内存分配与流读写 代码
来源:互联网 发布:二维码红包生成软件 编辑:程序博客网 时间:2024/05/16 11:22
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
'Powered by barenx
'分配内存
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
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
'Powered by barenx
'分配内存
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
- VB 内存分配与流读写 代码
- 内存的分配方式与代码耦合度
- VB.net 读写内存问题
- 【VB.NET 2008】内存读写
- VB INI 读写类 代码
- 静态内存分配与动态内存分配
- 指针与内存分配
- 内存分配 与 const
- 分配与释放内存
- 堆栈与内存分配
- 指针与内存分配
- 变量与内存分配
- 内存分配与初始化
- 管理与分配内存
- 内存分配与释放
- 内存处理与分配
- C++栈区的变量分配 与 内存读写越界问题引发的coredump问题
- 关于用VB读写内存的方法
- 优先查找树介绍
- 在JBOSS AS里安装JBOSSWS
- 编程题: 写一个Singleton出来。
- ORACLE学习
- 查询记录中没有记录的就返回空,否则返回原值
- VB 内存分配与流读写 代码
- jsp servlet 中文完全解决方案
- 长尾理论
- VIM 常用插件推荐
- sql的left join 命令详解与中Case 的不同用法
- 10年后重返校园的感受
- 使用gcc在Aix5.1上安装二进制的perl-5.8.8
- C#中GridView的排序用法
- C# 泛型