Dim lngMaxRecord As Long
    Dim sngValue As Single
    Dim i As Long
    Dim l As Long
    Dim Result As Long
    
    ReDim m_nSendBuf(1 To 7) As Long
    
    Dim dTICrecord  As TICRecord2
    
    If OpenPort Then
        ProgressBar1.Value = 0
        ReadClockRecordTotal hport, lngMaxRecord
        sngValue = 100 / lngMaxRecord
        Label4.Caption = "共有﹕" & lngMaxRecord & " 條記錄"
            
        Call CopyMemory(m_nSendBuf(1), dTICrecord, 50)
        
        Result = BatchReadRecord(hport, VarPtr(m_nSendBuf(1)))
        
        Call CopyMemory(dTICrecord, m_nSendBuf(1), 50)
        
        Debug.Print "讀取到數據﹕" & Result
        Debug.Print dTICrecord.Card
        Debug.Print dTICrecord.CardTimes
        Debug.Print dTICrecord.Mark
        Debug.Print dTICrecord.TimeString
        Debug.Print dTICrecord.CardGroup
        Debug.Print dTICrecord.Flag
        Debug.Print dTICrecord.EmpID
        
  我在運行以上代碼時沒有任何問題﹐但只運行一次能正常退出﹐多于一次時﹐退出就會出“該記憶體不能為"Written"的錯誤﹐請問各位大哥﹐是怎么回事來的﹖    End If

