I recently switched from Visual studio code to Visual studio 2019 because I wanted to download C++ package using VCPKG.
My code worked on Visual Studio Code but do not work on Visual Studio 2019, maybe because it's not the same compilator.
The code I found on a StackOverflow post ( Get Computer Name and logged user name ) retrieve the Computer name and the Username of a PC.
#define INFO_BUFFER_SIZE 32767
TCHAR infoBuf[INFO_BUFFER_SIZE];
DWORD bufCharCount = INFO_BUFFER_SIZE;
SID_IDENTIFIER_AUTHORITY pIdentStruct = SECURITY_NT_AUTHORITY;
PSID ptrSidStruct;
BOOL isMember;
Constructor::Contructor(): c_online(true), c_status("Alive"), c_master("Master IP"), c_port(80)
{
bufCharCount = INFO_BUFFER_SIZE;
if (GetComputerName(infoBuf, &bufCharCount))
{
c_pc = TEXT(infoBuf);
}
bufCharCount = INFO_BUFFER_SIZE;
if (GetUserName(infoBuf, &bufCharCount))
{
c_user = TEXT(infoBuf);
}
}
However while using the TEXT() function, I get the error "LinfoBuf not define". I don't understand this error, plus I declared infoBuf not LinfoBuf. I didn't find the GetUserName function using <windows.h> in MSND documentation but I found GetUserNameA using the header <winbase.h>
According to the Windows.h
header file, TEXT()
is not a function, its a macro. Its purpose is to convert string/character literals at compile-time to either wchar_t
or char
literals, depending on whether the UNICODE
conditional is defined or not, respectively.
Basically the definition of TEXT
is something like this,
#ifdef UNICODE
#define __TEXT(quote) L##quote
#else
#define __TEXT(quote) quote
#endif
#define TEXT(quote) __TEXT(quote)
In C++, narrow string literals are denoted as "some string"
and wide string literals are denoted as L"some string"
. A ##
in a macro, like what we see above, concatenates tokens together. In this case, concatenating L
in front of the macro argument when UNICODE
is defined. So when we say TEXT("Hello")
, the pre-processor output is L"Hello"
when UNICODE
is defined, otherwise the output is "Hello"
.
Since infoBuf
isn't a string literal, instead its a pointer, when you use TEXT(infoBuf)
, the compiler resolves it to be LinfoBuf
, where the L
comes from the macro conversion.
GetComputerName
and GetUserName
are macro wrappers for the GetComputerNameA
/ GetComputerNameW
and GetUserNameA
/ GetUserNameW
functions, respectively. These functions output the names as char
or wchar_t
strings. When UNICODE
is defined, the W
functions are used, otherwise the A
functions are used. So, if c_pc
and c_user
are containers of char
s (like std::string
), your best option is to use GetComputerNameA
and GetUserNameA
directly, eg:
#define INFO_BUFFER_SIZE 32767
CHAR infoBuf[INFO_BUFFER_SIZE];
DWORD bufCharCount = INFO_BUFFER_SIZE;
Constructor::Contructor(): c_online(true), c_status("Alive"), c_master("Master IP"), c_port(80)
{
bufCharCount = INFO_BUFFER_SIZE;
if (GetComputerNameA(infoBuf, &bufCharCount))
{
c_pc = infoBuf;
}
bufCharCount = INFO_BUFFER_SIZE;
if (GetUserNameA(infoBuf, &bufCharCount))
{
c_user = infoBuf;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.