none
BC30109_'String' is a class type and cannot be used as an expression. RRS feed

  • Question

  • I haven't used cryptography much since a 2007 ISYS1005 Access VBA Lab. I am familiar with basic cryptography. I have two texts by Chapman and Bonde. We used some of each in lab. My old laptop went through a wet outlet and a basement flood. My two week cram class of an EHR was ruined. I understand concept of AES->HMAC->RSA etc., but want to get through both texts as refreshers and basically create web forms instead of succumbing to ASP, MVC, etc. Seems Chapman's text is a previous version of the Visual Basic IDE within Visual Studio 2019. I easily changed the While->Wend to While->End While, but seems the IDE is asking a function as a string with sNameBuffer = String(lNameLength, vbNullChar) . Any help would be appreciated.

    Private Sub EnumCSPs()

     Dim lResult As Long
     Dim lIndex As Long
     Dim sNameBuffer As String
     Dim lNameLength As Long
     Dim lProvType As Long

     lIndex = 0
     lblList.Text = ""

    lResult = CryptEnumProviders(lIndex, 0, 0, lProvType, vbNullString, lNameLength)

    sNameBuffer = String(lNameLength, vbNullChar)      //BC30109 'String' is a class type and cannot be used as an expression.

        While CBool(CryptEnumProviders(lIndex, 0, 0, lProvType, sNameBuffer,
                lNameLength))

                If (lIndex > 0) Then lblList.Text = lblList.Text + vbCrLf
                lblList.Text = lblList.Text + sNameBuffer

                lIndex = lIndex + 1
                lResult = CryptEnumProviders(lIndex, 0, 0, lProvType, vbNullString, lNameLength)

                sNameBuffer = String(lNameLength, vbNullChar)
         End While

    End Sub

    Thursday, December 12, 2019 10:09 PM

