扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
//C语言 Long L; Long* pL = &L; *pL = 12; printf("L = %d *pL = %d", l, *pl); 'VB里 Dim pL As New pLong, L As Long pL.Attach L '也可以 pL.Ptr = VarPtr(L) pL = 12 Debug.Print "L ="; L; " *pL ="; pL |
Option Explicit '******************************************************** 'pLong.cls '包装一个Long型指针的类 '作者: 熊超 ID: AdamBear 2002年3月18日 'http://www.csdn.net/Author/AdamBear ' 你可以自由使用本类模块,不过请保留本声明 '******************************************************** Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long) Private m_Arr(0) As Long '缺省属性 Public Property Get Data() As Long Data = m_Arr(0) End Property Public Property Let Data(ByVal Value As Long) m_Arr(0) = Value End Property Public Sub Attach(Target As Long) Ptr = VarPtr(Target) End Sub Public Property Let Ptr(ByVal Target As Long) Dim pSA As Long '得到SafeArray结构指针pSA CopyMemory pSA, ByVal VarPtrArray(m_Arr), 4 '这个指针偏移12个字节后就是pvData指针 CopyMemory ByVal (pSA + 12), Target, 4 End Property Public Property Get Ptr() As Long Ptr = m_SA.pvData End Property Private Sub Class_Terminate() CopyMemory ByVal VarPtrArray(m_Arr), 0&, 4 End Sub |
Sub Main() Dim pB As New pByte, B As Byte Dim pI As New pInteger, I As Integer Dim pL As New pLong, L As Long '用Attach方法将经过类型检查,直接用Ptr属性则可以绕过类型检查 pB.Attach B pI.Attach I pL.Attach L '试试指针 B = 1 Debug.Print "B ="; B; " *pB ="; pB pB = 1 Debug.Print "B ="; B; " *pB ="; pB I = 1000 Debug.Print "I ="; I; " *pI ="; pI pI = 2000 Debug.Print "I ="; I; " *pI ="; pI L = 40000 Debug.Print "L ="; L; " *pL ="; pL pL = 60000 Debug.Print "L ="; L; " *pL ="; pL '试试C里的类型转换 '用Integer指针访问Long型变量 pI.Ptr = VarPtr(L) Debug.Print "*pI ="; pI End Sub |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者