📄 marshal.vb
字号:
Imports System
Imports interopserv = System.Runtime.InteropServices
Imports System.ComponentModel
'Namespace NetCF.Runtime.InteropServices
Public NotInheritable Class Marshal
Private Class WinApi
Public Const LMEM_FIXED As Long = 0
Public Const LMEM_MOVEABLE As Long = 2
Public Const LMEM_ZEROINIT As Long = &H40
Public Const LPTR As Long = LMEM_FIXED Or LMEM_ZEROINIT
'private constructor prevents instantiation
Private Sub New()
End Sub
' imported functions
<System.Runtime.InteropServices.DllImport("coredll.dll", SetLastError:=True)> _
Public Shared Function LocalAlloc(ByVal uFlags As System.Int32, ByVal uBytes As System.Int32) As IntPtr
End Function
<System.Runtime.InteropServices.DllImport("coredll.dll", SetLastError:=True)> _
Public Shared Function LocalFree(ByVal hMem As IntPtr) As IntPtr
End Function
<System.Runtime.InteropServices.DllImport("coredll.dll", SetLastError:=True)> _
Public Shared Function LocalReAlloc(ByVal hMem As IntPtr, ByVal uBytes As System.Int64, ByVal fuFlags As System.Int64) As IntPtr
End Function
End Class 'WinApi
'private contructor prevents instantiation
Private Sub New()
End Sub
'memory allocation / deallocation methods
'/ Allocates a block of memory using LocalAlloc.
'/ <param name="cb">The number of bytes in memory required.</param>
'/ <returns>
'/ An IntPtr to the newly allocated memory. This memory must be
'/ released using the Marshal.FreeHLocal method.
'/ </returns>
'/ <exception cref="OutOfMemoryException">
'/ There is insufficient memory to satisfy the request.
'/ </exception>
'/ <remarks>
'/ AllocHlocal exposes the LocalAlloc WinCE API from CoreDll.dll.
'/ For additional information about LocalAlloc, see the MSDN Library.
Public Shared Function AllocHLocal(ByVal cb As Int32) As IntPtr
Try
Return WinApi.LocalAlloc(WinApi.LPTR, cb)
Catch ex As Exception
End Try
End Function
'/ Frees memory previously allocated from the unmanaged memory
'/ of the process with AllocHLocal.
'/ <param name="hlocal">The handle returned by the original matching call to AllocHLocal.</param>
'/ <exception cref="Win32Exception">
'/ Indicates failure. The exception contains the error code obtained from GetLastError.
'/ </exception>
'/ <remarks>
'/ You can use FreeHlocal to free any memory from the heap allocated by AllocHLocal or
'/ ReAllocHLocal, or any equivalent unmanaged API method. If the hlocal parameter is a
'/ null reference (Nothing in Visual Basic), the method does nothing. FreeHlOCAL exposes
'/ the LocalFree function from CoreDll.DLL, which frees all bytes so that you can no longer
'/ use the memory pointed to by <paramref name="hLocal"/>. For additional information about
'/ LocalFree, see the MSDN Library.
Public Shared Sub FreeHLocal(ByVal hlocal As IntPtr)
'If hlocal <> IntPtr.Zero Then
If hlocal.ToInt32 <> 0 Then
'If IntPtr.Zero <> WinApi.LocalFree(hlocal) Then
If WinApi.LocalFree(hlocal).ToInt32 <> 0 Then
Throw New Win32Exception(interopserv.Marshal.GetLastWin32Error())
End If
hlocal = IntPtr.Zero
End If
End Sub
'/ Resizes a block of memory previously allocated with AllocHLocal.
'/ <param name="pv">A pointer to memory allocated with AllocHLocal.</param>
'/ <param name="cb">The new size of the allocated block.</param>
'/ <returns>
'/ An IntPtr to the reallocated memory. This memory must be released
'/ using Marshal.FreeHLocal.
'/ </returns>
'/ <exception cref="OutOfMemoryException">
'/ There is insufficient memory to satisfy the request.
'/ </exception>
'/ <remarks>
'/ ReAllocHLocal exposes the LocalRealloc WinCE API method from CoreDll.dll.
'/ The returned pointer can differ from the original. For additional information
'/ about LocalAlloc, see the MSDN Library.
'/ </remarks>
Public Shared Function ReAllocHLocal(ByVal pv As IntPtr, ByVal cb As Integer) As IntPtr
Dim newMem As IntPtr = WinApi.LocalReAlloc(pv, CType(cb, System.Int64), WinApi.LMEM_MOVEABLE)
'If newMem = IntPtr.Zero Then
If newMem.ToInt32 = 0 Then
Throw New OutOfMemoryException
End If
Return newMem
End Function
'/ Copies the contents of a managed String into unmanaged memory.
'/ <param name="s">A managed string to be copied.</param>
'/ <returns>
'/ The address, in unmanaged memory, to where the s was copied, or 0 if a null reference
'/ (Nothing in Visual Basic) string was supplied.
'/ </returns>
Public Shared Function StringToHLocalUni(ByVal s As String) As IntPtr
If s Is Nothing Then
Return IntPtr.Zero
Else
Dim nc As Integer = s.Length
Dim len As Integer = 2 * (1 + nc)
Dim hLocal As IntPtr = AllocHLocal(len)
'If hLocal = IntPtr.Zero Then
If hLocal.ToInt32 = 0 Then
Throw New OutOfMemoryException
Else
interopserv.Marshal.Copy(s.ToCharArray(), 0, hLocal, s.Length)
Return hLocal
End If
End If
End Function 'StringToHLocalUni
Public Shared Function IntToHLocalUni(ByVal s As Int32) As IntPtr
If s = 0 Then
Return IntPtr.Zero
Else
Dim nc As Integer = interopserv.Marshal.SizeOf(s)
Dim len As Integer = 2 * (1 + nc)
Dim hLocal As IntPtr = AllocHLocal(len)
'If hLocal = IntPtr.Zero Then
If hLocal.ToInt32 = 0 Then
Throw New OutOfMemoryException
Else
'interopserv.Marshal.Copy(s.ToString, 0, hLocal, nc)
Return hLocal
End If
End If
End Function 'StringToHLocalUni
End Class 'Marshal
'End Namespace 'NetCF.Runtime.InteropServices
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -