VB and "Subs", relationship between button clicks and others

I'm extremely new to programming. I've been tinkering with it on and off for decades but have never written an actual program. That changed yesterday.

Now, I've hit a brick wall.

I do not understand the fundamentals, but I get how code works for the most part.

My question is I have code that looks like this:

Private Sub Crystl32Button_Click(ByVal sender As System.Object, e As EventArgs) Handles Crystl32Button.Click
        For Each P As Process In System.Diagnostics.Process.GetProcessesByName("InHouseApp")
            P.Kill()
            P.WaitForExit()
        Next
        My.Computer.FileSystem.CopyFile("\\remoteserver\software\iha\ocx\crystl32\*.*", "C:\Windows\System32\",
        Microsoft.VisualBasic.FileIO.UIOption.AllDialogs, Microsoft.VisualBasic.FileIO.UICancelOption.DoNothing)
    End Sub

Open in new window


I want to add the functionality of this:

Sub RemoveFileSecurity(ByVal fileName As String, ByVal account As String, _
        ByVal rights As FileSystemRights, ByVal controlType As AccessControlType)

        ' Get a FileSecurity object that represents the 
        ' current security settings.
        Dim fSecurity As FileSecurity = File.GetAccessControl(fileName)

        ' Remove the FileSystemAccessRule from the security settings. 
        fSecurity.RemoveAccessRule(New FileSystemAccessRule(account, _
            rights, controlType))

        ' Set the new access settings.
        File.SetAccessControl(fileName, fSecurity)

    End Sub

Open in new window


This code is supposed to remove the ACL listing of a specific account for a file.

However, I do not know how to add this:

Sub RemoveFileSecurity(ByVal fileName As String, ByVal account As String, _
        ByVal rights As FileSystemRights, ByVal controlType As AccessControlType)

Open in new window


To my button click sub. Whenever I try, it auto-completes and removes the "Sub" I type in. I assume you can't have a Sub within a Sub but what do I modify in order to get the rest of the ACL code to work with the button click? Do I have to modify the button click Sub in order to match the ACL one?

I apologize if I'm not making sense. Again, this is my first real program and I'm not exactly up to par on all the methodologies.
LVL 1
Jason ParadisIT Operations SpecialistAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
You could call your RemoveFileSecurity sub from the button click:

Private Sub Crystl32Button_Click(ByVal sender As System.Object, e As EventArgs) Handles Crystl32Button.Click
        For Each P As Process In System.Diagnostics.Process.GetProcessesByName("InHouseApp")
            P.Kill()
            P.WaitForExit()
        Next
        My.Computer.FileSystem.CopyFile("\\remoteserver\software\iha\ocx\crystl32\*.*", "C:\Windows\System32\",
        Microsoft.VisualBasic.FileIO.UIOption.AllDialogs, Microsoft.VisualBasic.FileIO.UICancelOption.DoNothing)
        '/ assuming you want to do it here:
        RemoveFileSecurity(blah blah)
End Sub

Open in new window

Of course, you'll have to know the values that RemoveFileSecurity requires:

fileName As String
account As String
rights As FileSystemRights
controlType As AccessControlType

I'm not sure where you'd get those, as it's not clear what you're trying to accomplish.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jason ParadisIT Operations SpecialistAuthor Commented:
The problem I'm trying to fix is that our in house app has a DLL file that is so old Windows likes to lock it down and change the ACL from default to BUILTIN with Deny access on it whenever Windows Update runs or a reboot happens. Once changed the in house app fails as the DLL/OCX is no longer available.

First I copy a good version of the OCX file over to the System32 directory then I need to remove the BUILTIN account from security. Once BUILTIN is removed from the ACL the default ACL config should come back.

I'm not in charge of the in house app, that's another company. I'm just in charge of finding quick workarounds when it breaks.

I'll try your solution.
0
Jason ParadisIT Operations SpecialistAuthor Commented:
Unfortunately, I couldn't get it to work so I just put icacls commands in Shell(). I'd like to have it work the other way but nothing was coming out right.

VS kept underlining RemoveAccessRule and saying it wasn't declared.

I was going by the instructions found here but RemoveAccessRule is not declared in the example there either: https://msdn.microsoft.com/en-us/library/system.io.file.setaccesscontrol(v=vs.110).aspx
0
it_saigeDeveloperCommented:
Hi Jason,

Scott provided the correct answer, however, I think you misunderstood what he meant when he stated:
You could call your RemoveFileSecurity sub from the button click

You still have to declare the RemoveFileSecurity Sub; e.g. -
Private Sub Crystl32Button_Click(ByVal sender As System.Object, e As EventArgs) Handles Crystl32Button.Click
    For Each P As Process In System.Diagnostics.Process.GetProcessesByName("InHouseApp")
        P.Kill()
        P.WaitForExit()
    Next
    My.Computer.FileSystem.CopyFile("\\remoteserver\software\iha\ocx\crystl32\*.*", "C:\Windows\System32\",
    Microsoft.VisualBasic.FileIO.UIOption.AllDialogs, Microsoft.VisualBasic.FileIO.UICancelOption.DoNothing)
    RemoveFileSecurity("someFileName", "someAccountName", FileSystemRights.FullControl Or FileSystemRights.TakeOwnership, AccessControlType.Allow)
End Sub

Sub RemoveFileSecurity(ByVal fileName As String, ByVal account As String,
    ByVal rights As FileSystemRights, ByVal controlType As AccessControlType)

    ' Get a FileSecurity object that represents the 
    ' current security settings.
    Dim fSecurity As FileSecurity = File.GetAccessControl(fileName)

    ' Remove the FileSystemAccessRule from the security settings. 
    fSecurity.RemoveAccessRule(New FileSystemAccessRule(account,
        rights, controlType))

    ' Set the new access settings.
    File.SetAccessControl(fileName, fSecurity)
End Sub

Open in new window


One observation I would like to make is that this may fail because you kill the process that calls it ("InHoueApp").

-saige-
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.