I am using named pipes to communicate from one process to another. All projects that I am using are running with "Target CPU -> x86"
I have other libraries that i will need to use which need to be x86. When I run the projects as "Target CPU -> Any CPU" the issue seems to go away, but appears down the road in a different exception. I believe this exception is getting closer to the root issue.
Dim WithEvents P As New NamedPipes.PipeListener("mPipe", 1024)
Private Sub P_ClientConnected(ByVal sender As Object, ByVal e As NamedPipes.ClientConnectedEventArgs) Handles P.ClientConnected
Dim Handler As New iHandleClient(AddressOf HandleClient)
Handler.BeginInvoke(e.ClientStream, Nothing, Nothing)
Private Delegate Sub iHandleClient(ByVal ClientStream As IO.Stream)
Private Sub HandleClient(ByVal ClientStream As IO.Stream)
Dim myID As Integer = SID
SID += 1
Dim BytesRead As Integer
Dim Buffer(1023) As Byte
BytesRead = ClientStream.Read(Buffer, 0, Buffer.Length)
If BytesRead <> 0 Then
Dim incommingMessage As String = System.Text.Encoding.Unicode.GetString(Buffer, 0, BytesRead)
ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf RunAsync))
Loop Until BytesRead = 0
Now, when I send a command via some "client", it receives the message, as far as i can tell, without problem.
If i run the stub for RunAsync() as follows:
Public Sub RunAsync()
it seems to run without any problems at all, no first or second chance exceptions. on the other hand, if i run this:
Public Sub RunAsync()
it returns an AccessViolationException with the following message:
"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
imgExporter in this case is a class library that I created with code that essentially just writes images to file based on parameters. the code, however, is never being called. as far as i can tell, the error occurs when it tries to load the imgexporter.dll
also, in case you were wondering, the message from the pipe comes through perfectly. in the end i will use the message to determine what code to call, but at the moment i am just trying to call ANY code.
any help is much appreciated.
If, by chance, any of this is my fault based on the additions i made to the named pipes wrapper library, i will include some of that code as well.
the original wrapper that i used can be found here:
Asynchronous Named Pipes (Overlapped IO) with VB.NET
the additions I made were in the NativeMethods class:
<DllImport("advapi32.DLL", EntryPoint:="InitializeSecurityDescriptor", SetLastError:=True, CharSet:=CharSet.Unicode, CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function InitializeSecurityDescriptor(ByVal SecurityDescriptor As IntPtr, ByVal dwRevision As Integer) As Boolean
<DllImport("advapi32.DLL", EntryPoint:="SetSecurityDescriptorDacl", SetLastError:=True, CharSet:=CharSet.Unicode, CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function SetSecurityDescriptorDacl(ByVal SecurityDescriptor As IntPtr, ByVal bDaclPresent As Boolean, ByVal pDacl As IntPtr, ByVal bDaclDefaulted As Boolean) As Boolean
<DllImport("advapi32.dll", CharSet:=CharSet.Unicode, SetLastError:=True)> _
Public Shared Function ConvertStringSecurityDescriptorToSecurityDescriptor(ByVal StringSecurityDescriptor As String, ByVal StringSDRevision As UInt32, ByRef SecurityDescriptor As IntPtr, ByVal SecurityDescriptorSize As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
Public Structure SECURITY_ATTRIBUTES
Public nLength As System.UInt32
Public lpSecurityDescriptor As IntPtr
Public bInheritHandle As Boolean
Public Structure SECURITY_DESCRIPTOR
Public revision As Byte
Public size As Byte
Public control As Short
Public owner As IntPtr
Public group As IntPtr
Public sacl As IntPtr
Public dacl As IntPtr
and then some adjustments in the ServerPipeInstance constructor:
Public Sub New(ByVal name As String, ByVal access As IO.FileAccess, ByVal bufferSize As Integer, ByVal isAsync As Boolean)
If name.Contains("\") Or name = "" Or name.Length > 247 Then Throw New ArgumentException("Pipe name must be between 1 and 247 characters long and it cannot contain backslash (\).")
mAccess = access
mBufferSize = bufferSize
mIsAsync = isAsync
mName = name
Dim mode As Integer = access
If isAsync Then
mode = mode Or NativeMethods.FILE_FLAG_OVERLAPPED
Dim sd As New NativeMethods.SECURITY_DESCRIPTOR
Dim ptr As IntPtr = Marshal.AllocCoTaskMem(Marshal.SizeOf(sd))
Marshal.StructureToPtr(sd, ptr, False)
Dim result As Boolean = NativeMethods.InitializeSecurityDescriptor(ptr, 1)
If result = 0 Then ThrowUnknownException("InitializeSecurityDescriptor")
result = NativeMethods.SetSecurityDescriptorDacl(ptr, True, IntPtr.Zero, False)
If result = 0 Then ThrowUnknownException("SetSecurityDescriptionDacl")
Dim sa As New NativeMethods.SECURITY_ATTRIBUTES
sa.lpSecurityDescriptor = ptr
sa.bInheritHandle = False
sa.nLength = Marshal.SizeOf(sa)
Dim saPtr As IntPtr = Marshal.AllocCoTaskMem(Marshal.SizeOf(sa))
Marshal.StructureToPtr(sa, saPtr, False)
mHandle = New Microsoft.Win32.SafeHandles.SafeFileHandle( _
NativeMethods.PIPE_WAIT Or NativeMethods.PIPE_TYPE_BYTE, _
bufferSize, bufferSize, _
NativeMethods.NMPWAIT_WAIT_FOREVER, saPtr) _
If Handle.IsInvalid Then ThrowUnknownException("CreateNamedPipe")
The end product I am looking for is a windows service (system) receiving commands via named pipes from an alternate windows service (network service) and running code from the imgExporter library.
ANY help/suggestions would be much appreciated...
i have already spent a lot of time on this and have searched for everything with no luck.
i NEED to get this working though. if there is any more information that you guys need, please let me know.