• 0

VB button commands


Question

hi,

i'm not exactly experienced with VB, but i thought i'd make a start at it...

basically what i have right now is a form and two buttons, a "yes" button and a "no" button. i want the no button to close the form, which i think is simple code.

the problem is, the yes button code is very tricky: i want it to run a command line program to hibernate the computer, which isn't exactly just like opening or closing a form. i have the command line i need, and it works fine in the run box.

what i need is some code that i can paste my code into, and when the button is clicked it will hibernate the computer. (and maybe some code to close the form please)

thanks in advance,

Prism128

PS: before you all tell me to just click start -> shutdown etc, i'm running object dock so there is no start menu, and it takes ages when u press the start button, and i occasionally accidentally click the hibernate shortcut instead of one i want, so it shuts down :(

Link to comment
Share on other sites

10 answers to this question

Recommended Posts

  • 0

I'm guessing you want to do it using the command line method:

(declarations)
Private Declare Function ShellExecuteA Lib "shell32.dll" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

...

ShellExecuteA(hWnd, "open", "shutdown -h", vbNullString, App.Path, vbNormalFocus)

But the proper way is to use an API:

(declarations)
Private Declare Function SetSuspendState Lib "powrprof.dll" (ByVal Hibernate As Long, ByVal ForceCritical As Long, ByVal DisableWakeEvent As Long) As Long

...

SetSuspendState(True, True, True)

If you want to deploy your program to multiple computers, you might also want to use the following API function to check if the computer can actually hibernate first:

Private Declare Function IsPwrHibernateAllowed Lib "powrprof.dll" () As Integer

...

Select Case IsPwrHibernateAllowed
 ? Case = 0
 ? ? ?MsgBox "Your computer cannot hibernate."
 ? Case Else
 ? ? ?(hibernate)
End Select

Link to comment
Share on other sites

  • 0

oh i found it. thanks a lot :D. it seems to work with all the commands i've tried (apart from the one i will be using with it, as i don't want 2 shut down just yet!)

Link to comment
Share on other sites

  • 0

i think he knows that, Tbhoc, he said he had that fine. I thought you had to use APIs for hibernating, but if it's in shutdown then Shell("cmd /c shutdown -h") should work on NT-based systems.

Link to comment
Share on other sites

This topic is now closed to further replies.