This is supposed to reboot and it does but I have to manually cycle
my monitor power button to get my screen showing.
My help file said I needed a combination of the 2 flags. Guess I picked
the wrong two.

Thanks.

    .code

start:

        invoke ExitWindowsEx,1 or EWX_REBOOT,2
        invoke ExitProcess,eax

end start

        ; Flags for ExitWindowEx

        ; EWX_LOGOFF    equ 0
        ; EWX_SHUTDOWN  equ 1
        ; EWX_REBOOT    equ 2
        ; EWX_FORCE    equ 4
        ; EWX_POWEROFF  equ 8
Posted on 2005-08-27 14:03:38 by skywalker
I guess, 1 or EWX_REBOOT would tell it to: Shutdown (1) and Reboot (2) at the same time. I also don't understand the second param to be (2) as none of the Shutdown Reason codes have as value 2.

If all you want to do is Reboot, try the following: invoke ExitWindowEx, EWX_REBOOT, NULL
Posted on 2005-08-27 17:08:35 by JimmyClif
Shutdown reason #2 is...

#define SHTDN_REASON_MINOR_INSTALLATION  00000002h
Posted on 2005-08-27 19:18:59 by donkey
...and if you want to reboot a NT system, you'll need some additional code to get the proper access token.
Posted on 2005-08-27 19:33:35 by f0dder

I guess, 1 or EWX_REBOOT would tell it to: Shutdown (1) and Reboot (2) at the same time. I also don't understand the second param to be (2) as none of the Shutdown Reason codes have as value 2.

If all you want to do is Reboot, try the following: invoke ExitWindowEx, EWX_REBOOT, NULL


I got an assembler error using that. Here is what win32.hlp says:

(I like the "some combination" part. Like going in to a restaurant and saying "Surprise me" when they ask what you want to order. ?)

Parameters

uFlags

Specifies the type of shutdown. This parameter must be some combination of the following values:

Value Meaning
EWX_FORCE Forces processes to terminate. When this flag is set, Windows does not send the messages WM_QUERYENDSESSION and WM_ENDSESSION to the applications currently running in the system. This can cause the applications to lose data. Therefore, you should only use this flag in an emergency.

EWX_LOGOFF Shuts down all processes running in the security context of the process that called the ExitWindowsEx function. Then it logs the user off.

EWX_POWEROFF Shuts down the system and turns off the power. The system must support the power-off feature.Windows NT: The calling process must have the SE_SHUTDOWN_NAME privilege. For more information, see the following Remarks section. Windows 95: Security privileges are not supported or required.

EWX_REBOOT Shuts down the system and then restarts the system. Windows NT: The calling process must have the SE_SHUTDOWN_NAME privilege. For more information, see the following Remarks section. Windows 95: Security privileges are not supported or required.

EWX_SHUTDOWN Shuts down the system to a point at which it is safe to turn off the power. All file buffers have been flushed to disk, and all running processes have stopped. Windows NT: The calling process must have the SE_SHUTDOWN_NAME privilege. For more information, see the following Remarks section. Windows 95: Security privileges are not supported or required.
dwReserved

Reserved; this parameter is ignored.

Return Values

If the function succeeds, the return value is nonzero.
Posted on 2005-08-27 21:42:49 by skywalker

Shutdown reason #2 is...

#define SHTDN_REASON_MINOR_INSTALLATION  00000002h


You have me totally lost with this.
My code was a modification of the FastExit program in Hutch's Masm package.

Posted on 2005-08-27 21:44:28 by skywalker

...and if you want to reboot a NT system, you'll need some additional code to get the proper access token.



See reply to Donkey.

Posted on 2005-08-27 21:46:01 by skywalker
You must enable SE_SHUTDOWN_NAME privilege before the call will work.

The last parameter is used for the reason the system is shutting down, anyway it is ignored below Windows XP so just use 2. But there are quite a few different reason codes...

#define SHTDN_REASON_FLAG_COMMENT_REQUIRED  01000000h
#define SHTDN_REASON_FLAG_DIRTY_PROBLEM_ID_REQUIRED  02000000h
#define SHTDN_REASON_FLAG_CLEAN_UI  04000000h
#define SHTDN_REASON_FLAG_DIRTY_UI  08000000h

#define SHTDN_REASON_FLAG_USER_DEFINED  40000000h
#define SHTDN_REASON_FLAG_PLANNED  80000000h

#define SHTDN_REASON_MAJOR_OTHER  00000000h
#define SHTDN_REASON_MAJOR_NONE  00000000h
#define SHTDN_REASON_MAJOR_HARDWARE  00010000h
#define SHTDN_REASON_MAJOR_OPERATINGSYSTEM  00020000h
#define SHTDN_REASON_MAJOR_SOFTWARE  00030000h
#define SHTDN_REASON_MAJOR_APPLICATION  00040000h
#define SHTDN_REASON_MAJOR_SYSTEM  00050000h
#define SHTDN_REASON_MAJOR_POWER  00060000h

#define SHTDN_REASON_MINOR_OTHER  00000000h
#define SHTDN_REASON_MINOR_NONE  000000ffh
#define SHTDN_REASON_MINOR_MAINTENANCE  00000001h
#define SHTDN_REASON_MINOR_INSTALLATION  00000002h
#define SHTDN_REASON_MINOR_UPGRADE  00000003h
#define SHTDN_REASON_MINOR_RECONFIG  00000004h
#define SHTDN_REASON_MINOR_HUNG  00000005h
#define SHTDN_REASON_MINOR_UNSTABLE  00000006h
#define SHTDN_REASON_MINOR_DISK  00000007h
#define SHTDN_REASON_MINOR_PROCESSOR  00000008h
#define SHTDN_REASON_MINOR_NETWORKCARD  00000009h
#define SHTDN_REASON_MINOR_POWER_SUPPLY  0000000ah
#define SHTDN_REASON_MINOR_CORDUNPLUGGED  0000000bh
#define SHTDN_REASON_MINOR_ENVIRONMENT  0000000ch
#define SHTDN_REASON_MINOR_HARDWARE_DRIVER  0000000dh
#define SHTDN_REASON_MINOR_OTHERDRIVER  0000000eh
#define SHTDN_REASON_MINOR_BLUESCREEN  0000000Fh

#define SHTDN_REASON_UNKNOWN  SHTDN_REASON_MINOR_NONE

#define SHTDN_REASON_VALID_BIT_MASK  c0ffffffh

Posted on 2005-08-28 12:07:50 by donkey
Here's an example of a properly written system shutdown proc. It is written in GoAsm but should be easy to translate. I am not sure which one you were looking at but if it does not adjust the priviledge token it will not work for NT based versions of Windows...


LUID STRUCT
  LowPart  DD
  HighPart  DD
LUID ENDS

LUID_AND_ATTRIBUTES STRUCT
    Luid LUID        <0>
    Attributes dd
LUID_AND_ATTRIBUTES ENDS

TOKEN_PRIVILEGES STRUCT
  PrivilegeCount    DD
  Privileges        LUID_AND_ATTRIBUTES <0>
TOKEN_PRIVILEGES ENDS

CODE SECTION

SystemShutdown FRAME
LOCAL hToken :D
LOCAL hProcess :D
LOCAL tkp :TOKEN_PRIVILEGES

// Get the token for this process
invoke GetCurrentProcess
mov , eax
invoke OpenProcessToken, , TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, offset hToken

lea eax,tkp.Privileges.Luid
invoke LookupPrivilegeValue,NULL,SE_SHUTDOWN_NAME,eax
mov D,1
mov D,SE_PRIVILEGE_ENABLED
invoke AdjustTokenPrivileges,, FALSE,offset tkp,0,0,0
invoke GetLastError
test eax,eax
jnz >.ERROR
invoke ExitWindowsEx,EWX_SHUTDOWN + EWX_FORCE, \
SHTDN_REASON_MAJOR_OPERATINGSYSTEM  | SHTDN_REASON_MINOR_UPGRADE | SHTDN_REASON_FLAG_PLANNED

RET
.ERROR
invoke MessageBox,0,"Error adjusting priviledge",0,0
RET
ENDF
Posted on 2005-08-28 12:26:40 by donkey
Nice stuff, Edgar... I found the same MSDN page and started translating it... I was just about to scratch my head on the  &tkp.Privileges[0].Luid part :)
Posted on 2005-08-28 12:31:01 by JimmyClif

Nice stuff, Edgar... I found the same MSDN page and started translating it... I was just about to scratch my head on the  &tkp.Privileges[0].Luid part :)


My motto is "if in doubt check MSDN, if there is no doubt, check it anyway" :)
Posted on 2005-08-28 12:36:42 by donkey
I liked this enough to add it to my system.lib library. The lib based function allows you to specify Win9x and NT as well as type and reason parameters. It will not break a 9x app if the ADVAPI.DLL is not present so it should be safe for all systems:

SystemShutdown
This will perform a shutdown of the system. It is good for both 9x and NT based
versions of Windows.

Parameters:
f9XSystem = Set to TRUE if this is a 9x system (FALSE for NT systems)
Type = The type of shutdown, use the EWX_xxxxx constants
Reason = A reason code for the exit

For more information on type and reason codes see the ExitWindowsEx function
in the Platform SDK

Returns -1 if there is an error, 0 if successful
Posted on 2005-08-28 14:16:29 by donkey

Here's an example of a properly written system shutdown proc. It is written in GoAsm but should be easy to translate. I am not sure which one you were looking at but if it does not adjust the priviledge token it will not work for NT based versions of Windows...


