M
Marcel
Hi,
I'm trying to use a Named Pipe in Word2003. I will let Word talks via this
pipe with another application that serves as an pipe server. I have this
working in two other programming laguages Xbasic and PureBasic. Now I want
add VBA to it
The API function (Visual Basic version) doesn't work in VBA and let Word
crash is the following:
Public Declare Function CallNamedPipe Lib "kernel32" Alias "CallNamedPipeA"
_
(ByVal lpNamedPipeName As String, _
ByRef lpInBuffer As Any, _
ByVal nInBufferSize As Long, _
ByRef lpOutBuffer As Any, _
ByVal nOutBufferSize As Long, _
ByRef lpBytesRead As Long, _
ByVal nTimeOut As Long) As Long
When I change all the Longs in Integers and call it as an CallNamedPipeA
version it doesn't crash anymore and the communication does something.
Declare Function CallNamedPipeA Lib "kernel32.dll" ( _
ByVal lpNamedPipeName As String, _
lpInBuffer As Any, _
ByVal nInBufferSize As Integer, _
lpOutBuffer As Any, _
ByVal nOutBufferSize As Integer, _
lpBytesRead As Integer, _
ByVal nTimeOut As Integer) As Integer
Both versions do get a connection to the pipe server which is running at
that moment. The ReadFile() action in the server doesn't return with
success, res = 0. The server does work correct in connection to other
programs.
-------------- start of VBA code -----------
Option Explicit
Private Const pipeName As String = "\\.\pipe\mynamedpipe"
Private Const BUFFSIZE = 1024
Private hpipe As Integer
Private myerror As Integer
Public Const INVALID_HANDLE_VALUE As Integer = -1
Declare Function CallNamedPipeA Lib "kernel32.dll" ( _
ByVal lpNamedPipeName As String, _
lpInBuffer As Any, _
ByVal nInBufferSize As Integer, _
lpOutBuffer As Any, _
ByVal nOutBufferSize As Integer, _
lpBytesRead As Integer, _
ByVal nTimeOut As Integer) As Integer
Public Sub CallAPipe()
Dim res As Integer, cbRead As Integer, numBytes As Integer
Dim lpInBuffer As String
Dim lpOutBuffer As String
lpInBuffer = "Some text from Word."
numBytes = Len(lpInBuffer)
lpOutBuffer = String(BUFFSIZE, 0)
res = CallNamedPipeA(pipeName, lpInBuffer, numBytes, lpOutBuffer, BUFFSIZE,
cbRead, 0)
If res = 0 Then
myerror = Err.LastDllError
MsgBox "Error number " & myerror & Error(myerror), vbOKOnly
Else
MsgBox "Ok"
End If
End Sub
-------------- end of vba code -----
Is there someone who has done this before and can point me in the right
direction with this code? All kind of help with this code is appreciated.
Regards,
Marcel
I'm trying to use a Named Pipe in Word2003. I will let Word talks via this
pipe with another application that serves as an pipe server. I have this
working in two other programming laguages Xbasic and PureBasic. Now I want
add VBA to it
The API function (Visual Basic version) doesn't work in VBA and let Word
crash is the following:
Public Declare Function CallNamedPipe Lib "kernel32" Alias "CallNamedPipeA"
_
(ByVal lpNamedPipeName As String, _
ByRef lpInBuffer As Any, _
ByVal nInBufferSize As Long, _
ByRef lpOutBuffer As Any, _
ByVal nOutBufferSize As Long, _
ByRef lpBytesRead As Long, _
ByVal nTimeOut As Long) As Long
When I change all the Longs in Integers and call it as an CallNamedPipeA
version it doesn't crash anymore and the communication does something.
Declare Function CallNamedPipeA Lib "kernel32.dll" ( _
ByVal lpNamedPipeName As String, _
lpInBuffer As Any, _
ByVal nInBufferSize As Integer, _
lpOutBuffer As Any, _
ByVal nOutBufferSize As Integer, _
lpBytesRead As Integer, _
ByVal nTimeOut As Integer) As Integer
Both versions do get a connection to the pipe server which is running at
that moment. The ReadFile() action in the server doesn't return with
success, res = 0. The server does work correct in connection to other
programs.
-------------- start of VBA code -----------
Option Explicit
Private Const pipeName As String = "\\.\pipe\mynamedpipe"
Private Const BUFFSIZE = 1024
Private hpipe As Integer
Private myerror As Integer
Public Const INVALID_HANDLE_VALUE As Integer = -1
Declare Function CallNamedPipeA Lib "kernel32.dll" ( _
ByVal lpNamedPipeName As String, _
lpInBuffer As Any, _
ByVal nInBufferSize As Integer, _
lpOutBuffer As Any, _
ByVal nOutBufferSize As Integer, _
lpBytesRead As Integer, _
ByVal nTimeOut As Integer) As Integer
Public Sub CallAPipe()
Dim res As Integer, cbRead As Integer, numBytes As Integer
Dim lpInBuffer As String
Dim lpOutBuffer As String
lpInBuffer = "Some text from Word."
numBytes = Len(lpInBuffer)
lpOutBuffer = String(BUFFSIZE, 0)
res = CallNamedPipeA(pipeName, lpInBuffer, numBytes, lpOutBuffer, BUFFSIZE,
cbRead, 0)
If res = 0 Then
myerror = Err.LastDllError
MsgBox "Error number " & myerror & Error(myerror), vbOKOnly
Else
MsgBox "Ok"
End If
End Sub
-------------- end of vba code -----
Is there someone who has done this before and can point me in the right
direction with this code? All kind of help with this code is appreciated.
Regards,
Marcel