0

When I try to compile the code (showed below) I get those 2 errors:

- reference to the external simbol _WTSQuerySessionInformationW@20 not solved in the function void_cdecl Test(void)" (?Test@@YAXXZ);

- reference to the external simbol _WTSFreeMemory@20 not solved in the function void_cdecl Test(void)" (?Test@@YAXXZ)

The purpose was to output the name of the username logged on windows and the username who ran the program, but I got those 2 mistakes while compiling.

#include <iostream>

#include <Windows.h>
#include <wtsapi32.h>
#include <Lmcons.h>

void Test()
{
    WCHAR  UsernameSSR[UNLEN + 1];
    DWORD  bufCharCountSSR = UNLEN + 1;

    if (GetUserNameW(UsernameSSR, &bufCharCountSSR))
        std::wcout << L"UsernameSSR: " << UsernameSSR << std::endl;
    else
        std::wcout << L"Error getting UserNameSSR" << std::endl;


    LPWSTR UsernameWindows;
    DWORD  bufByteCountWindows;

    if (WTSQuerySessionInformationW(WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, WTSUserName, &UsernameWindows, &bufByteCountWindows))
    {
        LPWSTR Domain;
        DWORD bufByteCountDomain;

        std::wcout << L"UsernameWindows: ";

        if (WTSQuerySessionInformationW(WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, WTSDomainName, &Domain, &bufByteCountDomain))
        {
            if (*Domain)
                std::wcout << Domain << L"\\";
            WTSFreeMemory(Domain);
        }

        std::wcout << UsernameWindows << std::endl;

        WTSFreeMemory(UsernameWindows);
    }
    else
        std::wcout << L"Error getting UserNameWindows" << std::endl;

}
int main() {
    Test();
    return 0;
}
Shark44
  • 593
  • 1
  • 4
  • 11
  • 2
    Did you link with `Wtsapi32.lib`, as described in the documentation, of [WTSQuerySessionInformationW](https://learn.microsoft.com/en-us/windows/win32/api/wtsapi32/nf-wtsapi32-wtsquerysessioninformationw), and [WTSFreeMemory](https://learn.microsoft.com/en-us/windows/win32/api/wtsapi32/nf-wtsapi32-wtsfreememory)? – Algirdas Preidžius Sep 06 '19 at 17:44
  • Nope, that's the problem then – Shark44 Sep 06 '19 at 17:51
  • @AlgirdasPreidžius Do you know how to? – Shark44 Sep 06 '19 at 17:51
  • "Do you know how to?" Depends on compiler. I suspect, that you are using MSVS, so: Project Properties -> Linker -> Input -> Additional Dependencies. – Algirdas Preidžius Sep 06 '19 at 18:21
  • I'm using Visual Studio 2019.. but I can use Dev C++ too if you know it better – Shark44 Sep 06 '19 at 18:55
  • I.. Already told you, how to do it in MSVS..? Why would you assume, I knew Dev-C++ better, based on that fact? In addition, if I understand correctly, Dev-C++ is just an IDE, with which you can use any compiler (g++/clang/etc.), and compilers, might have different options. It's just MSVS, is typically used with built-in compiler: MSVC, and most of the options are within the GUI. – Algirdas Preidžius Sep 06 '19 at 19:02
  • Sorry.. I couldn't find few options so I thought you were referring to another version of MSVS.. anyway found them all after some time.. all worked, thanks for the help. – Shark44 Sep 06 '19 at 19:07
  • When linking to external libs, I usually prefer to do it explicitly in my code when possible, rather than in the project makefile, eg: `#pragma comment(lib, "Wtsapi32.lib")`. I put that near where the lib's headers are being `#include`d by the code. – Remy Lebeau Sep 06 '19 at 22:37
  • Wow.. didn't know about that, thanks. – Shark44 Sep 08 '19 at 11:13

0 Answers0