Thanks for your patience. I am not familiar with GoAsm. The FastExit.asm in Hutch's package shuts down the computer. Good short code that works on 98 and presumably would also work on 95 as well. I am looking to modify it or start from scratch if I have to and make one that just reboots a WIN 98 system.

As you can see from the previous info I posted from the Win32.hlp, it's instructions on the flags are clear as mud.
It gives EWX_REBOOT as one flag so I assume that rebooting is available.

Thanks,
            Andy
Posted on 2005-08-28 15:52:25 by skywalker
skywalker, place your own text *after* the /QUOTE tag :)

Have a look at the fastexit.asm file then - if it basically only includes the ShutdownWindows call and no mention of tokens, it won't be enough to work on a NT system. Basically, stay away from the m32lib - many of the routines are flawed.
Posted on 2005-08-28 16:39:32 by f0dder
Hi f0dder, this is the code from the MASM32 examples...


WndProc proc hWin  :DWORD,
            uMsg  :DWORD,
            wParam :DWORD,
            lParam :DWORD

      LOCAL Ps :PAINTSTRUCT

      .if uMsg == WM_INITDIALOG

        szText dlgTitle," Fast Shutdown"
        invoke SendMessage,hWin,WM_SETTEXT,0,ADDR dlgTitle

        invoke LoadIcon,hInstance,200
        mov hIcon, eax

        invoke SendMessage,hWin,WM_SETICON,1,hIcon

        invoke GetDlgItem,hWin,IDOK
        mov hButn1, eax

        invoke GetDlgItem,hWin,IDCANCEL
        mov hButn2, eax

        invoke GetDlgItem,hWin,3000
        mov hIwin, eax


        xor eax, eax
        ret

      .elseif uMsg == WM_COMMAND
        .if wParam == IDOK
          invoke ExitWindowsEx,EWX_SHUTDOWN or EWX_FORCE,0

          ; EWX_LOGOFF    equ 0
          ; EWX_SHUTDOWN  equ 1
          ; EWX_REBOOT    equ 2
          ; EWX_FORCE    equ 4
          ; EWX_POWEROFF  equ 8

        .elseif wParam == IDCANCEL
          jmp outa_here
        .endif

      .elseif uMsg == WM_CLOSE
        outa_here:
        invoke EndDialog,hWin,0

      .elseif uMsg == WM_PAINT
        invoke BeginPaint,hWin,ADDR Ps
      ; ----------------------------------------
      ; The following function are in MASM32.LIB
      ; ----------------------------------------
        invoke FrameGrp,hButn1,hButn2,6,1,0
        invoke FrameGrp,hIwin,hIwin,6,1,0
        invoke FrameGrp,hIwin,hIwin,3,1,1
        invoke FrameWindow,hWin,2,1,1
        invoke EndPaint,hWin,ADDR Ps
        xor eax, eax
        ret

      .endif

    xor eax, eax    ; this must be here in NT4
    ret

WndProc endp


Obviously it will not function except in Windows 9x (though it does mention NT4 for some reason) But it is more than adequate as an example of shutting down the system under the older 9x OS versions.
Posted on 2005-08-28 17:05:27 by donkey
Hi Skywalker,

You can use my lib based code within MASM as well, the main file contains the PROTO's you require and you just use IncludeLib to gain access to the functions. This will allow you to easily update the program to NT based systems (ie Windows 2K/XP) when needed. However, if you never plan on taking the application to another platform, the MASM32 example is fine.

SystemShutdown	PROTO	:DWORD,:DWORD,:DWORD
includelib  System.lib

.CODE
bswap ebx
test bl,bl
setz BYTE PTR f9xSystem

invoke SystemShutdown, f9xSystem, EWX_SHUTDOWN + EWX_FORCE,
SHTDN_REASON_MAJOR_OPERATINGSYSTEM + SHTDN_REASON_MINOR_UPGRADE + SHTDN_REASON_FLAG_PLANNED
Posted on 2005-08-28 17:17:40 by donkey

skywalker, place your own text *after* the /QUOTE tag :)

Have a look at the fastexit.asm file then - if it basically only includes the ShutdownWindows call and no mention of tokens, it won't be enough to work on a NT system. Basically, stay away from the m32lib - many of the routines are flawed.



You guys must be more tired than myself. In my last post I said I needed it for WIN 98, NOT AN NT SYSTEM.
:-)

Posted on 2005-08-28 20:27:09 by skywalker

You guys must be more tired than myself. In my last post I said I needed it for WIN 98, NOT AN NT SYSTEM.

Hehe, well, unless you only ever intend the app to run on your own box, doing the extra few lines for NT support is worth it.
Posted on 2005-08-28 21:57:14 by f0dder