Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2017-10-12
4
Low Priority
?
29 Views
Last Modified: 2017-10-16
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
Comment
Question by:Jason Paradis
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 85

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 500 total points
ID: 42328661
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
 
LVL 1

Author Comment

by:Jason Paradis
ID: 42328685
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
 
LVL 1

Author Comment

by:Jason Paradis
ID: 42328911
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
 
LVL 34

Assisted Solution

by:it_saige
it_saige earned 500 total points
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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Join & Write a Comment

Make the most of your online learning experience.
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
Six Sigma Control Plans
Introduction to Processes
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question