The MASM Forum Archive 2004 to 2012

Project Support Forums => GoAsm Assembler and Tools => Topic started by: donkey on December 06, 2006, 04:01:32 AM

Title: System standby
Post by: donkey on December 06, 2006, 04:01:32 AM
I recently needed a little application that would put my system in standby mode, this is how to do it if anyone ever needs to...

LUID STRUCT
LowPart DD
HighPart DD
ENDS

LUID_AND_ATTRIBUTES STRUCT
Luid LUID <>
Attributes DD
ENDS

TOKEN_PRIVILEGES STRUCT
PrivilegeCount DD
Privileges LUID_AND_ATTRIBUTES <>
ENDS

#define TRUE 1
#define FALSE 0
#define NULL 0
// security tokens
#define TOKEN_ADJUST_PRIVILEGES 0x0020
#define TOKEN_QUERY  0x0008
#define SE_PRIVILEGE_ENABLED  0x00000002
#define SE_SHUTDOWN_NAME 0040303Dh

CODE SECTION

invoke EnableTokenPrivilege,SE_SHUTDOWN_NAME
test eax,eax ; check success (TRUE = sucess)
jz >
invoke SetSystemPowerState,TRUE,TRUE
:
invoke ExitProcess,0

EnableTokenPrivilege FRAME PrivilegeName
LOCAL tp :TOKEN_PRIVILEGES
LOCAL luid :LUID
LOCAL hToken :D
LOCAL hProcess :D
LOCAL tpPrevious :TOKEN_PRIVILEGES
LOCAL cbPrevious :D

mov D[cbPrevious], sizeof TOKEN_PRIVILEGES

//
// Get debug privilege
//
invoke GetCurrentProcess
mov [hProcess],eax

invoke OpenProcessToken, [hProcess],TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, offset hToken
test eax,eax
jnz >
xor eax,eax
ret
:

invoke LookupPrivilegeValue, NULL, [PrivilegeName], offset luid
test eax,eax
jnz >
xor eax,eax
ret
:

//
// first pass.  get current privilege setting
//
mov D[tp.PrivilegeCount],1
mov eax,[luid.LowPart]
mov [tp.Privileges.Luid.LowPart],eax
mov eax,[luid.HighPart]
mov [tp.Privileges.Luid.HighPart],eax
mov D[tp.Privileges.Attributes],0

invoke AdjustTokenPrivileges,[hToken],FALSE,offset tp,SIZEOF TOKEN_PRIVILEGES,offset tpPrevious,offset cbPrevious

invoke GetLastError
test eax,eax
jz >
xor eax,eax
ret
:

//
// second pass.  set privilege based on previous setting
//
mov D[tpPrevious.PrivilegeCount],1
mov eax,[luid.LowPart]
mov [tpPrevious.Privileges.Luid.LowPart],eax
mov eax,[luid.HighPart]
mov [tpPrevious.Privileges.Luid.HighPart],eax
mov eax,[tpPrevious.Privileges.Attributes]
or eax,SE_PRIVILEGE_ENABLED
mov [tpPrevious.Privileges.Attributes],eax

invoke AdjustTokenPrivileges,[hToken],FALSE,offset tpPrevious,[cbPrevious],NULL,NULL

invoke GetLastError
test eax,eax
jz >
xor eax,eax
ret
:

xor eax,eax
inc eax
ret
ENDF
Title: Re: System standby
Post by: Biterider on December 10, 2006, 08:58:19 PM
Hi Donkey
Thanks for sharing your code. A couple of weeks ago I wrote something similar based on a code fragment I found on the net.
I compared it with yours and found that you forgot to close the handle. Tha latest MSDN information is clear about this issue.

Regards,

Biterider



[attachment deleted by admin]
Title: Re: System standby
Post by: braymailloux on October 16, 2010, 09:53:37 AM
Where may I find more information about the m2m instruction?
Title: Re: System standby
Post by: donkey on October 16, 2010, 10:42:29 AM
Quote from: braymailloux on October 16, 2010, 09:53:37 AM
Where may I find more information about the m2m instruction?

m2m is a macro that moves a value from one memory location to another. The x86 does not support direct memory to memory moves so generally it uses either a register or the stack to move the data in two instructions:

push [data1]
pop [data2]

Edgar
Title: Re: System standby
Post by: jj2007 on October 16, 2010, 12:25:52 PM
Quote from: braymailloux on October 16, 2010, 09:53:37 AM
Where may I find more information about the m2m instruction?

As Edgar wrote already, it's a macro. You can enable it with include \masm32\include\masm32rt.inc, as shown below.

include \masm32\include\masm32rt.inc

.data
TheSource dd 123
TheDest dd 0
AppName db "The destination changed:", 0

.code
start: m2m TheDest, TheSource
MsgBox 0, str$(TheDest), addr AppName, MB_OK
exit

end start
Title: Re: System standby
Post by: oex on October 16, 2010, 05:27:42 PM
It is actually in \masm32\macros\macros.inc