Win API GetUserName returns SYSTEM [duplicate]
This question already has an answer here:
How can I get the active user name in a SYSTEM process using C++?
4 answers
I have this method for getting user name of current logged in user, It works fine when I run my application as console application, But when I run it as a windows service, It gives me SYSTEM
as user name! Any Idea to achieve my desired behavior even if I run my application as windows service?
bool GetCurrentUserName(std::wstring& userName)
{
const int INFO_BUFFER_SIZE = 32767;
TCHAR infoBuf[INFO_BUFFER_SIZE] = L"";
DWORD bufCharCount = INFO_BUFFER_SIZE;
if (GetUserName(infoBuf, &bufCharCount))
{
userName = std::wstring(infoBuf);
return true;
}
return false;
}
c++ winapi service
marked as duplicate by Remy Lebeau
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 10:44
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
|
show 3 more comments
This question already has an answer here:
How can I get the active user name in a SYSTEM process using C++?
4 answers
I have this method for getting user name of current logged in user, It works fine when I run my application as console application, But when I run it as a windows service, It gives me SYSTEM
as user name! Any Idea to achieve my desired behavior even if I run my application as windows service?
bool GetCurrentUserName(std::wstring& userName)
{
const int INFO_BUFFER_SIZE = 32767;
TCHAR infoBuf[INFO_BUFFER_SIZE] = L"";
DWORD bufCharCount = INFO_BUFFER_SIZE;
if (GetUserName(infoBuf, &bufCharCount))
{
userName = std::wstring(infoBuf);
return true;
}
return false;
}
c++ winapi service
marked as duplicate by Remy Lebeau
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 10:44
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
That is the standard and well documented behavior of services, by default they all run with theSYSTEM
user.
– Some programmer dude
Nov 23 '18 at 7:32
Now, what is your real problem? Why do you need your service to run as another user? What is the actual problem that's supposed to solve?
– Some programmer dude
Nov 23 '18 at 7:34
1
what if multiple users logged in ?
– RbMm
Nov 23 '18 at 7:39
1
You are going to need to about Windows user names and the fact that processes, including services, can run under different users.
– David Heffernan
Nov 23 '18 at 7:40
1
Note, the accepted answer of the linked duplicate is not a good approach. Use one of the other answers that utilizeWTSQuerySessionInformation()
instead.
– Remy Lebeau
Nov 23 '18 at 10:47
|
show 3 more comments
This question already has an answer here:
How can I get the active user name in a SYSTEM process using C++?
4 answers
I have this method for getting user name of current logged in user, It works fine when I run my application as console application, But when I run it as a windows service, It gives me SYSTEM
as user name! Any Idea to achieve my desired behavior even if I run my application as windows service?
bool GetCurrentUserName(std::wstring& userName)
{
const int INFO_BUFFER_SIZE = 32767;
TCHAR infoBuf[INFO_BUFFER_SIZE] = L"";
DWORD bufCharCount = INFO_BUFFER_SIZE;
if (GetUserName(infoBuf, &bufCharCount))
{
userName = std::wstring(infoBuf);
return true;
}
return false;
}
c++ winapi service
This question already has an answer here:
How can I get the active user name in a SYSTEM process using C++?
4 answers
I have this method for getting user name of current logged in user, It works fine when I run my application as console application, But when I run it as a windows service, It gives me SYSTEM
as user name! Any Idea to achieve my desired behavior even if I run my application as windows service?
bool GetCurrentUserName(std::wstring& userName)
{
const int INFO_BUFFER_SIZE = 32767;
TCHAR infoBuf[INFO_BUFFER_SIZE] = L"";
DWORD bufCharCount = INFO_BUFFER_SIZE;
if (GetUserName(infoBuf, &bufCharCount))
{
userName = std::wstring(infoBuf);
return true;
}
return false;
}
This question already has an answer here:
How can I get the active user name in a SYSTEM process using C++?
4 answers
c++ winapi service
c++ winapi service
asked Nov 23 '18 at 7:29
rm-rf
2381732
2381732
marked as duplicate by Remy Lebeau
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 10:44
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Remy Lebeau
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 10:44
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
That is the standard and well documented behavior of services, by default they all run with theSYSTEM
user.
– Some programmer dude
Nov 23 '18 at 7:32
Now, what is your real problem? Why do you need your service to run as another user? What is the actual problem that's supposed to solve?
– Some programmer dude
Nov 23 '18 at 7:34
1
what if multiple users logged in ?
– RbMm
Nov 23 '18 at 7:39
1
You are going to need to about Windows user names and the fact that processes, including services, can run under different users.
– David Heffernan
Nov 23 '18 at 7:40
1
Note, the accepted answer of the linked duplicate is not a good approach. Use one of the other answers that utilizeWTSQuerySessionInformation()
instead.
– Remy Lebeau
Nov 23 '18 at 10:47
|
show 3 more comments
1
That is the standard and well documented behavior of services, by default they all run with theSYSTEM
user.
– Some programmer dude
Nov 23 '18 at 7:32
Now, what is your real problem? Why do you need your service to run as another user? What is the actual problem that's supposed to solve?
– Some programmer dude
Nov 23 '18 at 7:34
1
what if multiple users logged in ?
– RbMm
Nov 23 '18 at 7:39
1
You are going to need to about Windows user names and the fact that processes, including services, can run under different users.
– David Heffernan
Nov 23 '18 at 7:40
1
Note, the accepted answer of the linked duplicate is not a good approach. Use one of the other answers that utilizeWTSQuerySessionInformation()
instead.
– Remy Lebeau
Nov 23 '18 at 10:47
1
1
That is the standard and well documented behavior of services, by default they all run with the
SYSTEM
user.– Some programmer dude
Nov 23 '18 at 7:32
That is the standard and well documented behavior of services, by default they all run with the
SYSTEM
user.– Some programmer dude
Nov 23 '18 at 7:32
Now, what is your real problem? Why do you need your service to run as another user? What is the actual problem that's supposed to solve?
– Some programmer dude
Nov 23 '18 at 7:34
Now, what is your real problem? Why do you need your service to run as another user? What is the actual problem that's supposed to solve?
– Some programmer dude
Nov 23 '18 at 7:34
1
1
what if multiple users logged in ?
– RbMm
Nov 23 '18 at 7:39
what if multiple users logged in ?
– RbMm
Nov 23 '18 at 7:39
1
1
You are going to need to about Windows user names and the fact that processes, including services, can run under different users.
– David Heffernan
Nov 23 '18 at 7:40
You are going to need to about Windows user names and the fact that processes, including services, can run under different users.
– David Heffernan
Nov 23 '18 at 7:40
1
1
Note, the accepted answer of the linked duplicate is not a good approach. Use one of the other answers that utilize
WTSQuerySessionInformation()
instead.– Remy Lebeau
Nov 23 '18 at 10:47
Note, the accepted answer of the linked duplicate is not a good approach. Use one of the other answers that utilize
WTSQuerySessionInformation()
instead.– Remy Lebeau
Nov 23 '18 at 10:47
|
show 3 more comments
2 Answers
2
active
oldest
votes
Since there can be 0, 1 or more users logged in, you need LsaEnumerateLogonSessions
. For every user, call LsaGetLogonSessionData
to get the user name.
add a comment |
MSDN document says "Retrieves the name of the user associated with the current thread."
GetUserName
can only get users of the current thread. The user of the service is "SYSTEM".
1
Unless of course the service is configured to run as a different user.LocalService
is also common.
– MSalters
Nov 23 '18 at 10:52
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Since there can be 0, 1 or more users logged in, you need LsaEnumerateLogonSessions
. For every user, call LsaGetLogonSessionData
to get the user name.
add a comment |
Since there can be 0, 1 or more users logged in, you need LsaEnumerateLogonSessions
. For every user, call LsaGetLogonSessionData
to get the user name.
add a comment |
Since there can be 0, 1 or more users logged in, you need LsaEnumerateLogonSessions
. For every user, call LsaGetLogonSessionData
to get the user name.
Since there can be 0, 1 or more users logged in, you need LsaEnumerateLogonSessions
. For every user, call LsaGetLogonSessionData
to get the user name.
answered Nov 23 '18 at 8:02
MSalters
133k8115267
133k8115267
add a comment |
add a comment |
MSDN document says "Retrieves the name of the user associated with the current thread."
GetUserName
can only get users of the current thread. The user of the service is "SYSTEM".
1
Unless of course the service is configured to run as a different user.LocalService
is also common.
– MSalters
Nov 23 '18 at 10:52
add a comment |
MSDN document says "Retrieves the name of the user associated with the current thread."
GetUserName
can only get users of the current thread. The user of the service is "SYSTEM".
1
Unless of course the service is configured to run as a different user.LocalService
is also common.
– MSalters
Nov 23 '18 at 10:52
add a comment |
MSDN document says "Retrieves the name of the user associated with the current thread."
GetUserName
can only get users of the current thread. The user of the service is "SYSTEM".
MSDN document says "Retrieves the name of the user associated with the current thread."
GetUserName
can only get users of the current thread. The user of the service is "SYSTEM".
answered Nov 23 '18 at 8:06
Drake Wu
1274
1274
1
Unless of course the service is configured to run as a different user.LocalService
is also common.
– MSalters
Nov 23 '18 at 10:52
add a comment |
1
Unless of course the service is configured to run as a different user.LocalService
is also common.
– MSalters
Nov 23 '18 at 10:52
1
1
Unless of course the service is configured to run as a different user.
LocalService
is also common.– MSalters
Nov 23 '18 at 10:52
Unless of course the service is configured to run as a different user.
LocalService
is also common.– MSalters
Nov 23 '18 at 10:52
add a comment |
1
That is the standard and well documented behavior of services, by default they all run with the
SYSTEM
user.– Some programmer dude
Nov 23 '18 at 7:32
Now, what is your real problem? Why do you need your service to run as another user? What is the actual problem that's supposed to solve?
– Some programmer dude
Nov 23 '18 at 7:34
1
what if multiple users logged in ?
– RbMm
Nov 23 '18 at 7:39
1
You are going to need to about Windows user names and the fact that processes, including services, can run under different users.
– David Heffernan
Nov 23 '18 at 7:40
1
Note, the accepted answer of the linked duplicate is not a good approach. Use one of the other answers that utilize
WTSQuerySessionInformation()
instead.– Remy Lebeau
Nov 23 '18 at 10:47