我运行下面的VB就退出,不知是不是VB做的类型检查导致的?
dim test as long,test2 as string
test=65537
test2="    "
    CopyMemory MemPoint, VarPtr(test), 4
    CopyMemory VarPtr(test2), MemPoint, 4

解决方案 »

  1.   

    贴上完整的代码Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal pDst As Any, ByVal pSrc As Any, ByVal ByteLen As Long)'API memory functions
    Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
    Private Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long
    Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
    Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long'constants for API memory functions
    Private Const GMEM_MOVEABLE = &H2
    Private Const GMEM_ZEROINIT = &H40
    Private Const GHND = (GMEM_MOVEABLE Or GMEM_ZEROINIT)Dim pHnd As Long
    Dim MemPoint As LongPrivate Sub cmd_Alloc_Click()
        Dim i As Long
        Dim test As Long
        Dim test2 As string
        Dim ReadMem As Byte
        
        pHnd = GlobalAlloc(GHND, 8)
        MemPoint = GlobalLock(pHnd)
        
        test = 65537
        test2 = "    "
        CopyMemory MemPoint, VarPtr(test), 4
        CopyMemory VarPtr(test2), MemPoint, 4
        
        GlobalFree pHnd
    End Sub
      

  2.   

    你这个不是复制,是类型转换,即Long->String,用Copymenory()是错的
    用VB代码实现:
    test2=Cstr(test)如果你要保持长度为4的话
    test2=right("   " & cstr(test),4)
      

  3.   

    我上次读文件的时候也遇到过问题,就是byte和string*1,都是0-255的,这都会出问题,
    更别说integer了,它还包括负数的啊。。怎么可以这样。
      

  4.   

    呵呵此问题已知答案
    经返回试验,在CopyMemory 时VB会做强制类型检查。