Results 1 to 9 of 9

Thread: [RESOLVED] Shutting down/standby/or hibernate windows with VB6

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2010
    Posts
    10

    Resolved [RESOLVED] Shutting down/standby/or hibernate windows with VB6

    Hi everyone,

    I have used this site once previously and you guys were able to figure it out really quickly so I hope I can get the same results this time. I wish I could give back help to this site but I doubt I'm in a position of expertise to do so. xD

    Background:

    I have already made a program that opens and closes web browsers to designated pages at on one minute intervals, kind of like a macro, (to get views, kind of like youtube views).

    I have this program set up to read from a file that the user can edit to add or delete urls in the file and right now there are a quite a bit of urls. The program takes a bit of time to do everything it needs to do so I just let it run over night and turn of my computer in the morning.

    Problem:

    However, I would like to be able to make my program automatically turn off/standby/hibernate when its done doing its thang.



    I have looked around for a few hours and tried several coding samples such as

    Code:
    #If Win32 Then
        Private Declare Function Shutdown Lib "user32" Alias _
            "ExitWindowsEx" (ByVal uFlags As Long, ByVal _
            dwReserved As Long) As Long
        Private Const EWX_LOGOFF = 0
        Private Const EWX_SHUTDOWN = 1
        Private Const EWX_REBOOT = 2
        Private Const EWX_FORCE = 4
    #Else
        Private Declare Function Shutdown Lib "User" Alias _
            "ExitWindows" (ByVal dwReturnCode As Long, ByVal _
            wReserved As Integer) As Integer
        Private Const EW_REBOOTSYSTEM = &H43
        Private Const EW_RESTARTWINDOWS = &H42
    #End If
    
    Private SelectedOption As Integer
    
    Private Sub Form_Load()
        #If Win32 Then
            ' Prepare for 32 bit ExitWindowsEx.
            optExitOption(0).Caption = "Normal Shutdown"
            optExitOption(0).Tag = EWX_SHUTDOWN
    
            optExitOption(1).Caption = "Reboot"
            optExitOption(1).Tag = EWX_REBOOT
            
            optExitOption(2).Caption = "Log Off"
            optExitOption(2).Tag = EWX_LOGOFF
            
            optExitOption(3).Caption = "Forced Shutdown"
            optExitOption(3).Tag = EWX_FORCE
        #Else
            ' Prepare for 16 bit ExitWindows.
            optExitOption(0).Caption = "Normal Shutdown"
            optExitOption(0).Tag = 0
    
            optExitOption(1).Caption = "Reboot"
            optExitOption(1).Tag = EW_REBOOTSYSTEM
    
            optExitOption(2).Caption = "Restart Windows"
            optExitOption(2).Tag = EW_RESTARTWINDOWS
            
            optExitOption(3).Visible = False
        #End If
    End Sub
    
    Private Sub cmdOk_Click()
    Dim exit_option As Long
    
        exit_option = CLng(optExitOption(SelectedOption).Tag)
        'MsgBox ("")
        Shutdown exit_option, 0
    End Sub
    (which I believe I found on this site)

    and

    Code:
    Private Sub Command1_Click()
    Shell _
    ("Rundll32.exe user,_exitwindows")
    End Sub
    I am not too experienced in VB (mostly self taught) but I have managed to make something that can shell a program and i/o files and info so its not like I'm learning how to use a command button to display "Hello World!"

    Any help is greatly appreciated

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Shutting down/standby/or hibernate windows with VB6

    the api should work fine
    what is happening now, error or wrong result?
    what value for exit_option when calling shutdown?
    what value for selectedoption, or is it a function?

    you could also try shelling out to shutdown.exe, system32 directory
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2010
    Posts
    10

    Re: Shutting down/standby/or hibernate windows with VB6

    Quote Originally Posted by westconn1 View Post
    the api should work fine
    what is happening now, error or wrong result?
    what value for exit_option when calling shutdown?
    what value for selectedoption, or is it a function?

    you could also try shelling out to shutdown.exe, system32 directory
    For the second code, I got an error saying "error loading user the specified module could not be found"

    For the first one, nothing would happen.

    And there was another one that I tried that was similar to the second code that when I put a value of 0 (I think it was zero) my computer would log off right away. But supposedly there was another value (i think 4) for shut down (as well as others for restarting) that upon trying to use, nothing would happen.

    I think the only reason the first code is so long is to that the user can decide if they want to shut down/restart/log off, etc.

    Now about that shutdown thing in system32, would that ask the user to save open applications and stuff? I need the computer to just shut down right away after its done. Either that or put it into standby or hibernation so that the computer isnt just running all night.

    I tried to make a timed shut down off the power options (as in Hibernate after 40 minutes) but in the morning my computer was still running so I don't really know what happened there. I think it was becaus a message box is desplayed at the end saying that the program is finished and the computer wasn't actually idle while that box was displayed so it never activated the "hibernate after 40 minutes" option

    Thoughts?

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Shutting down/standby/or hibernate windows with VB6

    I think the only reason the first code is so long is to that the user can decide if they want to shut down/restart/log off, etc.
    in that case try cutting all the options and just call shutdown with value of ewx_force
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    New Member
    Join Date
    Aug 2010
    Posts
    10

    Re: Shutting down/standby/or hibernate windows with VB6

    Quote Originally Posted by westconn1 View Post
    in that case try cutting all the options and just call shutdown with value of ewx_force
    ok i simplified it down into this code:
    Code:
    Private Declare Function Shutdown Lib "user32" Alias _
            "ExitWindowsEx" (ByVal uFlags As Long, ByVal _
            dwReserved As Long) As Long
    Option Explicit
    
    Private Sub Command1_Click()
    Shutdown 0, 0
    End Sub
    it works with values of "0, 0" (log off) and "4, 0" (force log off)
    but nothing happens when i click it for values of "1, 0" (shut down) and "2, 0" (reboot)

    Last edited by Tagger 24; Dec 12th, 2010 at 12:34 AM. Reason: clarifying

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Shutting down/standby/or hibernate windows with VB6

    but nothing happens when i click it for values of "1, 0" (shut down) and "2, 0" (reboot)
    probably waiting for some user interaction, if unattended shutdown, use force
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Shutting down/standby/or hibernate windows with VB6

    From the Microsoft documentation for ExitWindowsEx (http://msdn.microsoft.com/en-us/libr...(v=VS.85).aspx)
    To shut down or restart the system, the calling process must use the AdjustTokenPrivileges function to enable the SE_SHUTDOWN_NAME privilege. For more information, see Running with Special Privileges.
    Even though the process is asynchronous, you ought to be checking the return value from the function.

  8. #8
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    Re: Shutting down/standby/or hibernate windows with VB6

    While you're at it you may want to consider how Anti-Virus software will react to this call if you plan to deploy your app.
    <--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
    If topic has been resolved, please pull down the Thread Tools & mark it Resolved.


    Is VB consuming your life, and is that a bad thing??

  9. #9

    Thread Starter
    New Member
    Join Date
    Aug 2010
    Posts
    10

    Resolved Re: Shutting down/standby/or hibernate windows with VB6

    i kind of found out how to do it.
    http://www.vbforums.com/showthread.php?t=396217
    this link is to a program that hibernates and unhibernates the computer like an alarm clock - more or less what i wanted. I dont quite understand it but i guess i can try to tinker with it to suit my needs

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