1 |
#Region "Microsoft.VisualBasic::1698c0af0197c40b47299d63cceef721, Microsoft.VisualBasic.Core\Extensions\Reflection\Marshal\MarshalExtensions.vb"
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
#End Region
|
41 |
|
42 |
Imports System.Runtime.CompilerServices
|
43 |
Imports Microsoft.VisualBasic.Emit.Marshal
|
44 |
|
45 |
|
46 |
|
47 |
</summary>
|
48 |
Public Module MarshalExtensions
|
49 |
|
50 |
|
51 |
Read unmanaged memory using memory pointer.
|
52 |
</summary>
|
53 |
<typeparam name="T">
|
54 |
<see cref="Integer"/>, <see cref="Char"/>, <see cref="Short"/>, <see cref="Long"/>, <see cref="Single"/>, <see cref="Byte"/>, <see cref="IntPtr"/>, <see cref="Double"/>
|
55 |
</typeparam>
|
56 |
<param name="p"></param>
|
57 |
<returns></returns>
|
58 |
<remarks>
|
59 |
```vbnet
|
60 |
Public Shared Sub Copy(source As IntPtr, destination() As Integer, startIndex As Integer, length As Integer)
|
61 |
Public Shared Sub Copy(source As IntPtr, destination() As Char, startIndex As Integer, length As Integer)
|
62 |
Public Shared Sub Copy(source As IntPtr, destination() As Short, startIndex As Integer, length As Integer)
|
63 |
Public Shared Sub Copy(source As IntPtr, destination() As Long, startIndex As Integer, length As Integer)
|
64 |
Public Shared Sub Copy(source As IntPtr, destination() As Single, startIndex As Integer, length As Integer)
|
65 |
Public Shared Sub Copy(source As IntPtr, destination() As Byte, startIndex As Integer, length As Integer)
|
66 |
Public Shared Sub Copy(source As IntPtr, destination() As IntPtr, startIndex As Integer, length As Integer)
|
67 |
Public Shared Sub Copy(source As IntPtr, destination() As Double, startIndex As Integer, length As Integer)
|
68 |
```
|
69 |
</remarks>
|
70 |
<Extension> Public Function MarshalAs(Of T)(p As System.IntPtr, chunkSize As Integer) As IntPtr(Of T)
|
71 |
Select Case GetType(T)
|
72 |
Case GetType(Integer) : Return DirectCast(CType(New [Integer](p, chunkSize), Object), IntPtr(Of T))
|
73 |
Case GetType(Char) : Return DirectCast(CType(New [Char](p, chunkSize), Object), IntPtr(Of T))
|
74 |
Case GetType(Short) : Return DirectCast(CType(New [Short](p, chunkSize), Object), IntPtr(Of T))
|
75 |
Case GetType(Long) : Return DirectCast(CType(New [Long](p, chunkSize), Object), IntPtr(Of T))
|
76 |
Case GetType(Single) : Return DirectCast(CType(New [Single](p, chunkSize), Object), IntPtr(Of T))
|
77 |
Case GetType(Byte) : Return DirectCast(CType(New [Byte](p, chunkSize), Object), IntPtr(Of T))
|
78 |
Case GetType(System.IntPtr) : Return DirectCast(CType(New IntPtr(p, chunkSize), Object), IntPtr(Of T))
|
79 |
Case GetType(Double) : Return DirectCast(CType(New [Double](p, chunkSize), Object), IntPtr(Of T))
|
80 |
Case Else
|
81 |
Throw New MemberAccessException(GetType(T).FullName & " is not a valid memory region!")
|
82 |
End Select
|
83 |
End Function
|
84 |
|
85 |
|
86 |
方便进行数组操作的一个函数
|
87 |
</summary>
|
88 |
<typeparam name="T"></typeparam>
|
89 |
<param name="raw"></param>
|
90 |
<param name="p"></param>
|
91 |
<returns></returns>
|
92 |
<Extension> Public Function MarshalAs(Of T)(ByRef raw As T(), Optional p As System.IntPtr = Nothing) As IntPtr(Of T)
|
93 |
Return New IntPtr(Of T)(raw, p)
|
94 |
End Function
|
95 |
End Module
|