Hi,

I always seem to get errors with the APIs... sigh.

Right, yesterday code like this worked fine. Then I got a corrupted mdb with the code in and had to re-write it to the below.

The code below doesn't read the file. It's erroring with last dll error 5 (access denied) which is very weird as I have a segment of test code that I copied this from and that works.

So I must have missed something that is holding that file open.
Any ideas?


Code is to read byte by byte, find the vblf that doesn't have a vbcr before it and insert the vbcr, then output to a new file with the original name. Due to my vba code later to read the file and it failing due to no vbcrlf - original text file from a linux system I think...


Code:
            strFNSrc = strFN & "_Orig.txt"
            Name strFN As strFNSrc
            
            lngFH_Dest = apiFileCreate(strFN, FileDA_Generic_Read, FileSM_Read Or FileSM_Write, ByVal 0&, FileCD_OpenAlways, FileAtt_Normal, 0)
            lngFH_Src = apiFileCreate(strFNSrc, FileDA_Generic_Write, FileSM_Read Or FileSM_Write, ByVal 0&, FileCD_OpenAlways, FileAtt_Normal, 0)
            
            If lngFH_Src <> -1 And lngFH_Dest <> -1 Then
        
                lngBytesToRead = 4096
                lngBytesRed = lngBytesToRead
                
                'dblFileSize = GetTheFileSizeAPI(lngFH_Src)
                
                Do Until lngBytesRed < lngBytesToRead
                    ReDim aryBuffer(lngBytesToRead)
                    ReDim aryNewBuffer(lngBytesToRead + 512)
                
                    lngRes = apiFileRead(lngFH_Src, aryBuffer(0), lngBytesToRead, lngBytesRed, 0)
                    If lngRes = 0 Then Debug.Print Err.LastDllError
                    lngBytesToWrite = 0
                    
                    If lngBytesRed > 0 Then
                        For lngL = 0 To lngBytesRed - 1
                        
                            If aryBuffer(lngL) = 10 And Not blnPrevCR Then
                                aryNewBuffer(lngBytesToWrite) = 13
                                lngBytesToWrite = lngBytesToWrite + 1
                                aryNewBuffer(lngBytesToWrite) = aryBuffer(lngL)
                            Else
                                aryNewBuffer(lngBytesToWrite) = aryBuffer(lngL)
                            End If
                            lngBytesToWrite = lngBytesToWrite + 1
                        Next
                    Else
                        txtProgress = Format(Now, "hh:nn:ss") & " - Failed to copy data file" & vbCrLf & txtProgress
                    End If
                    
                    lngRes = apiFileWrite(lngFH_Dest, aryNewBuffer(0), lngBytesToWrite, lngBytesWritten, 0)

'---- store how much has been processed
                    dblFileDone = dblFileDone + lngBytesRed
                    
                    PercBar boxPerc, txtProgress, dblFileDone, dblFileSize
                Loop
                
                lngRes = apiCloseHandle(lngFH_Src)
                lngRes = apiCloseHandle(lngFH_Dest)
            End If
        End If