CHEPELSAPAG-6557 avatar image
0 Votes"
CHEPELSAPAG-6557 asked TvanStiphout answered

WriteFile() Function in VBA Excel not work correctly, it returns zero.

Hello! i'm using the updated library for 64bits modCOMM - Written by: David M. Hitchner but i can't send data for serial port. The writefile() function returns zero all the time.

Below I copy the entry to the function. The readfile function is very similar, but it works fine. Please if anyone can help me I would appreciate it.

------------------- here the code-------------

Public Function CommWrite(intPortID As Integer, strData As String) As Long

Dim i As Integer
Dim lngStatus As Long, lngSize As Long
Dim lngWrSize As Long, lngWrStatus As Long

 On Error GoTo Routine_Error
 ' Get the length of the data.
 lngSize = Len(strData)
 ' Output the data.
 lngWrStatus = WriteFile(udtPorts(intPortID).lngHandle, strData, lngSize, _
     lngWrSize, udtCommOverlap)

 ' Note that normally the following code will not execute because the driver
 ' caches write operations. Small I/O requests (up to several thousand bytes)
 ' will normally be accepted immediately and WriteFile will return true even
 ' though an overlapped operation was specified.
 If lngWrStatus = 0 Then
     lngStatus = GetLastError
· 7
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Do you know the definition of WriteFile and udtCommOverlap?

0 Votes 0 ·
 The library only defines:

' Writes data to a file and is designed for both synchronous and a
' synchronous operation. The function starts writing data to the file
' at the position indicated by the file pointer. After the write
' operation has been completed, the file pointer is adjusted by the
' number of bytes actually written, except when the file is opened with
' FILE_FLAG_OVERLAPPED. If the file handle was created for overlapped
' input and output (I/O), the application must adjust the position of
' the file pointer after the write operation is finished.
Declare PtrSafe Function WriteFile Lib "kernel32"
(ByVal hFile As Long, ByVal lpBuffer As String,

ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, _
lpOverlapped As OVERLAPPED) As Long

and I found this for the last parameter:

Internal As Long
InternalHigh As Long
offset As Long
OffsetHigh As Long
hEvent As Long
End Type

0 Votes 0 ·

Try changing the first parameter of WriteFile: ByVal hFile As LongPtr.

1 Vote 1 ·
Show more comments

Did you also find the udtCommOverlap variable?

0 Votes 0 ·
Show more comments

sorry, Too this:

' Program Storage

Private udtCommOverlap As OVERLAPPED
Private udtCommError As COMM_ERROR
Private udtPorts(1 To MAX_PORTS) As COMM_PORT

0 Votes 0 ·

1 Answer

TvanStiphout avatar image
0 Votes"
TvanStiphout answered

hEvent As LongPtr

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.