All replies

  • Hi

    Maybe this. It creates a string buffer of the length of the 1NameLength value (I used 12345678 for testing)

    Dim lNameLength As Long = 12345678
    Dim sNameBuffer As String = StrDup(CInt(lNameLength), vbNullChar)
    
    Dim len As Integer = sNameBuffer.Length
    ' provides a string buffer of 12345678
    
    


    Regards Les, Livingston, Scotland

    Thursday, December 12, 2019 10:26 PM
  • Perhaps if I include the entire text example. Generally the Cint recommendation fixed the errors, but I started getting warning flags and debugging errors while using the buttons. Seems: Wend to End While and the label to label.Text instead of label.caption. The string error though. I've never had an issue with using certain cryptography in Windows10 from text examples, but I like the project of eventually getting back to a file on the C:drive of a SHA2 or similar with something neat like changing a table Matrix to show a red X instead of any M if a different or base file is used.


    It's a form with three buttons and a label. I'm assuming the exercise is for showing which SHA, DES, MD5;2,etc., are on the computer.

    'VERSION 5.00
    'Begin VB.Form frmCSPs
       '...Public Class Form1 with 3 buttons and a label

    '//////////////////////////////////////////////////////////////////////////////////

    Option Explicit

    'Public Class...form1_load(...

    Private Declare Function CryptEnumProviderTypes Lib "advapi32.dll" Alias "CryptEnumProviderTypesA" ( _
        ByVal dwIndex As Long, _
        ByVal pdwReserved As Long, _
        ByVal dwFlags As Long, _
        pdwProvType As Long, _
        ByVal pszTypeName As String, _
        pcbTypeName As Long) As Long
    Private Declare Function CryptEnumProviders Lib "advapi32.dll" Alias "CryptEnumProvidersA" ( _
        ByVal dwIndex As Long, _
        ByVal pdwReserved As Long, _
        ByVal dwFlags As Long, _
        pdwProvType As Long, _
        ByVal pszProvName As String, _
        pcbProvName As Long) As Long

        Private Sub cmdlistCSPs_Click(sender As Object, e As EventArgs) Handles cmdlistCSPs.Click
            EnumCSPs()
        End Sub
        Private Sub cmdTypes_Click(sender As Object, e As EventArgs) Handles cmdTypes.Click
            EnumTypes()
        End Sub
        Private Sub cmdExit_Click(sender As Object, e As EventArgs) Handles cmdExit.Click
            Me.Close()
        End Sub

    Private Sub EnumCSPs()
    '*************************************************************
    '* Written By: Davis Chapman
    '* Date:       September 22, 1999
    '*
    '* Syntax:     EnumCSPs
    '*
    '* Parameters:  None
    '*
    '* Purpose: This subroutine loops through all of the CSPs
    '*          installed on the system, listing them for the user.
    '*************************************************************
        Dim lResult As Long
        Dim lIndex As Long
        Dim sNameBuffer As String
        Dim lNameLength As Long
        Dim lProvType As Long
       
        '--- Initialize the necessary variables
        lIndex = 0
        lblList.Caption = ""
       
        '--- Determine the size of the buffer needed
        lResult = CryptEnumProviders(lIndex, 0, 0, lProvType, vbNullString, lNameLength)
       
        '--- Prepare a string buffer for the CryptEncrypt function
        sNameBuffer = String(lNameLength, vbNullChar)
       
        '--- Get the provider name, looping until all providers have been listed
        While CBool(CryptEnumProviders(lIndex, 0, 0, lProvType, sNameBuffer, _
                lNameLength))
           
            '--- Add the current CSP to the display list
            If (lIndex > 0) Then lblList.Caption = lblList.Caption + vbCrLf
            lblList.Caption = lblList.Caption + sNameBuffer
           
            '--- Increment the index counter
            lIndex = lIndex + 1
           
            '--- Determine the size of the buffer needed
            lResult = CryptEnumProviders(lIndex, 0, 0, lProvType, vbNullString, lNameLength)
           
            '--- Prepare a string buffer for the CryptEncrypt function
            sNameBuffer = String(lNameLength, vbNullChar)
        Wend
    End Sub
    Private Sub EnumTypes()
    '*************************************************************
    '* Written By: Davis Chapman
    '* Date:       September 22, 1999
    '*
    '* Syntax:     EnumTypes
    '*
    '* Parameters:  None
    '*
    '* Purpose: This subroutine loops through all of the CSP types
    '*          installed on the system, listing them for the user.
    '*************************************************************
        Dim lResult As Long
        Dim lIndex As Long
        Dim sNameBuffer As String
        Dim lNameLength As Long
        Dim lProvType As Long
       
        '--- Initialize the necessary variables
        lIndex = 0
        lblList.Caption = ""
       
        '--- Determine the size of the buffer needed
        lResult = CryptEnumProviderTypes(lIndex, 0, 0, lProvType, vbNullString, lNameLength)
       
        '--- Prepare a string buffer for the CryptEncrypt function
        sNameBuffer = String(lNameLength, vbNullChar)
       
        '--- Get the type name, looping until all types have been listed
        While CBool(CryptEnumProviderTypes(lIndex, 0, 0, lProvType, sNameBuffer, _
                lNameLength))
           
            '--- Add the current CSP to the display list
            If (lIndex > 0) Then lblList.Caption = lblList.Caption + vbCrLf
            lblList.Caption = lblList.Caption + sNameBuffer
           
            '--- Increment the index counter
            lIndex = lIndex + 1
           
            '--- Determine the size of the buffer needed
            lResult = CryptEnumProviderTypes(lIndex, 0, 0, lProvType, vbNullString, lNameLength)
           
            '--- Prepare a string buffer for the CryptEncrypt function
            sNameBuffer = String(lNameLength, vbNullChar)
        Wend
    End Sub

    'End Class

    Friday, December 13, 2019 12:20 AM
  •  but seems the IDE is asking a function as a string with sNameBuffer = String(lNameLength, vbNullChar) . Any help

    What do you mean by: "asking a function as a string"?

    "String" is a .NET class:

    String Class
    https://docs.microsoft.com/en-us/dotnet/api/system.string?view=netframework-4.8

    The expression: String(lNameLength, vbNullChar)
    has the syntax of a Function call.

    It is not a correct syntax for constructing a String, if that is what was
    intended. If the program has a Function named "String" defined then that 
    name should be changed, or qualified so that it can be distinguished from 
    System.String.

    <typename>' is a class type and cannot be used as an expression
    https://docs.microsoft.com/en-us/dotnet/visual-basic/misc/bc30109

    - Wayne

    Friday, December 13, 2019 12:36 AM
  • Perhaps if I include the entire text example. Generally the Cint recommendation fixed the errors, but I started getting warning flags and debugging errors while using the buttons. Seems: Wend to End While and the label to label.Text instead of label.caption. The string error though. I've never had an issue with using certain cryptography in Windows10 from text examples, but I like the project of eventually getting back to a file on the C:drive of a SHA2 or similar with something neat like changing a table Matrix to show a red X instead of any M if a different or base file is used.


    It's a form with three buttons and a label. I'm assuming the exercise is for showing which SHA, DES, MD5;2,etc., are on the computer.

    'VERSION 5.00
    'Begin VB.Form frmCSPs
       '...Public Class Form1 with 3 buttons and a label

    '//////////////////////////////////////////////////////////////////////////////////

    Option Explicit

    'Public Class...form1_load(...

    Private Declare Function CryptEnumProviderTypes Lib "advapi32.dll" Alias "CryptEnumProviderTypesA" ( _
        ByVal dwIndex As Long, _
        ByVal pdwReserved As Long, _
        ByVal dwFlags As Long, _
        pdwProvType As Long, _
        ByVal pszTypeName As String, _
        pcbTypeName As Long) As Long
    Private Declare Function CryptEnumProviders Lib "advapi32.dll" Alias "CryptEnumProvidersA" ( _
        ByVal dwIndex As Long, _
        ByVal pdwReserved As Long, _
        ByVal dwFlags As Long, _
        pdwProvType As Long, _
        ByVal pszProvName As String, _
        pcbProvName As Long) As Long

        Private Sub cmdlistCSPs_Click(sender As Object, e As EventArgs) Handles cmdlistCSPs.Click
            EnumCSPs()
        End Sub
        Private Sub cmdTypes_Click(sender As Object, e As EventArgs) Handles cmdTypes.Click
            EnumTypes()
        End Sub
        Private Sub cmdExit_Click(sender As Object, e As EventArgs) Handles cmdExit.Click
            Me.Close()
        End Sub

    Private Sub EnumCSPs()
    '*************************************************************
    '* Written By: Davis Chapman
    '* Date:       September 22, 1999
    '*
    '* Syntax:     EnumCSPs
    '*
    '* Parameters:  None
    '*
    '* Purpose: This subroutine loops through all of the CSPs
    '*          installed on the system, listing them for the user.
    '*************************************************************
        Dim lResult As Long
        Dim lIndex As Long
        Dim sNameBuffer As String
        Dim lNameLength As Long
        Dim lProvType As Long
       
        '--- Initialize the necessary variables
        lIndex = 0
        lblList.Caption = ""
       
        '--- Determine the size of the buffer needed
        lResult = CryptEnumProviders(lIndex, 0, 0, lProvType, vbNullString, lNameLength)
       
        '--- Prepare a string buffer for the CryptEncrypt function
        sNameBuffer = String(lNameLength, vbNullChar)
       
        '--- Get the provider name, looping until all providers have been listed
        While CBool(CryptEnumProviders(lIndex, 0, 0, lProvType, sNameBuffer, _
                lNameLength))
           
            '--- Add the current CSP to the display list
            If (lIndex > 0) Then lblList.Caption = lblList.Caption + vbCrLf
            lblList.Caption = lblList.Caption + sNameBuffer
           
            '--- Increment the index counter
            lIndex = lIndex + 1
           
            '--- Determine the size of the buffer needed
            lResult = CryptEnumProviders(lIndex, 0, 0, lProvType, vbNullString, lNameLength)
           
            '--- Prepare a string buffer for the CryptEncrypt function
            sNameBuffer = String(lNameLength, vbNullChar)
        Wend
    End Sub
    Private Sub EnumTypes()
    '*************************************************************
    '* Written By: Davis Chapman
    '* Date:       September 22, 1999
    '*
    '* Syntax:     EnumTypes
    '*
    '* Parameters:  None
    '*
    '* Purpose: This subroutine loops through all of the CSP types
    '*          installed on the system, listing them for the user.
    '*************************************************************
        Dim lResult As Long
        Dim lIndex As Long
        Dim sNameBuffer As String
        Dim lNameLength As Long
        Dim lProvType As Long
       
        '--- Initialize the necessary variables
        lIndex = 0
        lblList.Caption = ""
       
        '--- Determine the size of the buffer needed
        lResult = CryptEnumProviderTypes(lIndex, 0, 0, lProvType, vbNullString, lNameLength)
       
        '--- Prepare a string buffer for the CryptEncrypt function
        sNameBuffer = String(lNameLength, vbNullChar)
       
        '--- Get the type name, looping until all types have been listed
        While CBool(CryptEnumProviderTypes(lIndex, 0, 0, lProvType, sNameBuffer, _
                lNameLength))
           
            '--- Add the current CSP to the display list
            If (lIndex > 0) Then lblList.Caption = lblList.Caption + vbCrLf
            lblList.Caption = lblList.Caption + sNameBuffer
           
            '--- Increment the index counter
            lIndex = lIndex + 1
           
            '--- Determine the size of the buffer needed
            lResult = CryptEnumProviderTypes(lIndex, 0, 0, lProvType, vbNullString, lNameLength)
           
            '--- Prepare a string buffer for the CryptEncrypt function
            sNameBuffer = String(lNameLength, vbNullChar)
        Wend
    End Sub

    'End Class

    This is not VB.NET code at all. If you want to use VB.NET you best option is to start with a clean slate. If this is for VB Classic then go to the following link for VB Classic

    http://www.vbforums.com/forumdisplay.php?1-Visual-Basic-6-and-Earlier

    When coding in VB.NET its best to forget old ways of coding and learn new ways to code.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, December 13, 2019 12:39 AM
  • Hi

    To make the code more readable, please use the code block tool.


    Regards Les, Livingston, Scotland

    Friday, December 13, 2019 12:41 AM
  •  sNameBuffer = String(lNameLength, vbNullChar) . 

    Also see this thread:

    equivalent of vb 6 string
    https://social.msdn.microsoft.com/Forums/en-US/45721253-d8f3-42fc-933c-5343531f9fcc/equivalent-of-vb-6-string

    - Wayne

    Friday, December 13, 2019 12:48 AM
  • The Chapman text wasn't clear beyond Visual Basic applications. Messing around it seems Windows10 has simple3des, SHA1 and RSA. Somewhere around New Filestream is a little abstract, but I'm not even at form headers yet. Windows Forms (Visual Basic, .NET) is what I usually select in Visual Studio. Basically learning Visual Basic from a VBA transition. We had our Access files basically like MVC, but VBA.

    It encrypts and gives a message box with a signature(1form,1button), but close to the general project:

    Imports System.Security.Cryptography
    Imports System.IO
    Public Class Dashboard
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim plainText As String = InputBox("Text to Encrypt:")
            Dim password As String = InputBox("Enter a Password:")
            Dim wrapper As New Simple3Des(password)
            Dim cipherText As String = wrapper.EncryptData(plainText)
            'MsgBox("The cipher text is: " & cipherText)
            My.Computer.FileSystem.WriteAllText(
            My.Computer.FileSystem.SpecialDirectories.MyDocuments &
            "\cipherText.txt", cipherText, False)
            Dim password1 As String = "Password"
            'Create Password Key
            Dim SaltValueBytes As Byte() =
                System.Text.Encoding.ASCII.GetBytes("This is my salt")
            Dim passwordKey As Rfc2898DeriveBytes =
                New Rfc2898DeriveBytes(password1, SaltValueBytes)
            Dim alg As RijndaelManaged = New RijndaelManaged
            alg.Key = passwordKey.GetBytes(alg.KeySize / 8)
            alg.IV = passwordKey.GetBytes(alg.BlockSize / 8)
            'Read the unencrypted file into filedata
            Dim inFile As FileStream = New FileStream(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\cipherText.txt", FileMode.Open, FileAccess.Read)
            Dim fileData(inFile.Length) As Byte
            inFile.Read(fileData, 0, CType(inFile.Length, Integer))
            'Create the ICryptoTransform and CryptoStream object
            Dim encryptor As ICryptoTransform = alg.CreateEncryptor
            inFile.Close()
            Dim outFile As FileStream = New FileStream(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\cipherText.txt", FileMode.OpenOrCreate, FileAccess.Write)
            Dim encryptStream As CryptoStream = New CryptoStream(outFile, encryptor, CryptoStreamMode.Write)
            'Write contents to the CryptoStream
            encryptStream.Write(fileData, 0, fileData.Length)
            'Close the file handles
            encryptStream.Close()
            outFile.Close()
            Dim signer As RSACryptoServiceProvider = New RSACryptoServiceProvider
            'step 2
            Dim file As FileStream = New FileStream(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\cipherText.txt", FileMode.Open, FileAccess.Read)
            Dim reader As BinaryReader = New BinaryReader(file)
            Dim data As Byte() = reader.ReadBytes(CType(file.Length, Integer))
            'step 3// Calling SignData method and store signature
            Dim signature As Byte() = signer.SignData(data, New SHA1CryptoServiceProvider)
            'step 4// Export the public key
            Dim publicKey As String = signer.ToXmlString(False)
            MsgBox("Signature: " + Convert.ToBase64String(signature))
            reader.Close()
            file.Close()
        End Sub


    Friday, December 13, 2019 6:18 AM

  • What do you mean by: "asking a function as a string"?

    - Wayne

    It doesn't seem like String(…, ...) should be there, but a value passed from somewhere else. Like Dim … As String = textbox1(…, ...).
    Friday, December 13, 2019 6:42 AM
  • I was able to refresh on some of the topics. Foolish, but I was looking at the example without applying values and I overlooked the foundations like making values equal to not get overflows. Simply, I didn't apply folders to pull the CSPs. I apologize for the lame question, but hence refreshing from one Lab in 2007. 
    Saturday, December 14, 2019 4:51 AM