Mailslot application does not receive messages 
Author Message
 Mailslot application does not receive messages

After several news posting, and reading, I have assembled a couple of
functions to create a mailslot, and send and receive messages.
It seems that all is working, except ReadFile() function in
This function allways returns 0, after GetMailSlotInfo() indicates that
there are messages to retrieve.

Anyone can help with it ?

Option Explicit

Private fhSlot As Long

Private Declare Function CloseHandle Lib "kernel32" _
    (ByVal hHandle As Long) As Long

Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, _
    lpBuffer As Any, nNumberOfBytesToRead As Long, _
    lpNumberOfBytesRead As Long, ByVal lpOverlapped As Long) As Long

Private Declare Function WriteFile Lib "kernel32" (ByVal hFileName As Long,
    ByVal lpBuff As Any, _
    ByVal nNrBytesToWrite As Long, lpNrOfBytesWritten As Long, _
    ByVal lpOverlapped As Long) As Long

Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" _
    (ByVal lpFileName As String, ByVal dwAccess As Long, _
    ByVal dwShare As Long, ByVal lpSecurityAttrib As Long, _
    ByVal dwCreationDisp As Long, ByVal dwAttributes As Long, _
    ByVal hTemplateFile As Long) As Long

Private Declare Function GetMailslotInfo Lib "kernel32" (ByVal hMailslot As
Long, _
    ByVal lpMaxMessageSize As Long, lpNextSize As Long, _
    lpMessageCount As Long, ByVal lpReadTimeout As Long) As Long

Private Declare Function CreateMailslot Lib "kernel32" Alias
"CreateMailslotA" _
    (ByVal lpName As String, ByVal nMaxMessageSize As Long, _
    ByVal lReadTimeout As Long, ByVal lpSecurityAttributes As Long) As Long

Private Const OPEN_EXISTING = 3
Private Const GENERIC_READ = &H80000000
Private Const GENERIC_WRITE = &H40000000
Private Const GENERIC_EXECUTE = &H20000000
Private Const GENERIC_ALL = &H10000000
Private Const FILE_SHARE_READ = &H1
Private Const FILE_SHARE_WRITE = &H2
Private Const MAILSLOT_WAIT_FOREVER = (-1)

Private Sub cmdCreate_Click()
   Dim mslotname As String

   mslotname$ = "\\.\mailslot\tst"
   fhSlot& = CreateMailslot(mslotname$, 0, MAILSLOT_WAIT_FOREVER, 0)
   If fhSlot& = INVALID_HANDLE_VALUE Then MsgBox "Error creating mailslot"
End Sub

Private Sub cmdReceive_Click()
   Dim stat As Long, bytesread As Long
   Dim mslotname As String, msg As String
   Dim sizemsg As Long, nummsgs As Long

   sizemsg& = 0
   nummsgs& = 0
   bytesread& = 0

   stat& = GetMailslotInfo(fhSlot&, 0, sizemsg&, nummsgs&, 0)
   If stat& = 0 Then MsgBox "Error in mailslotinfo"
   If nummsgs& = 0 Then MsgBox "No messages present"

   Do While sizemsg& <> 0
      msg$ = Chr$(0) & Space$(sizemsg&) & Space$(80)
      stat& = ReadFile(fhSlot&, msg$, sizemsg&, bytesread&, 0)
      If stat& = 0 Then
         MsgBox "Error reading mslot"
         Exit Do
      End If

      MsgBox "THE MESSAGE IS : ->" & msg$ & "<-"

      stat& = GetMailslotInfo(fhSlot&, 0, sizemsg&, nummsgs&, 0)
      If stat& = 0 Then MsgBox "Error in mailslotinfo"
      If nummsgs& = 0 Then MsgBox "No more messages"
End Sub

Private Sub cmdSend_Click()
   Dim stat As Long
   Dim msg As String
   Dim byteswritten As Long
   Dim mslotname As String
   Dim hFile As Long

   hFile& = CreateFile("\\*\mailslot\tst", GENERIC_WRITE, _
                      FILE_SHARE_READ, 0, OPEN_EXISTING, _
                      FILE_ATTRIBUTE_NORMAL, 0)
   If hFile& = INVALID_HANDLE_VALUE Then MsgBox "Error creating file"

   msg$ = "HELLO WORLD!"
   stat& = WriteFile(hFile&, msg$, Len(msg$) + 1, byteswritten, 0)
   If stat& = 0 Then MsgBox "Error writing file"

   stat& = CloseHandle(hFile&)
   If stat& = 0 Then MsgBox ("Error closing file")
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
   Dim stat&

   stat& = CloseHandle(fhSlot&)
End Sub

Sat, 24 Mar 2001 03:00:00 GMT  
 [ 2 post ] 

 Relevant Pages 

1. Winpopup MailsLots problem (not send message)

2. Help in sending and receiving messages to another application

3. Could Not Receive Menu Message

4. Outlook not receiving VFP6 Mapi session message

5. Messages not received from servers

6. ReadFile on Mailslot not working

7. VB MailSlot / WFW3.11 Mailslots error. Messages sent twice.

8. Can Access receive wmcopy message from Windows

9. Receiving multiple copies of same message

10. The reverse order of receiving messages

11. Receiving an error when trying to retrieve the message header

12. Tracking Orginal Message receive dates


Powered by phpBB® Forum Software