Results 1 to 23 of 23

Thread: [RESOLVED] VB.net and windows vista bcdedit

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2009
    Posts
    173

    Resolved [RESOLVED] VB.net and windows vista bcdedit

    I have a program that modifies the boot.ini on windows XP.
    However the problem is windows Vista went to the BCDEDIT utility the boot.ini was eliminated. The BCDEDIT tool is a command line tool.
    IS there a way to program vb.net to use bcdedit and pass several different commands to it so that I can make the same changes in Vista

    I want to change to these values from there defaults

    Timeout =1
    and
    noexecute = AlwaysOff

    How can I do this ?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: VB.net and windows vista bcdedit

    There a submission from gigemboy in the VB.NET CodeBank forum dedicated to automating a command prompt.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2009
    Posts
    173

    Re: VB.net and windows vista bcdedit

    I have been looking at the code for two days and cannot figure out how to modify it to what I need.

    I dont want a textbox or anything like that showing.

    I need to have two seperate bcdedits based on two different checkboxes being checked.

    One checkbox would change the timeout setting
    The other would change the OptIn to AlwaysOff

  4. #4

    Re: VB.net and windows vista bcdedit

    Well, what are the command lines for BCEDIT on those specific options? If you know those, then you should be able to develop what you need in order to do this.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Aug 2009
    Posts
    173

    Re: VB.net and windows vista bcdedit

    I know the command lines for BCDEDIT what I dont know is how to code those command lines into the example code that was mentioned by the previous poster.

    Telling me I should know how to do it isnt very helpful when I am saying I don't know how...

  6. #6

    Re: VB.net and windows vista bcdedit

    Well I don't know the command lines, so I'm asking you what they are so I can help you get it to work.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: VB.net and windows vista bcdedit

    The principle is fairly simple. You start a Process, create a StreamWriter on its stdin stream and a StreamReader on its stdout stream. Whatever you would normally type into your console window you instead write to the StreamWriter. Whatever the program would normally write out to the console window you read from the StreamReader. That's pretty much all there is to it.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Aug 2009
    Posts
    173

    Re: VB.net and windows vista bcdedit

    Sorry I misunderstood you, the two seperate commands are

    Code:
    bcdedit /timeout 1
    bcdedit /nx AlwaysOff

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Aug 2009
    Posts
    173

    Re: VB.net and windows vista bcdedit

    I looked at the code example and it has me confused because it has a texxtbox and a button. I dont want text boxes, this all needs to happen in the background when the checkmark box is checked

    the code i was looking at is here http://www.vbforums.com/showthread.php?t=381405

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: VB.net and windows vista bcdedit

    If you don't want the TextBoxes then don't use them. Have you read post #7? Just do what you need.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Aug 2009
    Posts
    173

    Re: VB.net and windows vista bcdedit

    Quote Originally Posted by jmcilhinney View Post
    If you don't want the TextBoxes then don't use them. Have you read post #7? Just do what you need.
    Post 7 by Sonitin is asking a question...


    Im sorry I am still getting the ropes of vb and to me im confused as to what to do here.


    The code is here:
    Code:
           Dim CMDThread As New Threading.Thread(AddressOf CMDAutomateThread)
            CMDThread.Start()
        End Sub
        Private Sub CMDAutomateThread()
            TextBox1.Text = CMDAutomate("date", "12/31/2099") 'Set textbox to string return
        End Sub
    
        Private Function CMDAutomate(ByVal cmdString As String, ByVal cmdString2 As String) As String
            Dim myprocess As New Process
            Dim StartInfo As New System.Diagnostics.ProcessStartInfo
            Dim s As String = ""
            StartInfo.FileName = "cmd" 'starts cmd window        
            StartInfo.RedirectStandardInput = True
            StartInfo.RedirectStandardOutput = True
            StartInfo.UseShellExecute = False 'required to redirect    
            ' StartInfo.CreateNoWindow = True 'creates no cmd window         
            myprocess.StartInfo = StartInfo
            myprocess.Start()
            Dim SR As System.IO.StreamReader = myprocess.StandardOutput
            Dim SW As System.IO.StreamWriter = myprocess.StandardInput
            SW.WriteLine(cmdString) 'the commands you wish to run.....      
            SW.WriteLine(cmdString2)
            s = SR.ReadToEnd 'returns results of the command window  before exit
            SW.WriteLine("exit") 'exits command prompt window      
            SW.Close()
            SR.Close()
            Return s
        End Function
    I don't understand what to do with this because if i remove it then i get an error because of this call Dim CMDThread As New Threading.Thread(AddressOf CMDAutomateThread)

    Code:
     Private Sub CMDAutomateThread()
            TextBox1.Text = CMDAutomate("date", "12/31/2099") 'Set textbox to string return
        End Sub

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: VB.net and windows vista bcdedit

    I meant post #7 in THIS thread, not THAT one. Forget all the threading part. All you need is the contents of the CMDAutomate method. That's the method implements exactly what I said you need to do in post #7.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Aug 2009
    Posts
    173

    Re: VB.net and windows vista bcdedit

    Here is the code that worked in XP ... basically I need the same thing to happen in vista where I can do several things.

    1. I need to be able to change the value
    2. I need to be able to change it back to the default one
    3. I need to be able to check its current state and if it is already set for timeout =1 the box for that option is checked showing that it has been changed to that

    Code:
     ' changes the timeout setting in the boot.ini to 1 second 
        Sub OSwaitOptEnable()
            Dim fi As New IO.FileInfo("c:\boot.ini")
            fi.Attributes = IO.FileAttributes.Normal
            Dim fileStr As String = IO.File.ReadAllText("c:\boot.ini")
            fileStr = Regex.Replace(fileStr, "timeout=\d+", "timeout=1")
            IO.File.WriteAllText("c:\boot.ini", fileStr)
            fi.Attributes = IO.FileAttributes.ReadOnly
    
        End Sub
        ' Puts the timeout line in the boot.ini back to the default setting of 30 seconds
        Sub OSwaitOptDefault()
            Dim fi As New IO.FileInfo("c:\boot.ini")
            fi.Attributes = IO.FileAttributes.Normal
            Dim fileStr As String = IO.File.ReadAllText("c:\boot.ini")
            fileStr = Regex.Replace(fileStr, "timeout=\d+", "timeout=30")
            IO.File.WriteAllText("c:\boot.ini", fileStr)
            fi.Attributes = IO.FileAttributes.ReadOnly
        End Sub
        ' Disables Data Execution Prevention 
        Sub DEPDisable()
            Dim fi As New IO.FileInfo("c:\boot.ini")
            fi.Attributes = IO.FileAttributes.Normal
            Dim fileStr As String = IO.File.ReadAllText("c:\boot.ini")
            fileStr = Regex.Replace(fileStr, "noexecute=optin", "noexecute=AlwaysOff")
            IO.File.WriteAllText("c:\boot.ini", fileStr)
            fi.Attributes = IO.FileAttributes.ReadOnly
        End Sub
        ' Re-enables DEP back to its default setting
        Sub DEPDefault()
            Dim fi As New IO.FileInfo("c:\boot.ini")
            fi.Attributes = IO.FileAttributes.Normal
            Dim fileStr As String = IO.File.ReadAllText("c:\boot.ini")
            fileStr = Regex.Replace(fileStr, "noexecute=AlwaysOff", "noexecute=optin")
            IO.File.WriteAllText("c:\boot.ini", fileStr)
            fi.Attributes = IO.FileAttributes.ReadOnly
        End Sub

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Aug 2009
    Posts
    173

    Re: VB.net and windows vista bcdedit

    Post 7 means nothing to me unfortunately.... I've only been doing this for about two months and have never worked with stream writers or readers...

  15. #15
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: VB.net and windows vista bcdedit

    First up, if you haven't ever worked with StreamWriters and StreamReaders then you should be reading about them on MSDN or the web in general instead of asking us to write your code for you.

    That said, you don't even really need to know how because the code is right in front of you. The very code you copied and pasted from that other thread creates a StreamReader and a StreamWriter.

    It seems to me that you're just assuming that this is too hard and waiting for someone else to do it for you. I don't expect you to know everything to begin with but if you know that you need to use a StreamReader and you know you don't know how then you know that that is what you need to research. You've got an example that relates to exactly what you want to do. If that's not enough then you need to do some reading for yourself.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  16. #16
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: VB.net and windows vista bcdedit

    It's very simple. Instead of doing all of that, you can break it down to its fundamental parts.

    Dim a variable to hold your process. Next, call the StartInfo property of the Process so you can call methods such as WorkingDirectory, FileName, Arguments, etc.

    Call start and then you're good to go.

    Doing this way will open the command prompt, but the method JMC linked to redirects it to a text box.

    Here's a starting example:
    vb Code:
    1. Dim proc As New Process
    Last edited by weirddemon; Oct 2nd, 2009 at 09:23 AM. Reason: Forgot to include "New" while declaring a new process.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  17. #17

    Thread Starter
    Addicted Member
    Join Date
    Aug 2009
    Posts
    173

    Re: VB.net and windows vista bcdedit

    Will this work ?

    Code:
    Sub OSWaitMinimal()
            Shell("cmd bcdedit /timeout 1", AppWinStyle.Hide)
        End Sub
        Sub OSWaitDefault()
            Shell("cmd bcdedit /timeout 30", AppWinStyle.Hide)
        End Sub
    
        Sub OptInOff()
            Shell("cmd bcedit /nx AlwaysOff", AppWinStyle.Hide)
        End Sub
        Sub OptInOn()
            Shell("cmd bdcedit /nx OptIn", AppWinStyle.Hide)
        End Sub
    if so how would i read what the current configuration is in bcdedit ?

  18. #18
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: VB.net and windows vista bcdedit

    Shell is outdated. You'll want to use the Process.Start class instead. Which is what we've been telling you.

    Play around with Intellisense and see what you get.

    You're making this way harder than it it is.

    I gave you a really simple explanation. Actually read what I said and apply it. I showed you how to make the variable, now on a new line call the variable's StartInfo property and see what you get.

    I also gave you the methods you needed: FileName, Arguments, and Start
    Last edited by weirddemon; Oct 2nd, 2009 at 07:39 AM. Reason: The usual: Spelling mistaks ;)
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  19. #19

    Thread Starter
    Addicted Member
    Join Date
    Aug 2009
    Posts
    173

    Re: VB.net and windows vista bcdedit

    I did that and it told me declaration expected. So I dont know what Im doing wrong here.

    Ive tried it as a function,module, class and regular form it does the same thing in all of them

  20. #20

    Thread Starter
    Addicted Member
    Join Date
    Aug 2009
    Posts
    173

    Re: VB.net and windows vista bcdedit

    how about this below ?

    It is saying this though "Warning 3 Variable 'proc' is used before it has been assigned a value. A null reference exception could result at runtime."


    Code:
        Sub OSwaitEnable()
            Dim proc As Process
            proc.StartInfo.CreateNoWindow = True
            proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
            proc.StartInfo.FileName = "C:\%windir%\system32\cmd.exe"
            proc.StartInfo.Arguments = "bcdedit /timeout 1"
            proc.Start()
            proc.Close()
            proc.Dispose()
    
        End Sub

  21. #21
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: VB.net and windows vista bcdedit

    I don't really understand what the issue is here. You've already got the code. You posted it yourself.
    Code:
            Dim myprocess As New Process
            Dim StartInfo As New System.Diagnostics.ProcessStartInfo
            Dim s As String = ""
            StartInfo.FileName = "cmd" 'starts cmd window        
            StartInfo.RedirectStandardInput = True
            StartInfo.RedirectStandardOutput = True
            StartInfo.UseShellExecute = False 'required to redirect    
            ' StartInfo.CreateNoWindow = True 'creates no cmd window         
            myprocess.StartInfo = StartInfo
            myprocess.Start()
            Dim SR As System.IO.StreamReader = myprocess.StandardOutput
            Dim SW As System.IO.StreamWriter = myprocess.StandardInput
            SW.WriteLine(cmdString) 'the commands you wish to run.....      
            SW.WriteLine(cmdString2)
            s = SR.ReadToEnd 'returns results of the command window  before exit
            SW.WriteLine("exit") 'exits command prompt window      
            SW.Close()
            SR.Close()
    You simply change the writing calls to write whatever it is that you want to write, which you've also already posted:
    bcdedit /timeout 1
    bcdedit /nx AlwaysOff
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  22. #22
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: VB.net and windows vista bcdedit

    Quote Originally Posted by TheBouncer View Post
    how about this below ?

    It is saying this though "Warning 3 Variable 'proc' is used before it has been assigned a value. A null reference exception could result at runtime."


    Code:
        Sub OSwaitEnable()
            Dim proc As Process
            proc.StartInfo.CreateNoWindow = True
            proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
            proc.StartInfo.FileName = "C:\%windir%\system32\cmd.exe"
            proc.StartInfo.Arguments = "bcdedit /timeout 1"
            proc.Start()
            proc.Close()
            proc.Dispose()
    
        End Sub
    You'll want to declare it as a new process.

    You don't need to specify the path of the command window because it is in the system PATH. Also, I believe bcdedit.exe is also in the system PATH so you don't need to call CMD. Just call bcdedit.exe directly.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  23. #23

    Thread Starter
    Addicted Member
    Join Date
    Aug 2009
    Posts
    173

    Re: VB.net and windows vista bcdedit

    Sorry I haven't got back sooner...

    Thank you very much weirddemon its working perfectly now!

    you were right about just calling bcdedit.exe

    Also thank you jmcilhinney for your help too!

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