• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 53
  • Last Modified:

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.
0
Jason Paradis
Asked:
Jason Paradis
  • 2
2 Solutions
 
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
 
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now