c# - Delay shutdown and lockscreen -


project:

i devellop application warn user if leaves (lockscreen or shutdown) workplace , has left smartcard in reader.

i able detect through use of winapi (winscard.dll) if smarcard in reader.

problem:

i read (correct me if wrong) not possible application delay lockscreen focus on delaying shutdown.

the problem having need delay ongoing shutdown warn user left smartcard.

i try use shutdownblockreasoncreate delay shutdown @ least 5 seconds windows generously lets me. idea if user removes smartcard application calls shutdownblockreasondestroy continue shutdown.

i implemented 2 methods so:

[dllimport("user32.dll", entrypoint = "shutdownblockreasoncreate",             charset = charset.unicode, setlasterror = true)] internal static extern bool shutdownblockreasoncreate(     [in()]intptr wndhandle,     [in()]string reason);  [dllimport("user32.dll", entrypoint = "shutdownblockreasondestroy",             charset = charset.unicode, setlasterror = true)] internal static extern bool shutdownblockreasondestroy(     [in()]intptr wndhandle); 

furthermore use getlasterror check errors implemented way:

[dllimport("kernel32.dll", entrypoint = "getlasterror",             charset = charset.unicode, setlasterror = true)] internal static extern int getlasterror(); 

now strange thing if create reason

winapimethoden.shutdownblockreasoncreate(       new windowinterophelper(application.current.mainwindow).handle,                                "smartcard still in reader!"); 

and display error

messagebox.show(winapimethoden.getlasterror().tostring("x")); 

it shows 0 stands error_success.

so far seems great. if try shut pc down there no sign whatsoever application has requested pc should not shut down right now.

question:

what doing wrong shutdownblockreasoncreate not work intended?

or there maybe better way prevent user shutting pc down if smartcard still in preventing him initiating shutdown while card in or that?

tl;dr:

i try prevent shutdown while user has smartcard in reader. use shutdownblockreasoncreate doesnt seem work although there no error.

solution:

the accepted answer lead me solution of problem.

you have create cancle reason , subscribe handler systemevents.sessionending. handler has set e.cancel true

void systemevents_sessionending(object sender, sessionendingeventargs e) {     if (mgr.state == smartcardstate.inserted)     {         e.cancel = true;     } } 

to execute programm on shutdown addet shutdown scripts using gpendit.msc. programm gets executed after programs have been terminated. looks weird job.

please take @ these posts situation:

how abort shutdown in windows (xp|vista) programatically?

aborting computer shutdown windows service

prevent windows shutdown cancel option c#

is there way in c# detect windows shutdown/logoff , cancel action (after asking user)


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -