c# - Windows service: Get username when user log on -


my windows service should save name of user, logon/logoff @ moment. following code works me didn't save username:

protected override void onsessionchange(sessionchangedescription changedescription)     {         try         {             string user = "";              foreach (managementobject currentobject in _wmicomputersystem.getinstances())             {                 user += currentobject.properties["username"].value.tostring().trim();             }              switch (changedescription.reason)             {                 case sessionchangereason.sessionlogon:                     writelog(constants.logtype.continue, "logon - program continues: " + user);                     oncontinue();                     break;                 case sessionchangereason.sessionlogoff:                     writelog(constants.logtype.pause, "logoff - program paused: " + user);                     onpause();                     break;             }             base.onsessionchange(changedescription);         }         catch (exception exp)         {             writelog(constants.logtype.error, "error");         }     } 

edit: foreach loop gives me error:

message: access denied. (exception hresult: 0x80070005 (e_accessdenied)) type: system.unauthorizedaccessexception

but in opinion, code not solution, because saves users, logged onto server.

i ran similar problem while building windows service. you, had session id , needed corresponding username. after several unsuccessful solution hereon so, ran this particular answer , inspired solution:

here's code (all of them residing inside class; in case, class inheriting servicebase).

    [dllimport("wtsapi32.dll")]     private static extern bool wtsquerysessioninformation(intptr hserver, int sessionid, wtsinfoclass wtsinfoclass, out intptr ppbuffer, out int pbytesreturned);     [dllimport("wtsapi32.dll")]     private static extern void wtsfreememory(intptr pointer);      private enum wtsinfoclass     {         wtsusername = 5,          wtsdomainname = 7,     }      private static string getusername(int sessionid, bool prependdomain = true)     {         intptr buffer;         int strlen;         string username = "system";         if (wtsquerysessioninformation(intptr.zero, sessionid, wtsinfoclass.wtsusername, out buffer, out strlen) && strlen > 1)         {             username = marshal.ptrtostringansi(buffer);             wtsfreememory(buffer);             if (prependdomain)             {                 if (wtsquerysessioninformation(intptr.zero, sessionid, wtsinfoclass.wtsdomainname, out buffer, out strlen) && strlen > 1)                 {                     username = marshal.ptrtostringansi(buffer) + "\\" + username;                     wtsfreememory(buffer);                 }             }         }         return username;     } 

with above code in class, can username in method you're overriding calling

string username = getusername(changedescription.sessionid); 

Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -