Click here to Skip to main content
15,881,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,
VB6 to VB.Net convert the project below error display
Value of type 'Project1.Module1.SECURITY_ATTRIBUTES' cannot be converted to 'String'.

"sa" error how to clear error


Coding
VB
Public Sub ExecAndCapture(ByVal sCommandLine As String, ByRef txt As String, Optional ByVal sStartInFolder As String = vbNullString)

        Const BUFSIZE As Integer = 1024 * 10
        Dim hPipeRead As Integer
        Dim hPipeWrite As Integer
        Dim sa As SECURITY_ATTRIBUTES
        Dim si As STARTUPINFO
        Dim pi As PROCESS_INFORMATION
        Dim baOutput(BUFSIZE) As Byte
        Dim sOutput As String
        Dim lBytesRead As Integer

        With sa
            .nLength = Len(sa)
            .bInheritHandle = 1 ' get inheritable pipe handles
        End With 'SA

        'UPGRADE_WARNING: Couldn't resolve default property of object sa. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
        If CreatePipe(hPipeRead, hPipeWrite, sa, 0) = 0 Then  
             Exit Sub
        End If

        With si
            .cb = Len(si)
            .dwFlags = STARTF_USESHOWWINDOW Or STARTF_USESTDHANDLES
            .wShowWindow = SW_HIDE ' hide the window
            .hStdOutput = hPipeWrite
            .hStdError = hPipeWrite
        End With 'SI
        sCommandLine = sCommandLine & " " & txt
        Dim mystr As String

        If CreateProcess(vbNullString, sCommandLine, 0, 0, 1, 0, 0, sStartInFolder, si, pi) Then
            Call CloseHandle(hPipeWrite)
            Call CloseHandle(pi.hThread)
            hPipeWrite = 0
            Sleep((1000))
            Do
                System.Windows.Forms.Application.DoEvents()
                If ReadFile(hPipeRead, baOutput(0), BUFSIZE, lBytesRead, 0) = 0 Then
                    Exit Do
                End If
                'UPGRADE_ISSUE: Constant vbUnicode was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="55B59875-9A95-4B71-9D6A-7C294BF7139D"'
                sOutput = Left(StrConv(System.Text.UnicodeEncoding.Unicode.GetString(baOutput), unicode), lBytesRead)
                mystr = sOutput
            Loop

            Call CloseHandle(pi.hProcess)
        End If
        ' MsgBox(mystr)
        ' To make sure...
        Call CloseHandle(hPipeRead)
        Call CloseHandle(hPipeWrite)
        'line coding missing

        FileOpen(4, "d:\temp\fix1.txt", OpenMode.Output)
        PrintLine(4, mystr)
        FileClose(4)
    End Sub
Posted
Updated 8-Apr-14 2:29am
v3

1 solution

I am revising this answer because there is a method that does not require bringing in old VB6 libraies.

Your time may well be better spent learning .NET System.IO.Pipes. System.IO.Pipes Namespace[^]

There is an example here: NamedPipeClientStream Class[^]

If you insist on attempting to force the method you have now, you may experiment with the SECURITY_ATTRIBUTES member types. You are likely getting hung on the lpSecurityDescriptor attribute.

I have seen C++ code that sets all the attributes to a long type. I've also read a suggestion to use the variant type as well. (The only time I use variant is to find which way VB.NET will cast a type to determine which type I could use and change it to that type if it works.)

A C++ version sets the sa attributes thus: (Converted to VB)
VB
With sa
    .nLength = Len(sa)
    .bInheritHandle = True ' get inheritable pipe handles
    .lpSecurityDescriptor = vbNull  ' Use default ACL
End With 'SA


Good luck.

You need to create the structures yourself.

Try adding a module to your project and place the following code into it.
VB.NET
Imports System.Runtime.InteropServices
Imports System.Security.Permissions
Imports System.Reflection

Module Module1
    <StructLayout(LayoutKind.Sequential)> _
    Public Structure PROCESS_INFORMATION
        Public hProcess As IntPtr
        Public hThread As IntPtr
        Public dwProcessID As UInteger
        Public dwThreadID As UInteger
    End Structure 'PROCESS_INFORMATION

    <StructLayout(LayoutKind.Sequential)> _
    Public Structure SECURITY_ATTRIBUTES
        Public nLength As Integer
        Public lpSecurityDescriptor As IntPtr
        Public bInheritHandle As Boolean
    End Structure 'SECURITY_ATTRIBUTES

    <StructLayout(LayoutKind.Sequential)> _
    Public Structure STARTUPINFO
        Public cb As UInteger
        Public lpReserved As String
        Public lpDesktop As String
        Public lpTitle As String
        Public dwX As UInteger
        Public dwY As UInteger
        Public dwXSize As UInteger
        Public dwYSize As UInteger
        Public dwXCountChars As UInteger
        Public dwYCountChars As UInteger
        Public dwFillAttribute As UInteger
        Public dwFlags As UInteger
        Public wShowWindow As Short
        Public cbReserved2 As Short
        Public lpReserved2 As IntPtr
        Public hStdInput As IntPtr
        Public hStdOutput As IntPtr
        Public hStdError As IntPtr
    End Structure 'STARTINFO

    <DllImport("kernel32.dll")> _
    Function CreateProcess( _
    ByVal lpApplicationName As String, _
    ByVal lpCommandLine As String, _
    ByVal lpProcessAttributes As IntPtr, _
    ByVal lpThreadAttributes As IntPtr, _
    ByVal bInheritHandles As Boolean, _
    ByVal dwCreationFlags As UInteger, _
    ByVal lpEnvironment As IntPtr, _
    ByVal lpCurrentDirectory As String, _
    ByRef lpStartupInfo As STARTUPINFO, _
    ByRef lpProcessInformation As PROCESS_INFORMATION) As Boolean
    End Function
End Module
I assume you have all the API function calls generted for your code above.

Good luck.
 
Share this answer
 
v2
Comments
Member 10420597 9-Apr-14 0:00am    
I already add the above line but not working .....
Value of type 'Project1.Module1.SECURITY_ATTRIBUTES' cannot be converted to 'String'.

"sa" error how to clear error
S Houghtelin 9-Apr-14 8:40am    
Please see my revised answer.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900