CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Sep 2020
    Posts
    6

    Obtaining LogonDomain for logged on Windows Active Directory user

    I am trying to obtain the name of the LogonDomain for the currently logged on user. I need this information to be able to determine if the user was logged in using a MicrosoftAccount, AzureAD or standard Windows Active Directory Domain.

    When using the Sysinternals BGInfo.exe utility, when on a Windows active directoy I get the name of the Domain, when running for a user logged in using a Microsoft account it returns "MicrosoftAccount". This is what I am expecting.

    To this I am trying to use the WTSQuerySessionInformation with "WTSConfigInfo" for the WTS_INFO_CLASS parameter as follows:


    DWORD dwSessionID;
    LPSTR ppBuffer = NULL;
    DWORD dwBytesReturned = 0;
    WTSCONFIGINFO* pInfo = NULL;
    WTS_INFO_CLASS wtsci = WTSConfigInfo;

    if (!ProcessIdToSessionId(GetCurrentProcessId(), &dwSessionID))
    {
    return;
    }


    if (WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, dwSessionID, wtsci, &ppBuffer, &dwBytesReturned))
    {

    if (dwBytesReturned > 0)
    {
    pInfo = (WTSCONFIGINFO*)ppBuffer;
    OutputDebugStringX("LogonUserName = %s", pInfo->LogonUserName);
    OutputDebugStringX("LogonDomain = %s", pInfo->LogonDomain);
    }
    }

    WTSFreeMemory(ppBuffer);


    The code executes without error with 848 bytes returned. The problem is that besides the pInfo->Version parameter that returns "1", everything else is blank.

    Anyone have any idea why this is not working or if there is another way to determine who validated the logged on user?

  2. #2
    Join Date
    Jan 2009
    Posts
    399

    Re: Obtaining LogonDomain for logged on Windows Active Directory user

    Have you tried this code as administrator ?

  3. #3
    Join Date
    Sep 2020
    Posts
    6

    Re: Obtaining LogonDomain for logged on Windows Active Directory user

    Yes and I still have the same result

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Obtaining LogonDomain for logged on Windows Active Directory user

    Are you compiling as ASCII or Unicode? If as Unicode then LogonUserName is a wide which would display as blank using ASCII.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Sep 2020
    Posts
    6

    Re: Obtaining LogonDomain for logged on Windows Active Directory user

    I am compiling as ASCII but I have tried the exact code as above but explicitly using the Wide versions of the functions (i.e. WTSQuerySessionInformationW etc.) and the results are still the same.

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Obtaining LogonDomain for logged on Windows Active Directory user

    Have you tried the ASCII version (WTSQuerySessionInformationA)
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #7
    Join Date
    Sep 2020
    Posts
    6

    Re: Obtaining LogonDomain for logged on Windows Active Directory user

    Yes, again with the same result

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Obtaining LogonDomain for logged on Windows Active Directory user

    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Obtaining LogonDomain for logged on Windows Active Directory user

    Rather than using WMI, use the GetComputerNameEx Win32 function.

  10. #10
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Obtaining LogonDomain for logged on Windows Active Directory user

    Sysinternals BGInfo.exe utility
    This is a c# replacement for BGInfo - but there is source available so you might discover how its done.

    See https://github.com/zodiacon/BgInfo
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  11. #11
    Join Date
    Sep 2020
    Posts
    6

    Re: Obtaining LogonDomain for logged on Windows Active Directory user

    Thanks, I have looked at this. Unfortunately, for LogonDomain, this utility does not report the same information as the BGInfo utility by Sysinternals. When the user is logged in using a Microsoft account, this utility reports the workstation name and not "MicrosoftAccount" as the Sysinternals utility reports. It does not differentiate between a local account and a Microsoft account.

    All I am really looking for is to know whether the user logged on using a "MicrosoftAccount", "AzureAd" or normal Windows Domain AD account. Perhaps the approach I am taking is not best idea. Perhaps there is a completely different way of determining this. Anyone have any ideas?

  12. #12
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Obtaining LogonDomain for logged on Windows Active Directory user

    Quote Originally Posted by Edz1o View Post
    Perhaps there is a completely different way of determining this. Anyone have any ideas?
    Yes, see post #9.

  13. #13
    Join Date
    Sep 2020
    Posts
    6

    Re: Obtaining LogonDomain for logged on Windows Active Directory user

    As per the documentation for GetComputerNameEx "The names are established at system startup, when the system reads them from the registry." Therefore the name is the same regardless of whether the user logged in using a WindowsAccount, local account or normal Domain account.

    I have tried the function nevertheless (with all the 8 options for "Name Type") and it would either return the computer name, domain name or combination of computer and domain name.

    Obviously what I need is possible because Sysinternals is able to provide this information with BGInfo. I have not seen anyone else able to do it.

    Any other ideas?

  14. #14
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Obtaining LogonDomain for logged on Windows Active Directory user

    Possibly - but it's a bit a long shot. Use Sysinternals Process Monitor to monitor the registry activity for Sysinternals BGInfo. You might find where BGInfo is getting its info.

    Mark knows more about Windows internals that just about anyone who hasn't been involved in its development. When he ran his own company which originally produced these utilities many of them came with the source code so that you could study them and learn. Unfortunately this stopped when he 'joined' Microsoft and the existing source was pulled.

    The old source code is available at http://web.archive.org/web/200604272...ourceCode.html - but it seems BGInfo came out after this and so its source isn't there. But you might find some source of interest.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured