I want to expand user specific environment variables. I have API for that "ExpandEnvironmentStringsForUser". My code is running in context of service. I want to fetch currently logged in user. Whenever I use GetUserName API it returns "SYSTEM".
My problem is I want to expand %temp% in user specific mode and not in system mode. Is there any way to get currently logged in user when my code is running in service context?
Best How To :
If your program is running as a service, then there isn't "a logged-in user". The whole notion of multi-user systems makes such a concept meaningless. There could be any number of users logged in, and it is impossible for your program to guess which one you wanted it to pretend to be running under.
Your question is like asking "of all my family members, in whose bedroom is my car currently parked?" when in fact the car is safely and sanely sat outside in the driveway.
You can use the
qwinsta command (part of Terminal Services) to obtain a list of currently logged-in users, and do something with that; it'll have absolutely nothing to do with your service, but on some Windows systems that allow only one interactive session at a time (for licencing reasons), it'll be the only one marked
SESSIONNAME USERNAME ID STATE TYPE DEVICE
services 0 Disc
>console tomalak 1 Active
That's a little hacky, though; typically you would have a little user-space application that can talk to the back-end service, and do all the user-specific shenanigans in the application.