This is a demo of using Named Pipes for interprocess communication in LB. It is translated almost directly from the Microsoft VB.NET example: http://support.microsoft.com/kb/871044
This has two programs, obviously to demonstrate the way it works. The first one I called PipeServer.bas, but the name doesn't really matter :P
openMode = _PIPE_ACCESS_DUPLEX or _FILE_FLAG_WRITE_THROUGH
pipeMode = _PIPE_WAIT or _PIPE_TYPE_MESSAGE or _PIPE_READMODE_MESSAGE
hPipe = CreateNamedPipe("MyPipe", openMode, pipeMode, 10, 10000, 2000, 10000, 0)
struct a, num aslong
BUFFSIZE =10000
buffer$ ="WHEE"
buffer$ = buffer$ +space$(BUFFSIZE -4)
print hPipe
If hPipe =0thengoto[end]Do
res = ConnectNamedPipe(hPipe, 0)
cbnCount =4
res$ = ReadFile$(hPipe, len(a.struct), cbnCount)
byteCount =val(res$)
print res$
If byteCount > BUFFSIZE then byteCount = BUFFSIZE
res = WriteFile(hPipe, buffer$, byteCount)
res = FlushFileBuffers(hPipe)
res = DisconnectNamedPipe(hPipe)Loop Until byteCount =0
a = CloseHandle(hPipe)[end]endFunction CreateNamedPipe(lpName$, dwOpenMode, dwPipeMode, nMaxInstances, nOutBufferSize, nInBufferSize, nDefaultTimeOut, lpSecurityAttributes)
lpName$ ="\\.\pipe\";lpName$
CallDLL #kernel32, "CreateNamedPipeA",_
lpName$ as ptr,_
dwOpenMode aslong,_
dwPipeMode aslong,_
nMaxInstances aslong,_
nOutBufferSize aslong,_
nInBufferSize aslong,_
nDefaultTimeOut aslong,_
lpSecurityAttributes aslong,_
CreateNamedPipe asulongEndFunctionFunction ConnectNamedPipe(hNamedPipe, lpOverlapped)
CallDLL #kernel32, "ConnectNamedPipe",_
hNamedPipe asulong,_
lpOverlapped aslong,_
ConnectNamedPipe aslongEndFunctionFunction DisconnectNamedPipe(hNamedPipe)
CallDLL #kernel32, "DisconnectNamedPipe",_
hNamedPipe asulong,_
DisconnectNamedPipe aslongEndFunctionFunction WriteFile(hFile, buf$, size)
struct num, bytesWritten aslong
CallDLL #kernel32, "WriteFile",_
hFile asulong,_
buf$ as ptr,_
size aslong,_
num as struct,_
lpOverlapped aslong,_
WriteFile aslongEndFunctionFunction ReadFile$(hFile, size, byref num)
buf$ =space$(size)
struct num, bytesRead aslong
CallDLL #kernel32, "ReadFile",_
hFile asulong,_
buf$ as ptr,_
size aslong,_
num as struct,_
lpOverlapped aslong,_
ret aslong
ReadFile$ =trim$(buf$)
num = num.bytesRead.structEndFunctionFunction FlushFileBuffers(hFile)
CallDLL #kernel32, "FlushFileBuffers",_
hFile asulong,_
FlushFileBuffers aslongEndFunctionFunction CloseHandle(hHandle)
CallDLL #kernel32, "CloseHandle",_
hHandle asulong,_
CloseHandle aslongEndFunction
Note that this program hangs while listening, the debugger says it hangs at the "ConnectNamedPipe" API call until another process writes something to it.
This one I named PipeClient. Please note that this isn't the snipped posted on the Microsoft site, this is just how I did it based on what they said(they said you can use CreateFile, ReadFile and WriteFile to work with it, then give a different example showing transacted pipes using CallNamedPipe. I do it the first way)
'Open the pipe
hPipe = OpenPipe("MyPipe")'This I was using to make sure it connected right.
Print hPipe
'This tells the "server" we'll have a buffer set up for 256 bytes.
str$ ="256"
size = len(str$)
Print WriteFile(hPipe, str$, size)'This actually reads the 256 bytes.'The num is passed byref, and will hold the number of bytes acutally read.'Because of the way the server is set up, this should print WHEE.
Print ReadFile$(hPipe, 256, num)'This should print 256 on the screen.
Print num
'The code on the server is set up so if it recieves a 0 as'the amount of bytes to send, it closes, so this is how you get the first one'out of the infinite loop and terminate.
Print WriteFile(hPipe, "0", 1)'All handles opened through windows have to be specifically closed.
a = CloseHandle(hPipe)Function OpenPipe(lpName$)'I guessed the parameters for this due to the similarity to'opening logical drives for reading/writing. That's also possible in'LB, and
This has two programs, obviously to demonstrate the way it works. The first one I called PipeServer.bas, but the name doesn't really matter :P
Note that this program hangs while listening, the debugger says it hangs at the "ConnectNamedPipe" API call until another process writes something to it.
This one I named PipeClient. Please note that this isn't the snipped posted on the Microsoft site, this is just how I did it based on what they said(they said you can use CreateFile, ReadFile and WriteFile to work with it, then give a different example showing transacted pipes using CallNamedPipe. I do it the first way)