469,142 Members | 1,138 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,142 developers. It's quick & easy.

Mailslots in VB.NET

1
Hello,
I am having trouble finding much information about how to implement Mailslots with VB.NET.

This code works successfully when run on the same PC for all three steps: Create Mailslot, Write to Mailslot, and Read Mailslot.

I can also successfully (as in, no errors) create a mailslot (on the 'server'), write to it from a different PC (the 'client'), but when I try to read the mailslot on the 'server' I get:

"An unhandled exception of type 'System.NullReferenceException' occurred in MailSlot.exe
Additional information: Object reference not set to an instance of an object."

Prior to this I successfully use GetMailslotInfo to get the size of the message received by the 'server'.

I can't believe I haven't been able to find the answer on the net!

Below is my code.

Expand|Select|Wrap|Line Numbers
  1.     Declare Function CreateMailslot Lib "kernel32" Alias "CreateMailslotA" _
  2.             (ByVal lpName As String, _
  3.              ByVal nMaxMessageSize As Integer, _
  4.              ByVal lReadTimeout As Integer, _
  5.              ByRef lpSecurityAttributes As SECURITY_ATTRIBUTES) _
  6.              As IntPtr
  7.  
  8.     Declare Function GetMailslotInfo Lib "kernel32" _
  9.             (ByVal hMailslot As IntPtr, _
  10.              ByRef lpMaxMessageSize As Integer, _
  11.              ByRef lpNextSize As Integer, _
  12.              ByRef lpMessageCount As Integer, _
  13.              ByRef lpReadTimeout As Integer) _
  14.              As IntPtr
  15.  
  16.     Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" _
  17.             (ByVal lpFileName As String, _
  18.              ByVal dwDesiredAccess As Integer, _
  19.              ByVal dwShareMode As Integer, _
  20.              ByRef lpSecurityAttributes As SECURITY_ATTRIBUTES, _
  21.              ByVal dwCreationDisposition As Integer, _
  22.              ByVal dwFlagsAndAttributes As Integer, _
  23.              ByVal hTemplateFile As Integer) _
  24.              As IntPtr
  25.  
  26.     Declare Function WriteFile Lib "kernel32" _
  27.             (ByVal hFile As IntPtr, _
  28.              ByRef lpBuffer As Object, _
  29.              ByVal nNumberOfBytesToWrite As Integer, _
  30.              ByRef lpNumberOfBytesWritten As Integer, _
  31.              ByRef lpOverlapped As OVERLAPPED) _
  32.              As IntPtr
  33.  
  34.     Declare Function ReadFile Lib "kernel32" _
  35.             (ByVal hFile As IntPtr, _
  36.              ByRef lpBuffer As Object, _
  37.              ByVal nNumberOfBytesToRead As Integer, _
  38.              ByRef lpNumberOfBytesRead As Integer, _
  39.              ByRef lpOverlapped As OVERLAPPED) _
  40.              As Integer
  41.  
  42.     Declare Function CloseHandle Lib "kernel32" _
  43.             (ByVal hObject As IntPtr) _
  44.              As Long
  45.  
  46.  
  47.     Const GENERIC_READ = &H80000000
  48.     Const GENERIC_WRITE = &H40000000
  49.     Const FILE_SHARE_READ = &H1
  50.     Const FILE_SHARE_WRITE = &H2
  51.     Const CREATE_ALWAYS = 2
  52.     Const CREATE_NEW = 1
  53.     Const OPEN_ALWAYS = 4
  54.     Const OPEN_EXISTING = 3
  55.     Const TRUNCATE_EXISTING = 5
  56.     Const FILE_ATTRIBUTE_ARCHIVE = &H20
  57.     Const FILE_ATTRIBUTE_HIDDEN = &H2
  58.     Const FILE_ATTRIBUTE_NORMAL = &H80
  59.     Const FILE_ATTRIBUTE_READONLY = &H1
  60.     Const FILE_ATTRIBUTE_SYSTEM = &H4
  61.     Const FILE_FLAG_DELETE_ON_CLOSE = &H4000000
  62.     Const FILE_FLAG_NO_BUFFERING = &H20000000
  63.     Const FILE_FLAG_OVERLAPPED = &H40000000
  64.     Const FILE_FLAG_POSIX_SEMANTICS = &H1000000
  65.     Const FILE_FLAG_RANDOM_ACCESS = &H10000000
  66.     Const FILE_FLAG_SEQUENTIAL_SCAN = &H8000000
  67.     Const FILE_FLAG_WRITE_THROUGH = &H80000000
  68.  
  69.     Private hMyMailslot As System.Object
  70.     Private hWriteMailslot As System.Object
  71.     Private secAttrib As SECURITY_ATTRIBUTES
  72.  
  73.     Structure SECURITY_ATTRIBUTES
  74.  
  75.         Dim nLength As Integer
  76.         Dim lpSecurityDescriptor As Integer
  77.         Dim bInheritHandle As Integer
  78.  
  79.     End Structure
  80.  
  81.     Structure OVERLAPPED
  82.  
  83.         Dim Internal As Integer
  84.         Dim InternalHigh As Integer
  85.         Dim offset As Integer
  86.         Dim OffsetHigh As Integer
  87.         Dim hEvent As Integer
  88.  
  89.     End Structure
  90.  
  91.  
  92.     Private Sub cmdCreateMailslot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCreateMailslot.Click
  93.  
  94.         hMyMailslot = CreateMailslot("\\.\mailslot\test", 0, -1, secAttrib)
  95.  
  96.     End Sub
  97.  
  98.     Private Sub cmdWriteToMailslot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdWriteToMailslot.Click
  99.  
  100.         Dim strMyMessage As String = "Hello World."
  101.         Dim intLength As Integer = Len(strMyMessage)
  102.         Dim intBytesWritten As Integer
  103.  
  104.  
  105.         hWriteMailslot = CreateFile("\\MDMAUB442049F\mailslot\test", GENERIC_WRITE, FILE_SHARE_READ, secAttrib, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
  106.  
  107.         WriteFile(hWriteMailslot, strMyMessage, intLength, intBytesWritten, Nothing)
  108.  
  109.         CloseHandle(hWriteMailslot)
  110.  
  111.  
  112.     End Sub
  113.  
  114.     Private Sub cmdReadMailslot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdReadMailslot.Click
  115.  
  116.         Dim intMsg As Integer       'size of the message being returned
  117.         Dim strMsg As String = ""   'message returned
  118.         Dim intMsgSize As Integer
  119.  
  120.         GetMailslotInfo(hMyMailslot, 0, intMsg, 0, 0)
  121.  
  122.         MsgBox("size of message: " & intMsg.ToString)
  123.  
  124.         ReadFile(hMyMailslot, strMsg, intMsg, intMsgSize, Nothing)
  125.  
  126.         MsgBox("Message: " & strMsg)
  127.  
  128.         CloseHandle(hMyMailslot)
  129.  
  130.  
  131.  
  132.     End Sub
  133.  
  134.     Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  135.  
  136.         With secAttrib
  137.             .nLength = Len(secAttrib)
  138.             .lpSecurityDescriptor = Nothing
  139.             .bInheritHandle = 1
  140.         End With
  141.  
  142.     End Sub
Apr 2 '07 #1
1 3275
vaelek
1
I am a C# developer and can't fully follow your code, but I believe I know the problem. When using a "server" with mailslots, you can open the writing slot in a number of ways:

1. \\Domain\ComputerName\any\path\for\slotname
2. \\ComputerName\any\path\for\slotname
3. \\Domain\*\any\path\to\slotname

On the computer that is receiving the messages, you open \\.\any\path\to\slotname and read from there. You cannot read from a remote slot. The 3rd option above will broadcast the message to all computers on Domain. Note that UDP broadcast packets are not typically forwarded by routers so be prepared to be limited to one subnet. I have a fully functional mailslot control done in C# along with a very descriptive article at http://www.codinghut.com/node/5
Aug 11 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

By using this site, you agree to our Privacy Policy and Terms of Use.