当前位置: 首页 > 图文教程 > .Net技术 > VB.NET > VB 内存分配与流读写 代码

VB.NET
大话“基于对象”与“面向对象”
VB 内存分配与流读写 代码
VB.NET语法基础
vb.net与c#.net区别
用VB.Net读写数据库
百万程序员的苦恼-选择VB.NET还是C#
VB.net 多线程实例
VB.NET也区会大小写
VB.NET 2005编写定时关机程序
VB.NET控件编程定制拦截击键动作
如何实现.net程序的进程注入
VB.NET 菜单设计初级入门
VB.NET中关于DataGrid颜色的自定义
VB网络编程中Winsock的使用
VB.net编程教程:编写文字加解密程序
VB.NET中快速访问注册表技巧
VB.NET:在VB.NET中串行化对象
VB.NET:在 VB.NET 编程中使用数组
VB.NET:VB.NET路在何方?
大话“基于对象”与“面向对象”

VB.NET 中的 VB 内存分配与流读写 代码


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 271 ::
收藏到网摘: n/a

ption Explicit
Private Declare Function VirtualAlloc Lib "kernel32" (ByVal lpAddress As LongByVal dwSize As LongByVal flAllocationType As LongByVal flProtect As LongAs Long
Private Declare Function VirtualFree Lib "kernel32" (ByVal lpAddress As LongByVal dwSize As LongByVal dwFreeType As LongAs Long
Private Declare Function VirtualLock Lib "kernel32" (ByVal lpAddress As LongByVal dwSize As LongAs Long
Private Declare Function VirtualUnlock Lib "kernel32" (ByVal lpAddress As LongByVal dwSize As LongAs Long
Private Declare Function IsBadReadPtr Lib "kernel32" (ByVal lp As LongByVal ucb As LongAs Long
Private Declare Function IsBadWritePtr Lib "kernel32" (ByVal lp As LongByVal ucb As LongAs Long
Private Declare Function IsBadStringPtr Lib "kernel32" Alias "IsBadStringPtrA" (ByVal lpsz As LongByVal ucchMax As LongAs Long
Private Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" (ByVal lpStringDest As StringByVal lpStringSrc As LongAs Long
Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As LongAs Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal pDest As LongByVal pSrc As LongByVal 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 LongByVal 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 LongByVal 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