News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

System standby

Started by donkey, December 06, 2006, 04:01:32 AM

Previous topic - Next topic

donkey

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
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

Biterider

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]

braymailloux

Where may I find more information about the m2m instruction?

donkey

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
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

jj2007

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

oex

It is actually in \masm32\macros\macros.inc
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv