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

VB.NET
[转]全面剖析VB.NET技术(1)
[转]全面剖析VB.NET技术(2)
基于VB.NET技术的表达式计算器
VB.NET多线程技术及其实现
走近VB.Net VB.Net问答全集
如何应用VB.NET MonthCalendar控件
VB.NET 拖动无边框窗体编程实例
.Net 虚拟框架的实现原理
实现将数字转换为汉字大写
vb.net access xml file
vb.net入门:MDI 窗体的基础使用
C#、VB.NET使用Windows API控制系统音量及静音
vb.net access xml file
VB.NET的阳历与农历转换的算法
用VB创建FTP组件(get)
vb中利用xmlhttp来下载远程文件
.net中快捷键定义
.NET对PE结构的扩展
VB.net中Delegate和Event
VB.NET DES 加密/解密类库,支持文件和中文/UNICODE字符,返回BASE64编码字符串

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 270 ::
收藏到网摘: 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