Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Shutdown / reboot windows with VB.NET

Posted on 2004-04-13
10
Medium Priority
?
559 Views
Last Modified: 2011-08-18
Hi.
I am using VB.NET not VB6 !
---------------------------------
How do you shutdown or reboot Windows 2000 server, XP server 2003 server with Visual Basic .NET?

Ideally I need the following options:
- EWX_LogOff
- EWX_SHUTDOWN
- EWX_REBOOT
- EWX_FORCE
- EWX_POWEROFF

I need a working example please not just a description.

500 points.
Many thanks
0
Comment
Question by:gjok
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 22

Accepted Solution

by:
_TAD_ earned 500 total points
ID: 10816672


You'll need to use WinAPI for that, .Net does not handle that natively.

Here's a site that will help you:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winprog/winprog/functions_by_category.asp


You'll probably want to use the Advapi32.lib:

BOOL InitiateSystemShutdownEx(
  LPTSTR lpMachineName,
  LPTSTR lpMessage,
  DWORD dwTimeout,
  BOOL bForceAppsClosed,
  BOOL bRebootAfterShutdown,
  DWORD dwReason
);


0
 
LVL 22

Expert Comment

by:_TAD_
ID: 10816677

As for a sample... I've done this a few times in VB6 using the old method, I haven't actually tried it with the advanced libraries yet.  I'll have to get back to you on that one.
0
 
LVL 9

Assisted Solution

by:glsac
glsac earned 500 total points
ID: 10816687
Yup, no way in the framework...

http://www.mentalis.org/soft/class.qpx?id=7

Which seems to be down now has a class in .NET to do this using API...but again the site is down now.
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 2

Author Comment

by:gjok
ID: 10816817
Thanks _TAD_ ,glsac

glsac, I had previously downloaded this, but its just a couple of .vb files with no .sln file. How do I try it out. Also, is there a DLL or something to make the code work??

(I havn't quite got accustommed to .NET yet - still stuck in VB6 land)
gjok.
0
 
LVL 2

Author Comment

by:gjok
ID: 10817133
(Sorry for the above stupid comment. I see how it works now.)
0
 
LVL 28

Assisted Solution

by:iboutchkine
iboutchkine earned 500 total points
ID: 10817347
to shut down windows xp use shell "shutdown.exe -s" (shutdown) shell "shutown.exe -r" (reboot)
0
 
LVL 10

Assisted Solution

by:heintalus
heintalus earned 500 total points
ID: 10819448
Try this

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

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

    Public Sub ExitWindows(ByVal flag As Flags)
        ExitWindowsEx(flag, 0)
    End Sub

then where you want to call the exit windows function just call ExitWindows() with the appropriate flag for example to logoff you'd use
ExitWindows(Flags.Logoff)

HTH
Andy
0
 
LVL 9

Expert Comment

by:glsac
ID: 11086348
glsac, I had previously downloaded this, but its just a couple of .vb files with no .sln file. How do I try it out. Also, is there a DLL or something to make the code work??

(I havn't quite got accustommed to .NET yet - still stuck in VB6 land)
gjok.

 
Comment from gjok
Date: 04/13/2004 01:21PM PDT
 Author Comment  


(Sorry for the above stupid comment. I see how it works now.)
-------------------------------------
No matter...but I do think he got it to work with my link to a .net class file :)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

876 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