473,387 Members | 1,553 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,387 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

Jul 19 '05 #1
4 3673
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.
Jul 19 '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.
.

Jul 19 '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.
Jul 19 '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.
.

.

Jul 19 '05 #5

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

Similar topics

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: 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...
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.