Click here to Skip to main content
13,199,318 members (62,191 online)
Rate this:
Please Sign up or sign in to vote.
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.

Relevant Code:

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)
End Sub
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))
            End If
        Loop Until BytesRead = 0
End Sub

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()
    MsgBox("hello world")
End Sub

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()
End Sub

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
End Function

<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
End Function

<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
End Function

<StructLayout(LayoutKind.Sequential)> _
    Public nLength As System.UInt32
    Public lpSecurityDescriptor As IntPtr
    Public bInheritHandle As Boolean
End Structure

<StructLayoutAttribute(LayoutKind.Sequential)> _
    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
End Structure

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
        End If

'this is the code i added
        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.CreateNamedPipe(Me.FullName, _
                                        mode, _
                                        NativeMethods.PIPE_WAIT Or NativeMethods.PIPE_TYPE_BYTE, _
                                        NativeMethods.PIPE_UNLIMITED_INSTANCES, _
                                        bufferSize, bufferSize, _
                                        NativeMethods.NMPWAIT_WAIT_FOREVER, saPtr) _
                                        , True)
'end of the code i added - before the previous call had 0 in place of saPtr
        If Handle.IsInvalid Then ThrowUnknownException("CreateNamedPipe")
    End Sub

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.
Posted 22-Nov-10 5:00am
Updated 22-Nov-10 5:24am

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.171020.1 | Last Updated 22 Nov 2010
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100