Results 1 to 3 of 3

Thread: Argument not specified for parameter x of Private Function x(x As String) As String

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2018
    Posts
    51

    Question Argument not specified for parameter x of Private Function x(x As String) As String

    I'm getting this error in my VB.Net application:

    Argument not specified for parameter 'strFileName' of 'Private Function GetMD5String(strFilename As String) As String
    when trying to use a function called GetMD5String() in a Timer1_Tick sub.


    I'm using this code:


    Code:
    Private Function GetMD5String(ByVal strFilename As String) As String
    
            Dim cMD5 = Security.Cryptography.MD5.Create
            Dim bytHash As Byte()
            Dim sb As New System.Text.StringBuilder
            Dim scanbox As New TextBox
            scanbox.Text = My.Computer.FileSystem.ReadAllText("viruslist.txt").ToString
            Me.OpenFileDialog1.FileName = strFilename
    
            Using cStream As New IO.FileStream(strFilename, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)
    
                bytHash = cMD5.ComputeHash(cStream)
            End Using
    
            For Each c In bytHash
                sb.Append(c.ToString("X2"))
            Next
    
            If scanbox.Text.Contains(sb.ToString) Then
                TextBox1.Text = (strFilename)
                Me.OpenFileDialog1.FileName = strFilename
                Detect.ShowDialog()
                WriteToLog("Malicious exploit detected.")
                Quarantinevb.ListBox1.Items.Add(strFilename)
            End If
    
            Return sb.ToString
    
    
    
        End Function

    and this code for Timer1_Tick


    Code:
     Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            RefreshList()
            Timer1.Enabled = False 'Stops the timer
            Timer1.Enabled = True
            GetMD5String()
        End Sub

    Why is GetMD5String() throwing me this error?

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: Argument not specified for parameter x of Private Function x(x As String) As Stri

    Take a look at the function definition:
    Private Function GetMD5String(ByVal strFilename As String) As String
    The item(s) inside the brackets are values that you must pass to it when you call it, so in this case you must pass a String (which represents strFilename), so you might call it like this:
    Code:
    GetMD5String("C:\Folder\File.tmp")
    However, GetMD5String is a function, which means it returns a value (in this case a String, as that it what is listed after the brackets in the function definition), so you probably want something like this:
    Code:
    Dim MD5 as String = GetMD5String("C:\Folder\File.tmp")
    MessageBox.Show(MD5)

    edit: Also note that there is lots of code inside the function that doesn't really belong in there, as everything to do with scanbox or OpenFileDialog1 is irrelevant to getting an MD5 string. All of that code should really be outside of that function, possibly in your Timer sub instead, or maybe in another sub (with an appropriate name) that you call from the Timer sub.
    Last edited by si_the_geek; Jun 20th, 2018 at 07:13 AM.

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: Argument not specified for parameter x of Private Function x(x As String) As Stri

    This is how I recommend you split the function out:
    Code:
    Private Function GetMD5String(ByVal strFilename As String) As String
    
            Dim cMD5 = Security.Cryptography.MD5.Create
            Dim bytHash As Byte()
            Dim sb As New System.Text.StringBuilder
    
            Using cStream As New IO.FileStream(strFilename, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)
                bytHash = cMD5.ComputeHash(cStream)
            End Using
    
            For Each c In bytHash
                sb.Append(c.ToString("X2"))
            Next
    
            Return sb.ToString
    
        End Function
    Code:
        Private Sub CheckFile(strFilename As String)
            Dim scanbox As New TextBox
            scanbox.Text = My.Computer.FileSystem.ReadAllText("viruslist.txt").ToString
            Me.OpenFileDialog1.FileName = strFilename
    
            Dim MD5 as String = GetMD5String(strFilename)
    
            If scanbox.Text.Contains(MD5) Then
                TextBox1.Text = strFilename
                Me.OpenFileDialog1.FileName = strFilename
                Detect.ShowDialog()
                WriteToLog("Malicious exploit detected.")
                Quarantinevb.ListBox1.Items.Add(strFilename)
            End If
    
        End Sub
    Now GetMD5String just gets an MD5 string, and CheckFile does the rest of the steps (which I reckon is checking a file).

    The usage of this code would now be a bit simpler, eg:
    Code:
    CheckFile("C:\Folder\File.tmp")
    Note that there are several things in the code that I've moved to CheckFile that are odd, for example I don't understand what the usage of OpenFileDialog1 is about, and I don't understand why scanbox is a TextBox (shouldn't it just be a String? that would store text, but use less memory and be a bit quicker).

    It is also arguably not the best idea to be re-loading a file (viruslist.txt) every time you check a file, as that slows things down a lot (but it is a good idea if you expect viruslist.txt to change roughly as frequently as you check a file). Either way, it is wise to specify a full path to the file (as in my examples) instead of just the filename.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width