📄 module1.vb
字号:
Imports System.Runtime.InteropServices '引及Net框架中对底层操作支持的命名空间
Public Class clsDAMSMobileMarshal '我写的内存管理类
#Region "与内存有关的API声明"
REM 以下是与内存有关的移动设备API
Public Declare Function LocalAlloc Lib "coredll.dll" Alias "LocalAlloc" (ByVal wFlags As Int32, _
ByVal wBytes As Int32) As IntPtr
Public Declare Function LocalFree Lib "coredll.dll" Alias "LocalFree" (ByVal hMem As Int32) As Int32
Public Declare Function LocalLock Lib "coredll.dll" Alias "LocalLock" (ByVal hMem As Int32) As Int32
Public Declare Function LocalReAlloc Lib "coredll.dll" Alias "LocalReAlloc" (ByVal hMem As IntPtr, _
ByVal wBytes As Int32, ByVal wFlags As Int32) As IntPtr
#End Region
#Region "API常量声明"
Public Const LMEM_FIXED = 0
Public Const LMEM_MOVEABLE = &H2
Public Const LMEM_ZEROINIT = &H40
Public Const LPTR = LMEM_FIXED Or LMEM_ZEROINIT
#End Region
Public Shared Function fnAllocHLocal(ByVal ni_i32Size As Int32) As IntPtr
'申请本地内存,返回一个指向该内存块的指针
Return LocalAlloc(LPTR, ni_i32Size)
End Function
Public Shared Function fnFreeHLocal(ByRef ni_pLocal As IntPtr) As Int32
REM 释放指定的内存块柄
Dim ti32FunctionReturnValue As Int32
If ni_pLocal.Equals(IntPtr.Zero) = False Then
ti32FunctionReturnValue = (LocalFree(ni_pLocal.ToInt32))
If ti32FunctionReturnValue = 0 Then
ni_pLocal = IntPtr.Zero
End If
End If
Return (ti32FunctionReturnValue)
End Function
Public Shared Function fnReAllocHLocal(ByVal ni_pIn As IntPtr, ByVal ni_i32Size As Int32) As IntPtr
'对指定的内存块重新定义大小
Return LocalReAlloc(ni_pIn, ni_i32Size, LMEM_MOVEABLE)
End Function
Public Shared Function fnStringToHLocalUni(ByVal ni_strIn As String) As IntPtr
'将指定的字符串复制到一个内存块中,并返回该内存块的指针,这个指针必须使用fnFreeHLocal函数释放
Dim ti32StringBufLength As Int32
Dim tpTempA As IntPtr
If Not (ni_strIn Is Nothing) Then
If ni_strIn.Length = 0 Then
Return IntPtr.Zero
Else
ti32StringBufLength = (ni_strIn.Length + 1) * 2 ' 包括最后一个中止字符
tpTempA = fnAllocHLocal(ti32StringBufLength)
If tpTempA.Equals(IntPtr.Zero) = False Then '申请内存成功
Marshal.Copy(ni_strIn.ToCharArray, 0, tpTempA, ni_strIn.Length)
Return tpTempA
End If
End If
End If
End Function
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -