Win API GetUserName returns SYSTEM [duplicate]












0















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;
}









share|improve this question













marked as duplicate by Remy Lebeau c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

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 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
















0















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;
}









share|improve this question













marked as duplicate by Remy Lebeau c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

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 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














0












0








0








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;
}









share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 7:29









rm-rf

2381732




2381732




marked as duplicate by Remy Lebeau c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

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 c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

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 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














  • 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








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












2 Answers
2






active

oldest

votes


















5














Since there can be 0, 1 or more users logged in, you need LsaEnumerateLogonSessions. For every user, call LsaGetLogonSessionData to get the user name.






share|improve this answer





























    0














    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".






    share|improve this answer

















    • 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


















    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    5














    Since there can be 0, 1 or more users logged in, you need LsaEnumerateLogonSessions. For every user, call LsaGetLogonSessionData to get the user name.






    share|improve this answer


























      5














      Since there can be 0, 1 or more users logged in, you need LsaEnumerateLogonSessions. For every user, call LsaGetLogonSessionData to get the user name.






      share|improve this answer
























        5












        5








        5






        Since there can be 0, 1 or more users logged in, you need LsaEnumerateLogonSessions. For every user, call LsaGetLogonSessionData to get the user name.






        share|improve this answer












        Since there can be 0, 1 or more users logged in, you need LsaEnumerateLogonSessions. For every user, call LsaGetLogonSessionData to get the user name.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 8:02









        MSalters

        133k8115267




        133k8115267

























            0














            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".






            share|improve this answer

















            • 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
















            0














            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".






            share|improve this answer

















            • 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














            0












            0








            0






            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".






            share|improve this answer












            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".







            share|improve this answer












            share|improve this answer



            share|improve this answer










            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














            • 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



            Popular posts from this blog

            What visual should I use to simply compare current year value vs last year in Power BI desktop

            Alexandru Averescu

            Trompette piccolo