VB.NET

Moderators: seancampbell
Number of threads: 4020
Number of posts: 10026

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Shut down the computer Posted by M V Mack on 3 Aug 2007 at 3:32 AM
Windows XP
I am trying to shut down the computer after saving files and have tried to use this code which was supplied on another thread. The code works fine for loging off but I can't get it to shut down the computer. Am I missing something?


Module Module1
Declare Function ExitWindowsEx Lib "user32" Alias "ExitWindowsEx" (ByVal uFlags As _
Integer, ByVal dwReserved As Integer) As Integer

Public Enum Choices
Logoff = 0
Shutdown = 1
Reboot = 2
ForceLogOff = 4
ForceReboot = 6
PowerDown = 8
ForcePowerDown = 12
End Enum

Public Sub ExitFromWindows(ByVal Choice As Choices)
ExitWindowsEx(Choice, 0)
End Sub

End Module

Thanks Malcolm Mack
Report
Re: Shut down the computer Posted by BitByBit_Thor on 3 Aug 2007 at 8:24 AM
Well you might not have shutdown priviledges.

Or else, try calling the API twice in a row. I've seen it used occasionally.

Best Regards,
Richard

The way I see it... Well, it's all pretty blurry
Report
Re: Shut down the computer Posted by M V Mack on 3 Aug 2007 at 3:51 PM
I am the computer administrator, is there something else I need to do to have shut down privalages?

Malcolm
Report
Re: Shut down the computer Posted by BitByBit_Thor on 4 Aug 2007 at 1:35 AM
Ahhh... the heavenly archives of MSDN

http://msdn2.microsoft.com/en-us/library/aa376868.aspx

In particular, check the shutdown constants, and the remarks section.
Mostly, the AdjustTokenPriveledge part (also: http://msdn2.microsoft.com/en-us/library/aa375202.aspx)

EDIT: Complete example in C: http://msdn2.microsoft.com/en-us/library/aa376871.aspx

Now note that this code will only work for XP and above.
If you're also planning on using it for 98 and/or NT/2000 then you'll probably have to get a bit móre complicated.

Best Regards,
Richard

The way I see it... Well, it's all pretty blurry
Report
Re: Shut down the computer Posted by M V Mack on 5 Aug 2007 at 2:55 AM
Thanks for the Link but it doen't help me much because it is C.
I have tried to work it out but there also appears to be calls to functions in a library I haven't included.

I do have microsoft.win32.

Any VB code that does the same job as the C and any required libraries would be helpfull.

regards Malcolm Mack

: Ahhh... the heavenly archives of MSDN
:
: http://msdn2.microsoft.com/en-us/library/aa376868.aspx
:
: In particular, check the shutdown constants, and the remarks section.
: Mostly, the AdjustTokenPriveledge part (also:
: http://msdn2.microsoft.com/en-us/library/aa375202.aspx)
:
: EDIT: Complete example in C:
: http://msdn2.microsoft.com/en-us/library/aa376871.aspx
:
: Now note that this code will only work for XP and above.
: If you're also planning on using it for 98 and/or NT/2000 then
: you'll probably have to get a bit móre complicated.
:
: Best Regards,
: Richard
:
: The way I see it... Well, it's all pretty blurry
Report
Re: Shut down the computer Posted by formulav8 on 5 Aug 2007 at 2:30 PM
I have a complete .NET control at my website under the Visual Basic.NET - Controls page that will restart, shutdown, logoff, initiate shutdown, and more that is very easy to use. Simply add a reference to the compiled class library and create a instance and your good to go.

You could also use shell code to manipulate the Windows Shutdown Process to shutdown the computer. Example:

Process.Start("shutdown -s -f -t 0") 'Will Shutdown the computer.

Process.Start("shutdown -r -f -t 0") 'Will Reboot the computer.

Process.Start("shutdown -l -f -t 0") 'Will Logoff the user.


Hope this helps :)


Jason



: Windows XP
: I am trying to shut down the computer after saving files and have
: tried to use this code which was supplied on another thread. The
: code works fine for loging off but I can't get it to shut down the
: computer. Am I missing something?
:
:
: Module Module1
: Declare Function ExitWindowsEx Lib "user32" Alias
: "ExitWindowsEx" (ByVal uFlags As _
: Integer, ByVal dwReserved As Integer) As Integer
:
: Public Enum Choices
: Logoff = 0
: Shutdown = 1
: Reboot = 2
: ForceLogOff = 4
: ForceReboot = 6
: PowerDown = 8
: ForcePowerDown = 12
: End Enum
:
: Public Sub ExitFromWindows(ByVal Choice As Choices)
: ExitWindowsEx(Choice, 0)
: End Sub
:
: End Module
:
: Thanks Malcolm Mack

--------------------------------------------

Visual Basic Code Source (New Design!) - http://www.vbcodesource.org

Visual Basic Code Source - http://www.vbcodesource.com
Report
Re: Shut down the computer Posted by M V Mack on 6 Aug 2007 at 1:33 AM
Thanks Jason that worked just fine.

: I have a complete .NET control at my website under the Visual
: Basic.NET - Controls page that will restart, shutdown, logoff,
: initiate shutdown, and more that is very easy to use. Simply add a
: reference to the compiled class library and create a instance and
: your good to go.
:
: You could also use shell code to manipulate the Windows Shutdown
: Process to shutdown the computer. Example:
:
: Process.Start("shutdown -s -f -t 0") 'Will Shutdown the computer.
:
: Process.Start("shutdown -r -f -t 0") 'Will Reboot the computer.
:
: Process.Start("shutdown -l -f -t 0") 'Will Logoff the user.
:
:
: Hope this helps :)
:
:
: Jason
:
:
:
: : Windows XP
: : I am trying to shut down the computer after saving files and have
: : tried to use this code which was supplied on another thread. The
: : code works fine for loging off but I can't get it to shut down the
: : computer. Am I missing something?
: :
: :
: : Module Module1
: : Declare Function ExitWindowsEx Lib "user32" Alias
: : "ExitWindowsEx" (ByVal uFlags As _
: : Integer, ByVal dwReserved As Integer) As Integer
: :
: : Public Enum Choices
: : Logoff = 0
: : Shutdown = 1
: : Reboot = 2
: : ForceLogOff = 4
: : ForceReboot = 6
: : PowerDown = 8
: : ForcePowerDown = 12
: : End Enum
: :
: : Public Sub ExitFromWindows(ByVal Choice As Choices)
: : ExitWindowsEx(Choice, 0)
: : End Sub
: :
: : End Module
: :
: : Thanks Malcolm Mack
:
: --------------------------------------------
:
: Visual Basic Code Source (New Design!) - http://www.vbcodesource.org
:
: Visual Basic Code Source - http://www.vbcodesource.com
Report
Re: Shut down the computer Posted by BitByBit_Thor on 6 Aug 2007 at 10:05 AM
: I have a complete .NET control at my website under the Visual
: Basic.NET - Controls page that will restart, shutdown, logoff,
: initiate shutdown, and more that is very easy to use. Simply add a
: reference to the compiled class library and create a instance and
: your good to go.

I took a look at the control. Nice code!
I almost had a working code, but the AdjustTokenPrivelege function kept giving me errors and I had not yet succeeded in fixing it.

Best Regards,
Richard

The way I see it... Well, it's all pretty blurry
Report
Re: Shut down the computer Posted by wvbotha on 28 Aug 2007 at 11:03 PM
: : I have a complete .NET control at my website under the Visual
: : Basic.NET - Controls page that will restart, shutdown, logoff,
: : initiate shutdown, and more that is very easy to use. Simply add a
: : reference to the compiled class library and create a instance and
: : your good to go.
:
: I took a look at the control. Nice code!
: I almost had a working code, but the AdjustTokenPrivelege function
: kept giving me errors and I had not yet succeeded in fixing it.
:
: Best Regards,
: Richard
:
: The way I see it... Well, it's all pretty blurry

These Codes will work only on XP.
:
: Process.Start("shutdown -s -f -t 0") 'Will Shutdown the computer.
:
: Process.Start("shutdown -r -f -t 0") 'Will Reboot the computer.
:
: Process.Start("shutdown -l -f -t 0") 'Will Log off the user.
:
if u used this command, your program will restart or do what ever by sending it to DOS Prompt try this in DOS Mode (Windows)
shutdown -s -f -t 0
to understand about is try this on dos prompt.
shutdown /?
--------------------------------------------------------------
if u use
Shutdown -a
shutdown or restart process will abort.

the way you are going to restart, is not the best practice.




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.