2

its very irritating, i found a code sample to get username from stackoverflow on how to get under which username a process running and its working fine in console app but not working in windows service. returnVal is 2 and not showing username and domain. Can anyone tell me do i need to change any setting in windows service.

Community
  • 1
  • 1
Raj
  • 3,890
  • 7
  • 52
  • 80
  • Do you know what value it's supposed to return (Local System, Network Service, a specific account, etc)? – RQDQ Mar 07 '11 at 13:49
  • 2
    http://msdn.microsoft.com/en-us/library/aa390460.aspx value `2` means **Access Denied**! – Jaroslav Jandek Mar 07 '11 at 13:52
  • @RQDQ: it should return "raj", as i said working fine in console based app. – Raj Mar 07 '11 at 13:57
  • @RAJ K - what account is the windows service that is trying to look at the other services running under? – RQDQ Mar 07 '11 at 14:00
  • Do you mean the console app is able to determine the username the console app is running under? And a service isn't able to determine under which account it is running? Or do you mean a console app is able to determine the user for another process, but a service is not able to determine the user for another process? – comecme Mar 07 '11 at 14:25
  • no, i called method from console app, to check explorer.exe and its showing current user but in windows service same code returning access denied error code. – Raj Mar 07 '11 at 14:26

2 Answers2

1

Try running the service under an account that has enough privileges to call GetOwner().

RQDQ
  • 15,461
  • 2
  • 32
  • 59
  • its running under "LocalSystem" account and i think it is the highly privileged account. – Raj Mar 07 '11 at 14:17
  • Try running it under your account and see if that fixes it. – RQDQ Mar 07 '11 at 14:19
  • hi @RQDQ: i am facing new problem now, i configured my service to run under user account but when i install service using setup project it gave me **No mapping between account names and security IDs was done** error during installation. and installation rolls back. username and password is valid & i login using same. – Raj Mar 08 '11 at 05:18
0

I believe that what you're after is simply:

string user = Environment.UserName;

The service itself is running using some system account but you said you're looking for your own account name, meaning the logged in user account.

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
  • I didn't downvote it, but it's kind of obvious you didn't read the question. – RQDQ Mar 07 '11 at 14:31
  • @RQDQ yes I did, from his comments I came to assume he simply doesn't really know what he's after. He's running the service as "LocalSystem" yet expects to get his own account name "raj" as result of the code. How can you explain this conflict in the question? – Shadow The GPT Wizard Mar 07 '11 at 14:34
  • did you look at the sample code he referenced? I agree that the question as worded isn't vague, but the link seems to clarify it. – RQDQ Mar 07 '11 at 14:39
  • @RQD took a glance, not in depth. Like I said the conflict in the question vs. comments made by OP made me to suggest this different approach. Who knows, maybe that's what he's really after. – Shadow The GPT Wizard Mar 07 '11 at 14:43