Mailslot possible with proper PInvoke?

  • Thread starter Thread starter Ted
  • Start date Start date
T

Ted

Is it possible to use mailslots in .NET using PInvoke?

I have a VC++ 6.0 based app that creates and listens to a
mailslot. I have a second VC++ 6.0 based app that opens
the mailslot and writes to it successfully. So this stuff
works.

Additionally, I have a VB.NET app that opens the mailslot
successfully using CreateFile() (with PInvoke). What looks
like a proper handle is returned and there is no error.
(Marshal.GetLastWin32Error() returns 0).

However, when I try to use this handle (VB.NET app) with
WriteFile() (using PInvoke) the write fails with error
code 0x06, which is "The handle is invalid".

I have tried multiple versions of PInvoke signatures,etc.
with the same result.

IS IT AT ALL POSSIBLE TO USE MAILSLOTS WITH .NET?

Thank you,
Ted
 
Mattias,

I have also tried to pass the handle to WriteFile using a
ByVal parameter, but had the same result. (bad handle)

Thanks,
Ted
I have included my VB.NET declarations:

-----------------------------------------------------------
<DllImport("kernel32.dll", _
CharSet:=CharSet.Ansi, _
EntryPoint:="CreateFileA", _
ExactSpelling:=True, _
SetLastError:=True)> _
Public Shared Function CreateFile(
ByVal lpFileName As String, _
ByVal dwDesiredAccess As Int32, _
ByVal dwSharedMode As Int32, _
ByRef lpSecurityAttributes As IntPtr, _
ByVal dwCreationDisposition As Int32, _
ByVal dwFlagsAndAttributes As Int32, _
ByRef hTemplateFile As IntPtr) As IntPtr
End Function

<DllImport("kernel32.dll", _
EntryPoint:="WriteFile", _
ExactSpelling:=True, _
SetLastError:=True)> _
Public Shared Function WriteFile(
ByRef hFile As IntPtr, _
ByRef lpBuffer As IntPtr, _
ByVal nNumberOfBytesToWrite As Int32, _
ByRef lpNumberOfBytesWritten As Int32, _
ByRef lpOverLapped As IntPtr) As Integer
End Function
-----------------------------------------------------
 
Ted,

With the exception of lpNumberOfBytesWritten, all parameters you
currently have declared ByRef should be ByVal.

Also, FWIW, explicitly calling the ANSI version of CreateFile hurts
performance on NT based Windows versions. It's generally better to use
CharSet.Auto.



Mattias
 
Back
Top