473,395 Members | 1,616 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

computer shutdown

hi

does someone knows how to shut down you're compueter with vb 6.0?

thanks Maarten
Jul 17 '05 #1
5 7324
On Wed, 13 Oct 2004 09:56:50 +0200, "Maarten" <gu******@hotmail.com>
you typed some letters in random order:
hi

does someone knows how to shut down you're compueter with vb 6.0?

thanks Maarten

I'm not shure it works for OS > win98

Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As
Long, ByVal dwReserved As Long) As Long

Call ExitWindowsEx(1, 0)

Groetjenz,

Mickey
--
#### gewoan skrieve su ast ut seist ####
Jul 17 '05 #2
On Wed, 13 Oct 2004 23:18:03 +0200, mickey <mickeyATgruizelgruis.nl>
wrote:
On Wed, 13 Oct 2004 09:56:50 +0200, "Maarten" <gu******@hotmail.com>
you typed some letters in random order:
hi

does someone knows how to shut down you're compueter with vb 6.0?

thanks Maarten

I'm not shure it works for OS > win98

Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As
Long, ByVal dwReserved As Long) As Long

Call ExitWindowsEx(1, 0)


For NT systems it's a bit more complex - you have to give the thread
permission to shut down, otherwise all you can do is logoff.

Here's what I use (call quitWindows to use):

' found somewhere on the www (can't remember where)
' so don't give me credit

Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Declare Function OpenProcessToken Lib "advapi32" ( _
ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, _
TokenHandle As Long) As Long
Private Declare Function LookupPrivilegeValue Lib "advapi32" _
Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, _
ByVal lpName As String, lpLuid As LUID) As Long
Private Declare Function AdjustTokenPrivileges Lib "advapi32" ( _
ByVal TokenHandle As Long, ByVal DisableAllPrivileges As Long, _
NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Long, _
PreviousState As TOKEN_PRIVILEGES, ReturnLength As Long) As Long
Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As _
Long, ByVal dwReserved As Long) As Long
Private Declare Function GetVersionEx Lib "kernel32" Alias _
"GetVersionExA" (ByRef lpVersionInformation As OSVERSIONINFO) _
As Long

Public Const EWX_LOGOFF = 0
Public Const EWX_SHUTDOWN = 1
Public Const EWX_REBOOT = 2
Public Const EWX_FORCE = 4
Public Const EWX_POWEROFF = 8
Public Const EWX_FORCEIFHUNG = 16

Private Const TOKEN_QUERY = 8
Private Const TOKEN_ADJUST_PRIVILEGES = 32

Private Const SE_PRIVILEGE_ENABLED = 2

Private Const ANYSIZE_ARRAY = 1
Private Const VER_PLATFORM_WIN32_NT = 2

Type OSVERSIONINFO
dwOSVersionInfoSize As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatformId As Long
szCSDVersion As String * 128
End Type

Public Type LUID
LowPart As Long
HighPart As Long
End Type

Public Type LUID_AND_ATTRIBUTES
pLuid As LUID
Attributes As Long
End Type

Public Type TOKEN_PRIVILEGES
PrivilegeCount As Long
Privileges(ANYSIZE_ARRAY) As LUID_AND_ATTRIBUTES
End Type

Public Sub quitWindows(which As Long)
Dim n As Long

If IsWinNT Then EnableShutDown
n = ExitWindowsEx(which, &HFFFF) '((which Or EWX_FORCE), &HFFFF)
If n Then
Dim x As String * 256
FormatMessage FORMAT_MESSAGE_FROM_SYSTEM, 0, Err.LastDllError, _
0, x, 256, 0
Else
Unload frmBackground
End If
End Sub

Public Function IsWinNT() As Boolean
' Detect if the program is running under Windows NT
Dim myOS As OSVERSIONINFO

myOS.dwOSVersionInfoSize = Len(myOS)
GetVersionEx myOS
IsWinNT = (myOS.dwPlatformId = VER_PLATFORM_WIN32_NT)
End Function

Private Sub EnableShutDown()
' Set the shut down privilege for the current application
Dim hProc As Long, hToken As Long, mLUID As LUID
Dim mPriv As TOKEN_PRIVILEGES, mNewPriv As TOKEN_PRIVILEGES

hProc = GetCurrentProcess()
OpenProcessToken hProc, TOKEN_ADJUST_PRIVILEGES + TOKEN_QUERY, _
hToken
LookupPrivilegeValue "", "SeShutdownPrivilege", mLUID
mPriv.PrivilegeCount = 1
mPriv.Privileges(0).Attributes = SE_PRIVILEGE_ENABLED
mPriv.Privileges(0).pLuid = mLUID

' enable shutdown privilege for the current application
AdjustTokenPrivileges hToken, False, mPriv, 4 + (12 * _
mPriv.PrivilegeCount), mNewPriv, 4 + (12 * _
mNewPriv.PrivilegeCount)
End Sub

--
auric underscore underscore at hotmail dot com
*****
The absence of war does not automatically mean peace.
Jul 17 '05 #3
thanks for the reply

Public Sub quitWindows(which As Long)
Dim n As Long

If IsWinNT Then EnableShutDown
n = ExitWindowsEx(which, &HFFFF) '((which Or EWX_FORCE), &HFFFF)
If n Then
Dim x As String * 256
FormatMessage FORMAT_MESSAGE_FROM_SYSTEM, 0, Err.LastDllError, _
0, x, 256, 0
Else
Unload frmBackground
End If
End Sub

in this part i get ann error

FormatMessage sub or function not derfined?

I'm using a computer with xp to program, but i test and run my software on
a computer with win 98. wil it work for both?

thanks Maarten

"Auric__" <no*********@email.address> wrote in message
news:h5********************************@4ax.com...
On Wed, 13 Oct 2004 23:18:03 +0200, mickey <mickeyATgruizelgruis.nl>
wrote:
On Wed, 13 Oct 2004 09:56:50 +0200, "Maarten" <gu******@hotmail.com>
you typed some letters in random order:
hi

does someone knows how to shut down you're compueter with vb 6.0?

thanks Maarten

I'm not shure it works for OS > win98

Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As
Long, ByVal dwReserved As Long) As Long

Call ExitWindowsEx(1, 0)


For NT systems it's a bit more complex - you have to give the thread
permission to shut down, otherwise all you can do is logoff.

Here's what I use (call quitWindows to use):

' found somewhere on the www (can't remember where)
' so don't give me credit

Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Declare Function OpenProcessToken Lib "advapi32" ( _
ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, _
TokenHandle As Long) As Long
Private Declare Function LookupPrivilegeValue Lib "advapi32" _
Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, _
ByVal lpName As String, lpLuid As LUID) As Long
Private Declare Function AdjustTokenPrivileges Lib "advapi32" ( _
ByVal TokenHandle As Long, ByVal DisableAllPrivileges As Long, _
NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Long, _
PreviousState As TOKEN_PRIVILEGES, ReturnLength As Long) As Long
Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As _
Long, ByVal dwReserved As Long) As Long
Private Declare Function GetVersionEx Lib "kernel32" Alias _
"GetVersionExA" (ByRef lpVersionInformation As OSVERSIONINFO) _
As Long

Public Const EWX_LOGOFF = 0
Public Const EWX_SHUTDOWN = 1
Public Const EWX_REBOOT = 2
Public Const EWX_FORCE = 4
Public Const EWX_POWEROFF = 8
Public Const EWX_FORCEIFHUNG = 16

Private Const TOKEN_QUERY = 8
Private Const TOKEN_ADJUST_PRIVILEGES = 32

Private Const SE_PRIVILEGE_ENABLED = 2

Private Const ANYSIZE_ARRAY = 1
Private Const VER_PLATFORM_WIN32_NT = 2

Type OSVERSIONINFO
dwOSVersionInfoSize As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatformId As Long
szCSDVersion As String * 128
End Type

Public Type LUID
LowPart As Long
HighPart As Long
End Type

Public Type LUID_AND_ATTRIBUTES
pLuid As LUID
Attributes As Long
End Type

Public Type TOKEN_PRIVILEGES
PrivilegeCount As Long
Privileges(ANYSIZE_ARRAY) As LUID_AND_ATTRIBUTES
End Type

Public Sub quitWindows(which As Long)
Dim n As Long

If IsWinNT Then EnableShutDown
n = ExitWindowsEx(which, &HFFFF) '((which Or EWX_FORCE), &HFFFF)
If n Then
Dim x As String * 256
FormatMessage FORMAT_MESSAGE_FROM_SYSTEM, 0, Err.LastDllError, _
0, x, 256, 0
Else
Unload frmBackground
End If
End Sub

Public Function IsWinNT() As Boolean
' Detect if the program is running under Windows NT
Dim myOS As OSVERSIONINFO

myOS.dwOSVersionInfoSize = Len(myOS)
GetVersionEx myOS
IsWinNT = (myOS.dwPlatformId = VER_PLATFORM_WIN32_NT)
End Function

Private Sub EnableShutDown()
' Set the shut down privilege for the current application
Dim hProc As Long, hToken As Long, mLUID As LUID
Dim mPriv As TOKEN_PRIVILEGES, mNewPriv As TOKEN_PRIVILEGES

hProc = GetCurrentProcess()
OpenProcessToken hProc, TOKEN_ADJUST_PRIVILEGES + TOKEN_QUERY, _
hToken
LookupPrivilegeValue "", "SeShutdownPrivilege", mLUID
mPriv.PrivilegeCount = 1
mPriv.Privileges(0).Attributes = SE_PRIVILEGE_ENABLED
mPriv.Privileges(0).pLuid = mLUID

' enable shutdown privilege for the current application
AdjustTokenPrivileges hToken, False, mPriv, 4 + (12 * _
mPriv.PrivilegeCount), mNewPriv, 4 + (12 * _
mNewPriv.PrivilegeCount)
End Sub

--
auric underscore underscore at hotmail dot com
*****
The absence of war does not automatically mean peace.

Jul 17 '05 #4
On Thu, 14 Oct 2004 11:24:19 +0200, "Maarten" <gu******@hotmail.com>
wrote:
thanks for the reply

Public Sub quitWindows(which As Long)
Dim n As Long

If IsWinNT Then EnableShutDown
n = ExitWindowsEx(which, &HFFFF) '((which Or EWX_FORCE), &HFFFF)
If n Then
Dim x As String * 256
FormatMessage FORMAT_MESSAGE_FROM_SYSTEM, 0, Err.LastDllError, _
0, x, 256, 0
Else
Unload frmBackground
End If
End Sub

in this part i get ann error

FormatMessage sub or function not derfined?
Sorry, FormatMessage is another API call; in the project I pulled that
file from I declared it public (I use it more than once) in another
module. I use it specifically to get the error message so I can log it
(if ExitWindowsEx returns a non-zero value, there was an error). You can
safely remove that entire if-then-else structure, or you can add a call
to your logging routine immediately after the call to FormatMessage.
Here's what you need to add to use it:

Public Declare Function FormatMessage Lib "kernel32" Alias _
"FormatMessageA" (ByVal dwFlags As Long, lpSource As Any, _
ByVal dwMessageId As Long, ByVal dwLanguageId As Long, ByVal _
lpBuffer As String, ByVal nSize As Long, Arguments As Long) _
As Long

Public Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
I'm using a computer with xp to program, but i test and run my software on
a computer with win 98. wil it work for both?


That's the whole point of quitWindows - it starts off by finding out
whether or not you're using an NT-based system (using the IsWinNT
function), and if so it kicks up its permissions so it can do the
shutdown. If it's a 9x system, well, the whole concept of thread
security is non-existent there, so *any* process can shutdown Windows
just by making the appropriate API calls.

Another thing that I see I forgot is that the value you pass to
quitWindows (which as long) needs to be one of the EWX_* values:
EWX_LOGOFF
EWX_SHUTDOWN
EWX_REBOOT
EWX_POWEROFF

You can OR them with EWX_FORCE or EWX_FORCEIFHUNG to force closing apps
that don't respond:
quitWindows EWX_REBOOT Or EWX_FORCEIFHUNG

There might also be an EWX_STANDBY, but since I don't use it (and don't
have the API viewer installed) I can't check.
--
auric underscore underscore at hotmail dot com
*****
IRC: real-time Usenet.
Jul 17 '05 #5
very nice thank jou verry much. ik worked fine

Maarten

"Maarten" <gu******@hotmail.com> wrote in message
news:41*********************@news.skynet.be...
hi

does someone knows how to shut down you're compueter with vb 6.0?

thanks Maarten

Jul 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: EW | last post by:
I have a problem when using the python script found here: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/360649 It is a script to remotely shutdown a windows computer. When I use it,...
0
by: Shannon | last post by:
Hey, I need to be able to shutdown a machine before the computer log's on, due to changes that are made to the computer's name and workgroup through a program that runs on startup. Is it...
4
by: Mohammed Abdel-Razzak | last post by:
Dear sirs I want to know how can I shutdown or restart my computer using C# Also I want to know how can I open any windows program using C# (EX: opening the windows calculator from my...
6
by: Herbert VON GRÜNENWALD | last post by:
in c++ it was ExitWindowsEx() thanks
3
by: joja15 | last post by:
I am working on a Python script to perform as a remote computer manager. So far I have a WOL function working and I would like to add the ability to show if a machine is on or off (I figured I...
6
by: Cylix | last post by:
I wrote two application for my company, but I found that, if anyone of application is opened, the user cannot shutdown the computer. the behaviour is that: if one of application is opened, and...
1
by: sankar2 | last post by:
i want programming code for shutdown the system or others system in network it is possible in c# language. plz anyone answer for this query.
0
by: BrianT | last post by:
I'm trying to build code that allows the computer name to be changed, then asks the user to reboot to make the change affective. I got the code working when logged in as the local computer...
2
by: sheperson | last post by:
Hi, Does anyone know how to restart a computer using C# code? I can use shutdown -r command but this is not a good choice because it takes 30 seconds to restart. I have made a wizard for one of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.