473,397 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Mailslot possible with proper PInvoke?

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

Nov 22 '05 #1
4 1800
Ted,
I have tried multiple versions of PInvoke signatures,etc.
with the same result.
Please post your declarations so we can have a look.

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


I don't see why not.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 22 '05 #2
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
-----------------------------------------------------
-----Original Message-----
Ted,
I have tried multiple versions of PInvoke signatures,etc. with the same result.


Please post your declarations so we can have a look.

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


I don't see why not.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
.

Nov 22 '05 #3
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 22 '05 #4
Ted
Mattias,

I found my bug, thank you VERY MUCH for your help.
Ted
-----Original Message-----
Mattias,

I have tried a number of ways including as you described:
With the exception of lpNumberOfBytesWritten, all parameters you
currently have declared ByRef should be ByVal.


The result is the same. The handle used with WriteFile()
returns an invalid handle error.

Have you ever used mailslots with .NET successfully? Do
you have a small example of code using mailslots

with .NET?

Thanks,
Ted
-----Original Message-----
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
.

.

Nov 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Ted | last post by:
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...
0
by: aherzallah | last post by:
Hi every one I have a question, how can I create a MailSlot in .NET using C#, just like the windows API CreateMailslot (in case still not clear please see the link...
1
by: aherzallah | last post by:
Hi every one can anyone help me on how can I create/write/read from a MailSlot in .NET using C#, just like the windows API CreateMailslot, (in case still not clear please see the link...
3
by: chip | last post by:
I know it can be done in a dll, but is it possible to call a c++ / c function directly from c-sharp. -chip
3
by: Brett Robichaud | last post by:
I have created a simple background thread to make one pinvoke call into a DLL I've created. My Winforms app spawns the thread in the form load event then go about it's business. The problem is...
1
by: vertigo | last post by:
Hello Can i use mailslot mechnism in C# ? I could find API for mailslot only for C/C++ for example: HANDLE CreateMailslot( LPCTSTR lpName, DWORD nMaxMessageSize, DWORD lReadTimeout,...
1
by: vertigo | last post by:
Hello I've tried to create mailslot server, and send there some packets. On both sides every operation is successful, but on server side message do not appear. Is there any client under...
0
by: vertigo | last post by:
Hello I receive some mailslot packets. Can i check from whom i have received them ? Thanx Michal
4
by: Rainer Queck | last post by:
Hi NG, I am working on a project in VS2005 that has to comunicate with an older application, using MailSlots. As far as I know, I have to use pInvoke to achieve this. It would be greate, to...
14
by: Mohamed Mansour | last post by:
Hey there, this will be somewhat a long post, but any response is appreciated! I have done many PInvoke in the past from C++ to C#, but I did PInvoke within C# not C++/CLI. Can someone explain...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.