Shutdown windows from Smalltalk program

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Shutdown windows from Smalltalk program

Louis LaBrunda
Hi,

I'm trying to write some VA Smalltalk to shutdown windows.  Basically I'm trying to write the VA Smalltalk equivalent of the code quoted below.  So far (playing in a workspace) I have:


| op h tkp ptkp |
op := ByteArray new: 4.
PlatformFunctions::OpenProcessToken callWith: OSHandle getCurrentProcess with: (PlatformConstants::TokenAdjustPrivileges | PlatformConstants::TokenAdjustDefault) with: op.
h := OSHandle value: (op uint32At: 0).

tkp := OSTokenPrivileges new.
ptkp := OSTokenPrivileges null.
PlatformFunctions::LookupPrivilegeValue callWith: nil with: PlatformConstants::SeShutdownName with: tkp.

which seems to work until I try to add:

tkp PrivilegeCount: 1.

which crashes everything with: "Exit due to error: 53 - General protection fault - read from invalid memory location".

Does anyone have any idea where I'm going wrong?

Thanks, Lou

BOOL CApp::Shutdown()
{
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
BOOL fResult;

// Get the current process token handle so we can get shutdown privilege.
if(!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
return FALSE;

// Get the LUID for shutdown privilege.
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);

tkp.PrivilegeCount = 1;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

// Get shutdown privilege for this process.
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES) NULL, 0);

// Cannot test the return value of AdjustTokenPrivileges.
if(GetLastError() != ERROR_SUCCESS)
return FALSE;

// Actually shutdown
fResult = InitiateSystemShutdown(NULL, NULL, 0, TRUE, FALSE); // to shutdown
if(!fResult)
return FALSE;

// Disable shutdown privilege.
tkp.Privileges[0].Attributes = 0;

AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES) NULL, 0);
if(GetLastError() != ERROR_SUCCESS)
return FALSE;

return TRUE;
}

--
You received this message because you are subscribed to the Google Groups "VA Smalltalk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/va-smalltalk/-/d0KMPcH8Ls8J.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/va-smalltalk?hl=en.
Reply | Threaded
Open this post in threaded view
|

Re: Shutdown windows from Smalltalk program

Louis LaBrunda
fixing the memory of the privileges structure:

tkp := OSTokenPrivileges new fix.
ptkp := OSTokenPrivileges null fix.

get past that problem.  Now I'm not sure ho to do this:

tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

Lou

--
You received this message because you are subscribed to the Google Groups "VA Smalltalk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/va-smalltalk/-/wNbYvEpFuH0J.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/va-smalltalk?hl=en.
Reply | Threaded
Open this post in threaded view
|

Re: Shutdown windows from Smalltalk program

Louis LaBrunda
Okay, now the code below seems to function without a problem.

| op h tkp ptkp luid |
op := ByteArray new: 4.
OpenProcessToken callWith: OSHandle getCurrentProcess with: (TokenAdjustPrivileges | PlatformConstants::TokenAdjustDefault) with: op.
h := OSHandle value: (op uint32At: 0).

luid := OSLuid new fix.
LookupPrivilegeValue callWith: nil with: SeShutdownName with: luid.

tkp := OSTokenPrivileges new fix.
ptkp := OSTokenPrivileges null fix.
tkp PrivilegeCount: 1.

But when I try to access or set tkp Privileges Luid, I get into trouble.  Any ideas?

Lou

--
You received this message because you are subscribed to the Google Groups "VA Smalltalk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/va-smalltalk/-/ddmpr50QdDsJ.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/va-smalltalk?hl=en.
Reply | Threaded
Open this post in threaded view
|

Re: Shutdown windows from Smalltalk program

wobotzen
Hallo
try
 OSTokenPrivileges  callocVariable: 3
it is a variable struct and Priviliges is in the variable part, donot know how much entries are needed
regards
wolfgang

Am Freitag, 16. November 2012 00:07:01 UTC+1 schrieb Louis LaBrunda:
Okay, now the code below seems to function without a problem.

| op h tkp ptkp luid |
op := ByteArray new: 4.
OpenProcessToken callWith: OSHandle getCurrentProcess with: (TokenAdjustPrivileges | PlatformConstants::TokenAdjustDefault) with: op.
h := OSHandle value: (op uint32At: 0).

luid := OSLuid new fix.
LookupPrivilegeValue callWith: nil with: SeShutdownName with: luid.

tkp := OSTokenPrivileges new fix.
ptkp := OSTokenPrivileges null fix.
tkp PrivilegeCount: 1.

But when I try to access or set tkp Privileges Luid, I get into trouble.  Any ideas?

Lou

--
You received this message because you are subscribed to the Google Groups "VA Smalltalk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/va-smalltalk/-/ywIxDAZ188oJ.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/va-smalltalk?hl=en.
Reply | Threaded
Open this post in threaded view
|

Re: Shutdown windows from Smalltalk program

Louis LaBrunda
Thanks Wolfgang,

That helps, I think.  I am at a point where I make the call to adjustTokenPrivileges: but it still returns false.

I am wondering what to use for the bufferLength value?  Also, OSLuidAndAttributes>>Attributes it is at an offset of 8.  This seems odd to me, shouldn't it be 7, since the offsets start at zero (0)?

Lou

--
You received this message because you are subscribed to the Google Groups "VA Smalltalk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/va-smalltalk/-/pJMd-nzj9eYJ.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/va-smalltalk?hl=en.
Reply | Threaded
Open this post in threaded view
|

Re: Shutdown windows from Smalltalk program

Louis LaBrunda
Hi,

Well I finally have windows shutdown code (see below) working from VA Smalltalk.  Windows is such a pain in the ass or am I expecting too much?  Anyway, it works.  Execute the first line in the comment to add the pool dictionaries to the workspace and the the second line to reset things.

Lou


"
EtWindow defaultEvaluationPools: (EtWindow defaultEvaluationPools asSet addAll: #(#PlatformFunctions #PlatformConstants)) asArray.
EtWindow defaultEvaluationPools: nil.
"

| op h tkp ptkp luid rl |
op := ByteArray new: 4.
OpenProcessToken callWith: OSHandle getCurrentProcess with: (TokenAdjustPrivileges | TokenAdjustDefault | TokenQuery) with: op.
h := OSHandle value: (op uint32At: 0).

luid := OSLuid new fix.
LookupPrivilegeValue callWith: nil with: SeShutdownName with: luid.

tkp := OSTokenPrivileges callocVariable: 3.
ptkp := OSTokenPrivileges callocVariable: 3.
tkp PrivilegeCount: 1.
tkp Privileges Luid: luid.
tkp Privileges Attributes: SePrivilegeEnabled.
"tkp Privileges Attributes inspect."

rl := ByteArray new: 4.
h adjustTokenPrivileges: false newState: tkp bufferLength: 16 previousState: ptkp returnLength: rl.
"OSCall new getLastError inspect."

InitiateSystemShutdown callWith: nil with: 'Shutdown via WorldClock Alarm' with: 30 with: true with: false.

--
You received this message because you are subscribed to the Google Groups "VA Smalltalk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/va-smalltalk/-/TNeWqlGUOrEJ.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/va-smalltalk?hl=en.