troubleshooting Question

Passing a .NET structure pointer to a API

Avatar of Dick202A
Dick202A asked on
Visual Basic.NET
2 Comments1 Solution299 ViewsLast Modified:
I am converting a namedpipe routine from VB6. After converting the VB6 TYPE to a .NEt STRUCTURE, how to I create a pointer to pass to the API.

VB6 Code
Type SECURITY_ATTRIBUTES
           nLength As Long
           lpSecurityDescriptor As Long
           bInheritHandle As Long
   End Type

Declare Function CreateNamedPipe Lib "kernel32" Alias _
      "CreateNamedPipeA" ( _
      ByVal lpName As String, _
      ByVal dwOpenMode As Long, _
      ByVal dwPipeMode As Long, _
      ByVal nMaxInstances As Long, _
      ByVal nOutBufferSize As Long, _
      ByVal nInBufferSize As Long, _
      ByVal nDefaultTimeOut As Long, _
      lpSecurityAttributes As Any) As Long
.
.
Private pSD As Long, sa As SECURITY_ATTRIBUTES

 pSD = GlobalAlloc(GPTR, SECURITY_DESCRIPTOR_MIN_LENGTH)
    rslt = InitializeSecurityDescriptor(pSD, SECURITY_DESCRIPTOR_REVISION)
    rslt = SetSecurityDescriptorDacl(pSD, -1, 0, 0)
    sa.nLength = Len(sa)
    sa.lpSecurityDescriptor = pSD
    sa.bInheritHandle = True
   
    'Create the Named Pipe
     dwOpenMode = PIPE_ACCESS_DUPLEX Or FILE_FLAG_WRITE_THROUGH
     dwPipeMode = PIPE_WAIT Or PIPE_TYPE_MESSAGE Or PIPE_READMODE_MESSAGE
     hPipe = CreateNamedPipe(iPipe_name, dwOpenMode, dwPipeMode, _
                             10, 10000, 2000, 10000, sa)

         [ above works in VB6 ]
------------------------------------------------------------------------------------------------------
.NET code
Structure SECURITY_ATTRIBUTES
        Public nLength As Integer
        Public lpSecurityDescriptor As Integer
        Public bInheritHandle As Integer
    End Structure

Declare Function CreateNamedPipe Lib "kernel32" Alias "CreateNamedPipeA" _
    (ByVal lpName As String, ByVal dwOpenMode As Integer, _
    ByVal dwPipeMode As Integer, ByVal nMaxInstances As Integer, _
    ByVal nOutBufferSize As Integer, ByVal nInBufferSize As Integer, _
    ByVal nDefaultTimeOut As Integer, ByVal lpSecurityAttributes As Integer _
    ) As Integer

.
.

Private pSD As Long, sa As SECURITY_ATTRIBUTES

        pSD = GlobalAlloc(GPTR, SECURITY_DESCRIPTOR_MIN_LENGTH)
        rslt = InitializeSecurityDescriptor(pSD, SECURITY_DESCRIPTOR_REVISION)
        rslt = SetSecurityDescriptorDacl(pSD, -1, 0, 0)
        sa.nLength = Len(sa)
        sa.lpSecurityDescriptor = pSD
        sa.bInheritHandle = True
.
.
       hPipe = CreateNamedPipe(pipeName, openMode, pipeMode, 10, 10000, 2000, 10000, sa)

          [ this does not work. I can't seem to define a pointer to the sa structure ]

Question:   How do I get a pointer to the sa structure to pass as the CreateNamedPipe parameter??
                 Since the ANY parameter type is not supported in .NET, I used thr Interger type. How should the
                security attribute parameter in the DECLARE of the CreateNamedPipe function be defined ?
                INTEGER, INTPTR oe something else?
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 2 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros