Dick202A
asked on
Passing a .NET structure pointer to a API
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_LE NGTH)
rslt = InitializeSecurityDescript or(pSD, SECURITY_DESCRIPTOR_REVISI ON)
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_LE NGTH)
rslt = InitializeSecurityDescript or(pSD, SECURITY_DESCRIPTOR_REVISI ON)
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?
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_LE
rslt = InitializeSecurityDescript
rslt = SetSecurityDescriptorDacl(
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
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_LE
rslt = InitializeSecurityDescript
rslt = SetSecurityDescriptorDacl(
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
((<MarshalAs(UnmanagedType
ByVal dwOpenMode As Integer, _
ByVal dwPipeMode As Integer, ByVal nMaxInstances As Integer, _
ByVal nOutBufferSize As Integer, ByVal nInBufferSize As Integer, _
ByVal nDefaultTimeOut As Integer, ByRef lpSecurityAttributes AsSECURITY_ATTRIBUTES _
) As Integer