.net - How do I lock the workstation from a windows service? -


i need lock workstation windows service written in vb.net. writing app on windows 7 needs work under vista , xp well.

user32 api lockworkstation not work requires interactive desktop , return value of 0.

i tried calling %windir%\system32\rundll32.exe user32.dll,lockworkstation both process , shell, still nothing happens.

setting service interact desktop no-go running service under admin account can other stuff requires admin rights - disabling network, , can select interact desktop option if running under local system account.

that secondary question - how run app admin rights service running under local system account without bugging user.

i writing app control kids computer/internet access (which plan open source when done) need happen stealthily possible.

i have ui handles settings , status notifications in taskbar, easy kill , defeat locking. make hidden windows forms app handle locking, seems rather inelegant solution.

better ideas anyone?

i've been fighting on week , after reading lot of information issue, got solution this...

you have use createprocessasuser function this:

  private shared sub executer(byval content string)     dim objprocess system.diagnostics.process      dim filename string     filename = "e:\lock.bat"      'create bat file ''rundll32.exe user32.dll,lockworkstation'' inside      dim usertokenhandle intptr = intptr.zero     windowsapi.wtsqueryusertoken(windowsapi.wtsgetactiveconsolesessionid, usertokenhandle)      dim procinfo new windowsapi.process_information     dim startinfo new windowsapi.startupinfow     startinfo.cb = cuint(marshal.sizeof(startinfo))      windowsapi.createprocessasuser(usertokenhandle, filename, intptr.zero, intptr.zero, intptr.zero, false, 0, intptr.zero, nothing, startinfo, procinfo)     if not usertokenhandle = intptr.zero         windowsapi.closehandle(usertokenhandle)     end if  end sub 

got of code here can, also, find windowsapi use function. i'me still trying find if can avoid bat file @ least decent solution.

edit: avoid using external *.bat file execute code edit windowsapi class , replace createprocessasuser , advapi32.dll import part this:

    <dllimport("advapi32.dll", entrypoint:="createprocessasuser", exactspelling:=false,      setlasterror:=true, charset:=charset.unicode)> _     public shared function createprocessasuser( _                        byval htoken intptr, _                        byval lpapplicationname string, _                        <[in](), out(), [optional]()> byval lpcommandline stringbuilder, _                        byval lpprocessattributes intptr, _                        byval lpthreadattributes intptr, _                        <marshalas(unmanagedtype.bool)> byval binherithandles boolean, _                        byval dwcreationflags integer, _                        byval lpenvironment intptr, _                        byval lpcurrentdirectory string, _                        <[in]()> byref lpstartupinfo startupinfow, _                        <out()> byref lpprocessinformation process_information) <marshalas(unmanagedtype.bool)> boolean     end function 

and can use stringbuilder thrid argument(comandline) createprocessasuser function , put second(applicationame) 'nothing' this:

dim cmdline new stringbuilder cmdline.append("rundll32.exe user32.dll,lockworkstation") windowsapi.createprocessasuser(usertokenhandle, nothing, cmdline, intptr.zero, intptr.zero, false, 0, intptr.zero, nothing, startinfo, procinfo) 

and work!!!!

regards, ap


Comments