Jump to content
Xtreme .Net Talk

Recommended Posts

  • 3 weeks later...
Posted

is there any way to do this without an API?

 

if you can do everything without API's, then your program will be able to work on linux when they get the .net framework on there, as i understand it...

  • Leaders
Posted

The .NET Framework uses Api's within it's self anyway , they are internal ( inside most of the .net dlls ) , you will have to use Api ( whether it be adding them in to a form , or using the internal ( private ) api's via Type.InvokeMethod.

anyway the Api way you will want to set the Privilages to allow you to shut down the pc ( if running an NT based system , ie: Windows XP ) , try this ...

Private Declare Function GetCurrentProcess Lib "kernel32.dll" () As IntPtr
Private Declare Function OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle As IntPtr, ByVal DesiredAccess As Int32, ByRef TokenHandle As IntPtr) As Int32
Private Declare Function LookupPrivilegeValue Lib "advapi32.dll" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, ByRef lpLuid As LUID) As Int32
Private Declare Function AdjustTokenPrivileges Lib "advapi32.dll" (ByVal TokenHandle As IntPtr, ByVal DisableAllPrivileges As Int32, ByRef NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Int32, ByRef PreviousState As TOKEN_PRIVILEGES, ByRef ReturnLength As Int32) As Int32

Private Declare Function ExitWindowsEx Lib "user32.dll" (ByVal uFlags As Int32, ByVal dwReserved As Int32) As Int32
Private Const EWX_FORCE As Int32 = 4
Private Const EWX_SHUTDOWN As Int32 = 1
Private Const EWX_REBOOT As Int32 = 2
Private Const EWX_LOGOFF As Int32 = 0

Public Structure LUID
	Dim LowPart As Int32
	Dim HighPart As Int32
End Structure

Public Structure TOKEN_PRIVILEGES
	Public PrivilegeCount As Integer
	Public Privileges As LUID
	Public Attributes As Int32
End Structure

Private Sub btn_ShutDown_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_ShutDown.Click
	ShutDown()
End Sub

Private Sub btn_Restart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Restart.Click
	Restart()
End Sub

Private Sub Restart()
	If MessageBox.Show("Would you like to re-boot the system", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = DialogResult.OK Then
		Dim platform As New PlatformID
		Select Case Environment.OSVersion.Platform
			Case PlatformID.Win32NT
				Dim token As TOKEN_PRIVILEGES
				Dim blank_token As TOKEN_PRIVILEGES
				Dim token_handle As IntPtr
				Dim uid As LUID
				Dim ret_length As Integer
				Dim ptr As IntPtr = GetCurrentProcess() '/// get the process handle

				OpenProcessToken(ptr, &H20 Or &H8, token_handle)
				LookupPrivilegeValue("", "SeShutdownPrivilege", uid)
				token.PrivilegeCount = 1
				token.Privileges = uid
				token.Attributes = &H2

				AdjustTokenPrivileges(token_handle, False, token, System.Runtime.InteropServices.Marshal.SizeOf(blan  k_token), blank_token, ret_length)

				ExitWindowsEx(EWX_LOGOFF Or EWX_FORCE Or EWX_REBOOT, &HFFFF)

			Case Else
				ExitWindowsEx(EWX_SHUTDOWN Or EWX_FORCE Or EWX_REBOOT, &HFFFF)
		End Select
	End If
End Sub
Private Sub ShutDown()
	If MessageBox.Show("Would you like to shut down the system", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = DialogResult.OK Then
		Dim platform As New PlatformID
		Select Case Environment.OSVersion.Platform
			Case PlatformID.Win32NT
				Dim token As TOKEN_PRIVILEGES
				Dim blank_token As TOKEN_PRIVILEGES
				Dim token_handle As IntPtr
				Dim uid As LUID
				Dim ret_length As Integer
				Dim ptr As IntPtr = GetCurrentProcess() '/// get the process handle

				OpenProcessToken(ptr, &H20 Or &H8, token_handle)
				LookupPrivilegeValue("", "SeShutdownPrivilege", uid)
				token.PrivilegeCount = 1
				token.Privileges = uid
				token.Attributes = &H2

				AdjustTokenPrivileges(token_handle, 0, token, System.Runtime.InteropServices.Marshal.SizeOf(blan  k_token), blank_token, ret_length)

				ExitWindowsEx(EWX_SHUTDOWN Or EWX_FORCE, &HFFFF)

			Case Else
				ExitWindowsEx(EWX_SHUTDOWN Or EWX_FORCE, &HFFFF)
		End Select
	End If
End Sub

  • 4 months later...
Posted

Or just do this:

 

Sub ShutDown()

       Dim nLogOff = 0
       Dim nReboot = 2
       Dim nForceLogOff = 4
       Dim nForceReboot = 6
       Dim nPowerDown = 8
       Dim nForcePowerDown = 12

       Dim oOS As Object = GetObject("winmgmts:{(Shutdown)}").ExecQuery("Select * from Win32_OperatingSystem")
       Dim oOperatingSystem As Object

       For Each oOperatingSystem In oOS
           oOperatingSystem.Win32Shutdown(nForcePowerDown)
       Next

   End Sub

Read the Fovean Chronicles

Because you just can't spend your whole day programming!

Posted
Yes, it's on by default in 2000 and XP, it wasn't present until 98 SE if I know the history right. NT 4 it was there, but many components of it were missing. Some places that I have worked have it turned off because of security reasons, but that's way beyond any at home users, you shouldn't need to worry really.
Posted
Don't know if anyone's still interested in this thread but you can use Process.Start to initiate a shutdown. Sounds a bit like using the Start menu to shutdown doesn't it. There is an executable file in the system32 folder named shutdown.exe. It takes various command line arguments that enable it to do various things. I have three shortcuts on my desktop that use it that allow me to log off, restart or shut down with one click. Use the command prompt to get all the details about the arguments and use Process.Start("shutdown", <arguments>) to initiate it.
  • Leaders
Posted
Don't know if anyone's still interested in this thread but you can use Process.Start to initiate a shutdown. Sounds a bit like using the Start menu to shutdown doesn't it. There is an executable file in the system32 folder named shutdown.exe. It takes various command line arguments that enable it to do various things. I have three shortcuts on my desktop that use it that allow me to log off' date=' restart or shut down with one click. Use the command prompt to get all the details about the arguments and use Process.Start("shutdown", <arguments>) to initiate it.[/quote']

Interesting, but it is not available in all versions of windows, and just like the ExitWindowsEx API, it is almost guarunteed to not work in Mono.

[sIGPIC]e[/sIGPIC]
Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...