解决方案 »

  1.   

    redim的数组空间释放掉,程序退出的时候
    erase m_nSendBuf
      

  2.   

    再請問一下﹐運行時有時會出“該記憶體不能為"Written"的錯誤﹐這個又是怎么引起的﹖
      

  3.   

    不好意意﹐打錯﹐更正一下
    再請問一下﹐運行時有時會出“該記憶體不能為"Read"的錯誤﹐這個又是怎么引起的﹖
      

  4.   

    to : yinweihong(真名:尹伟红)
       redim的数组空间释放掉,程序退出的时候
       erase m_nSendBuf我使用這個后﹐雖不報不能為"Written"的錯誤﹐但現在變成了“該記憶體不能為"Read"的錯誤﹐請問怎么解決啊﹖
      

  5.   

    最好跟踪一下到底是哪一句的问题先.你CopyMemory可能会因为数据长度问题而出错,而BatchReadRecord函数的调用也是可能会出错的,所以你最好先确定哪句有问题
      

  6.   

    原先是這樣的
    Call CopyMemory(m_nSendBuf(1), dTICrecord, LenB(dTICrecord))
            
    Result = BatchReadRecord(hport, VarPtr(m_nSendBuf(1)))
            
    Call CopyMemory(dTICrecord, m_nSendBuf(1), LenB(dTICrecord))但這樣也是會出這種錯誤﹐所以我換成了50
      

  7.   

    這個是BatchReadRecord 函數的定議
    '批量讀數據(880 1.28以上版本)
    Declare Function BatchReadRecord Lib "KN200.dll" (ByVal hport As Long, ByVal Records As Any) As Longhport是一個端口地址﹐這個沒有問題
    Records 是取得的數據,是自定義結構類型﹐
    Type TICRecord2
        Card As String * 19
        TimeString As String * 16
        EmpID As String * 2
        Mark As Long
        Flag As Long
        CardTimes As Long
        CardGroup As Long
    End TypeRecords 取得的數據只是一個內存地址
      

  8.   

    可能结构有问题
    as string * n 都换成 as byteType TICRecord2
        Card(18) As Byte
        TimeString(15) As Byte
        EmpID(1) As Byte
        Mark As Long
        Flag As Long
        CardTimes As Long
        CardGroup As Long
    End Type
      

  9.   

    回复人: supergreenbean(超级绿豆 - 天气酷热,把我煮了熬汤吧) ( ) 信誉:157  2005-09-16 11:12:00  得分: 0  
     
     
       可能结构有问题
    as string * n 都换成 as byteType TICRecord2
        Card(18) As Byte
        TimeString(15) As Byte
        EmpID(1) As Byte
        Mark As Long
        Flag As Long
        CardTimes As Long
        CardGroup As Long
    End Type
      
     
    換了﹐還是不行
      

  10.   

    有一段Delphi的代碼﹐但我在vb中不知如何轉為正確的代碼﹕function ReadTextLine: Integer;
      var
         i: Integer;
         Buf: array[0..$10] of TICRecord2;
      begin
        Result := 0;
        if bFast and((pclk.bIs880 and(pclk.Fireware>=FW128))or(Is780(pclk.DeviceType))) then 
        begin
          Flag := 0;
          FillChar(Buf, SizeOf(Buf), 0);
          Result := BatchReadRecord(hPort, Buf);
          if Result>= then
          ...................
    我依上面的代碼在Delphi調試是一點事都沒﹐我懷疑是 
    Buf: array[0..$10] of TICRecord2 
    FillChar(Buf, SizeOf(Buf), 0);
    Result := BatchReadRecord(hPort, Buf);
    這個調用的方式有點問題﹐BatchReadRecord這個是一次取16條記錄的﹐而我定義的是一次取一條﹐但在vb中定義時﹐我不知從何下手啊
      

  11.   

    Declare Function BatchReadRecord Lib "KN200.dll" (ByVal hport As Long, ByVal Records As Any) As Long
    结构体类型传参用Byref吧
      

  12.   

    Buf: array[0..$10] of TICRecord2 
    FillChar(Buf, SizeOf(Buf), 0);//创建字符缓冲区,同const BufSize=sizeof(buf)  buf=string(bufsize,0)
    Result := BatchReadRecord(hPort, Buf);//那里有一次取16條記錄的啊????
      

  13.   

    回复人: yinweihong(真名:尹伟红) ( ) 信誉:137  2005-09-16 12:54:00  得分: 0  
     
     
       Buf: array[0..$10] of TICRecord2 
    FillChar(Buf, SizeOf(Buf), 0);//创建字符缓冲区,同const BufSize=sizeof(buf)  buf=string(bufsize,0)
    Result := BatchReadRecord(hPort, Buf);//那里有一次取16條記錄的啊???? 不好意思﹐沒注明﹐我使用這個函數讀取數據時﹐就會讀取16條記錄
     
      

  14.   

    没,用len,呵呵
    结构体类型传参用Byref
      

  15.   

    剛試了直接使用結構不行﹐只能取內存中的值
     Call CopyMemory(m_nSendBuf(1), dTICrecord, LenB(dTICrecord))        
     Result = BatchReadRecord(hport, VarPtr(m_nSendBuf(1)))        
     Call CopyMemory(dTICrecord, m_nSendBuf(1), LenB(dTICrecord))
    這樣子能取到值﹐但就是會常常來個“該記憶體不能為"Written"的錯誤
      

  16.   

    从delphi程序看,该函数被调用时并没有什么用来表示TICRecord2个数的参数,因此,我怀疑是否必须要连续声明16个结构
      

  17.   

    ........    
        ReDim Buf(1 To 4) As TICRecord2             '定義四個結構
        ReDim m_nSendBuf(1 To 28) As Long           '每個結構有7組地址 4*7=28
                       
        BufSize = Len(Buf(1))
        Buf = String(BufSize, 0)           
        Result = BatchReadRecord(hport, VarPtr(m_nSendBuf(1)))        //將取得的記錄地址存入nSendBuf(1) 
        Call CopyMemory(Buf(1), m_nSendBuf(1), Len(Buf(1)))           //從地址m_nSendBuf(1)中取出值存入Buf(1)中我定義了四個結構﹐只能取到一個結構的數據﹐本是將  m_nSendBuf(1 to 28)地址里面的值全給結構buf(1 to 4) 的,但現在只給到了結構Buf(1)裏面,我查過m_nSendBuf(1 to 28)裏面是有值的,CopyMemory不能將它們全部給多個結構嗎?
      

  18.   

    呵呵 我說得太亂了,我依你們的建議改動了一下,現在是程序運行時沒問題,但我退出程序就會出該記憶體不能為"Written"或"Read"的錯誤﹐我想來想去應該是用CopyMemory的地方有問題
      

  19.   

    已用erase m_nSendBuf 將数组空间释放掉,但重複執行讀取資料後,退出時就會出錯
      

  20.   

    我突然发现你的m_nSendBuf类型是 Long  而不是 Byte阿门啊~~~我眼睛有问题了啊~~
      

  21.   

    實在受不了了,用vb整不出來,改用delphi做了個讀取程序,在vb里面調用了,各位說說看,用vb碰到這種操作指針問題就頭大,vb真的能用指針來寫一個穩定的程序嗎?
      

  22.   

    呵呵,請問一下,vb中一般用指針寫什么程序啊?感覺用vb做做mis系統差不多,底層的東東vb做起來沒vc等好用
      

  23.   

    在与其他api交互的程序里用的比较多