Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi,all.I'm trying to the script initiate a system shutdown on Windows2000,but it can't working.What can I do?Has a good idea for use Win32::API?Thank you very much! Good Look!
########################## #!d:/usr/bin/perl.exe -w use Win32::API; my $op=pack('L',0); my $tr=pack("LL",0,0); my $xx=pack("LLLL",1,unpack("LL",$tr),0x02); my $wa=pack("LLLL",0,0,0,0); my $ui=length($wa); my $sd=pack('L',0); ######## my $gcp=Win32::API->new("kernel32" ,"GetCurrentProcess" ,[] + ,'N'); my $opt=Win32::API->new("advapi32" ,"OpenProcessToken" ,[N,N,N] + ,'N'); my $atp=Win32::API->new("advapi32" ,"AdjustTokenPrivileges" ,[N,N,P,N, +P,N] ,'N'); my $lpv=Win32::API->new("advapi32" ,"LookupPrivilegValueA" ,[P,P,P] + ,'N'); my $ewe=Win32::API->new("user32" ,"ExitWindowsEx" ,[N,N] + ,'N'); ######## my $gh=$gcp->Call(); my $wo=$opt->Call($gh,0x20||0x08,$op); $wo=$lpv->Call('',"SeShutdownPrivilege",$tr); $wo=$atp->Call($op,0,$xx,$ui,$wa,$sd); ######## $ewe->Call(0x02,0);
edited by boo_radley : code tags. Resisted urge to correct "Good Look"

Replies are listed 'Best First'.
Re: Help,ExitWindowsEx problem.
by BrowserUk (Patriarch) on Dec 16, 2002 at 06:55 UTC

    Please make life easy for the editors and read the site how to especially the section on "Submitting Code and Escaping Characters".

    And make life easy for yourself and download Win32::AdminMisc from the website of brother monk dada.

    In particular look at the ExitWindows($Flag);function available in that module.


    Examine what is said, not who speaks.

Re: Help,ExitWindowsEx problem.
by tachyon (Chancellor) on Dec 16, 2002 at 09:24 UTC

    The shutdown.exe widget lets you do this:

    #!/usr/bin/perl `shutdown.exe \\\\some_systems_unc_name`

    There are a range of options to reboot, force closuer of open programs, print a text message set timeout, etc.

    Shutdown.exe is not part of the standard Windows distribution but is widely available on the net for free and as part of the M$ resource kits if you want to pay for it ;-). It is handy in that you can call it from a batch file, schedule with AT etc + perl not required (horrors).

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      Thanks all!but I want a reboot script for use Win32::API on windows2000.Call help!Bottoms up!and vb example under: ###################################################### Reboots a Windows 2000 PC. Many examples shell to the kernel and just kill the PC. This does it properly and takes into account a user privilages. 'API Calls used for RebootPC Private Const TOKEN_ADJUST_PRIVILEGES = &H20 Private Const TOKEN_QUERY = &H8 Private Const SE_PRIVILEGE_ENABLED = &H2 Private Const EWX_SHUTDOWN As Long = 1 Private Const EWX_FORCE As Long = 4 Private Const EWX_REBOOT = 2 Private Type LUID  UsedPart As Long  IgnoredForNowHigh32BitPart As Long End Type Private Type TOKEN_PRIVILEGES  PrivilegeCount As Long  TheLuid As LUID  Attributes As Long End Type Private Declare Function ExitWindowsEx Lib "user32" (ByVal dwOptions As Long, ByVal dwReserved As Long) As Long 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 Sub RebootPC()  On Local Error GoTo RebootPC_ErrorHandler  Const csProcName = "RebootPC"    Dim hProcessHandle As Long  Dim hTokenHandle As Long  Dim tmpLuid As LUID  Dim tkpNew As TOKEN_PRIVILEGES  Dim tkpPrevious As TOKEN_PRIVILEGES  Dim lBufferNeeded As Long  hProcessHandle = GetCurrentProcess()  Call OpenProcessToken(hProcessHandle, TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, hTokenHandle) ' Get the LUID for the shutdown privilege  Call LookupPrivilegeValue("", "SeShutdownPrivilege", tmpLuid)  tkpNew.PrivilegeCount = 1 ' One privilege to set  tkpNew.TheLuid = tmpLuid  tkpNew.Attributes = SE_PRIVILEGE_ENABLED ' Enable the shutdown privilege in the access token of this process.  lBufferNeeded = 0  Call AdjustTokenPrivileges(hTokenHandle, False, tkpNew, Len(tkpPrevious), tkpPrevious, lBufferNeeded) ' Force a Reboot (no option to save files to cancel out)  Call ExitWindowsEx(EWX_FORCE Or EWX_REBOOT, &HFFFF)  Exit Sub RebootPC_ErrorHandler:  Call RaiseError(csModName, csProcName, Err.Number, Err.Description) End Sub ########################################################## Thank you!
        Sorry!Sorry!forget the < CODE >text< /CODE >.Very Sorry!