Results 1 to 14 of 14

Thread: I want to use a Function inside a sub, how is this done?

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2018
    Posts
    51

    Question I want to use a Function inside a sub, how is this done?

    I have a Private Function called GetMD5String. All this function does, is it scans for files that are being opened, and if their MD5 matches with a database, an alert is shown saying
    Virus detected!

    Everything works well, however, I have one issue.

    I need to refresh the GetMD5String function so that my program continues to have GetMD5String monitoring the files being opened. The way I think is the most practical way of doing it, is putting my GetMD5String function into a Timer1_Tick sub so my program can check every set interval. I've tried this, but I'm getting this error:


    Argument not specified for parameter 'strFilename' of 'Private Function GetMD5String(strFilename As String) As String

    I get this error when I try to pass GetMD5String() into Timer1_Tick


    Is there any way I can refresh GetMD5String without needing to use a Timer? Or, by using a Timer but a different way of doing so?


    GetMD5String code:



    Code:
    Private Function GetMD5String(ByVal strFilename As String) As String
            Dim MD5 As String = GetMD5String(strFilename)
            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
                Detect.ShowDialog()
                WriteToLog("Malicious exploit detected.")
                Quarantinevb.ListBox1.Items.Add(strFilename)
            End If
    
            Return sb.ToString
    
    
    
        End Function


    Thanks

  2. #2
    Lively Member
    Join Date
    Jun 2018
    Posts
    80

    Re: I want to use a Function inside a sub, how is this done?

    Code:
    Private Function GetMD5String(ByVal strFilename As String) As String
            Dim MD5 As String = GetMD5String(strFilename)
            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
                Detect.ShowDialog()
                WriteToLog("Malicious exploit detected.")
                Quarantinevb.ListBox1.Items.Add(strFilename)
            End If
    
            Return sb.ToString
    
        End Function
    I want to use a Function inside a sub, how is this done?
    This is not a Sub it IS the function.

    PS: When you declare the function with (ByVal strFilename As String) you are creating a NEW var called strFilename.
    You should assign a value before using it it.

    KBConsole
    Last edited by KBConsole; Jun 22nd, 2018 at 08:45 AM.

  3. #3
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,140

    Re: I want to use a Function inside a sub, how is this done?

    Your previous thread in which you basically asked the same question had a very complete explanation of the error you are receiving.

    http://www.vbforums.com/showthread.p...ing)-As-String

    Your idea of a timer with a 1 second interval is not necessarily the most practical way of doing it. That would result in programs being allowed to execute for an average of at least 0.5 seconds before your program can terminate them, which may be long enough for some payload to take place.

    A more practical way of doing it is for your program to hook into the operating system so that your program sits in between the user initiating the execution of the program and the program actually executing, so that your program can scan the file and control whether it is executed or not. That might well be above and beyond your current coding capabilities.

    If your goal is just tinkering for the sake of learning, then the absolute best of luck to you, and have fun with it.

  4. #4

    Thread Starter
    Member
    Join Date
    Jun 2018
    Posts
    51

    Re: I want to use a Function inside a sub, how is this done?

    How do I hook it to the OS?

  5. #5
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: I want to use a Function inside a sub, how is this done?

    Quote Originally Posted by Modulus View Post
    How do I hook it to the OS?
    You could run it as a Scheduled Task to fire every so often, could have it run at Windows Startup via the Windows Registry, or you could install it as a Service with startup with the OS.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  6. #6

    Thread Starter
    Member
    Join Date
    Jun 2018
    Posts
    51

    Re: I want to use a Function inside a sub, how is this done?

    Quote Originally Posted by JuggaloBrotha View Post
    You could run it as a Scheduled Task to fire every so often, could have it run at Windows Startup via the Windows Registry, or you could install it as a Service with startup with the OS.
    Do you have any references on how to do this?

  7. #7
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,140

    Re: I want to use a Function inside a sub, how is this done?

    No offense, but since you now have three different threads where you are still reportedly struggling with the same concept of calling a function that requires a parameter be passed, I don't believe you are currently skilled to do anything like what you are asking about.

    The only thing providing you answers to your questions above would do is result in more questions, since those answers would involve concepts that are likely beyond your current scope of programming knowledge.

    First, figure out how to call a function and pass a parameter. Then, stick with your current plan of using a timer and at least get the program functional. Then, do more reading and research and coding and keep improving your program as you learn new things.

    Good luck.
    Last edited by OptionBase1; Jun 23rd, 2018 at 09:55 AM.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,352

    Re: I want to use a Function inside a sub, how is this done?

    I have to agree with OptionBase here. You're asking questions that are, on their face, a bit nonsensical and then, when people make basic suggestions about what you should do, the concepts seem to mean nothing to you. I would suggest that you need to learn the basics of VB.NET programming first. There's a tutorial link in my signature below and I would suggest that you follow it and work through the entire tutorial before doing anything else. Calling methods is one of the absolute fundamentals of VB.NET programming and it's something you should know how to do before posting anything here. If you have a question about something that is difficult to do, we should be able to tell you to call a method as part of the solution and you should be able to do that, which is obviously not the case at the moment.

  9. #9
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: I want to use a Function inside a sub, how is this done?

    Quote Originally Posted by Modulus View Post
    Do you have any references on how to do this?
    Google
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  10. #10
    Fanatic Member Peter Porter's Avatar
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    538

    Re: I want to use a Function inside a sub, how is this done?

    Quote Originally Posted by jmcilhinney View Post
    I would suggest that you need to learn the basics of VB.NET programming first. There's a tutorial link in my signature below and I would suggest that you follow it and work through the entire tutorial before doing anything else.
    jmcilhinney, your tutorial links don't always work, but they've been archived by the Wayback Machine:

    Microsoft Visual Basic .NET tutorials for beginners

    Visual C# .NET for complete beginners

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,352

    Re: I want to use a Function inside a sub, how is this done?

    Quote Originally Posted by Peter Porter View Post
    jmcilhinney, your tutorial links don't always work
    That's odd. They've never not worked for me and they are working for me now.

  12. #12
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,047

    Re: I want to use a Function inside a sub, how is this done?

    They work for me, too. What are you seeing when they don't work? Do you get something like a 404, or an error?
    My usual boring signature: Nothing

  13. #13
    Fanatic Member Peter Porter's Avatar
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    538

    Re: I want to use a Function inside a sub, how is this done?

    The site has been having issues. First I couldn't connect, and when I was able to, some of the tutorials were 404 pages, but everything seems to be working now.

  14. #14
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: I want to use a Function inside a sub, how is this done?

    Quote Originally Posted by Peter Porter View Post
    The site has been having issues. First I couldn't connect, and when I was able to, some of the tutorials were 404 pages, but everything seems to be working now.
    Were you logged into the forum at the time of clicking?

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