C++;获取和显示用户名的Microsoft示例不';不编译 我想用最简单的方式在C++中获取用户名。我的程序只为Windows设计
我想用这个。我在一个全新的Visual Studio 2019中复制粘贴代码示例,但在C++;获取和显示用户名的Microsoft示例不';不编译 我想用最简单的方式在C++中获取用户名。我的程序只为Windows设计,c++,windows,winapi,wchar-t,tchar,C++,Windows,Winapi,Wchar T,Tchar,我想用这个。我在一个全新的Visual Studio 2019中复制粘贴代码示例,但在文本说明上有错误: 类型为“const wchar\u t*”的值不能用于初始化类型为“TCHAR*”的实体 这是完整的代码: #include <windows.h> #include <tchar.h> #include <stdio.h> TCHAR* envVarStrings[] = { TEXT("OS = %OS%"), TEXT("P
文本
说明上有错误:
类型为“const wchar\u t*”的值不能用于初始化类型为“TCHAR*”的实体
这是完整的代码:
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
TCHAR* envVarStrings[] =
{
TEXT("OS = %OS%"),
TEXT("PATH = %PATH%"),
TEXT("HOMEPATH = %HOMEPATH%"),
TEXT("TEMP = %TEMP%")
};
#define ENV_VAR_STRING_COUNT (sizeof(envVarStrings)/sizeof(TCHAR*))
#define INFO_BUFFER_SIZE 32767
void printError( TCHAR* msg );
void main( )
{
DWORD i;
TCHAR infoBuf[INFO_BUFFER_SIZE];
DWORD bufCharCount = INFO_BUFFER_SIZE;
// Get and display the name of the computer.
bufCharCount = INFO_BUFFER_SIZE;
if( !GetComputerName( infoBuf, &bufCharCount ) )
printError( TEXT("GetComputerName") );
_tprintf( TEXT("\nComputer name: %s"), infoBuf );
// Get and display the user name.
bufCharCount = INFO_BUFFER_SIZE;
if( !GetUserName( infoBuf, &bufCharCount ) )
printError( TEXT("GetUserName") );
_tprintf( TEXT("\nUser name: %s"), infoBuf );
// Get and display the system directory.
if( !GetSystemDirectory( infoBuf, INFO_BUFFER_SIZE ) )
printError( TEXT("GetSystemDirectory") );
_tprintf( TEXT("\nSystem Directory: %s"), infoBuf );
// Get and display the Windows directory.
if( !GetWindowsDirectory( infoBuf, INFO_BUFFER_SIZE ) )
printError( TEXT("GetWindowsDirectory") );
_tprintf( TEXT("\nWindows Directory: %s"), infoBuf );
// Expand and display a few environment variables.
_tprintf( TEXT("\n\nSmall selection of Environment Variables:") );
for( i = 0; i < ENV_VAR_STRING_COUNT; ++i )
{
bufCharCount = ExpandEnvironmentStrings(envVarStrings[i], infoBuf,
INFO_BUFFER_SIZE );
if( bufCharCount > INFO_BUFFER_SIZE )
_tprintf( TEXT("\n\t(Buffer too small to expand: \"%s\")"),
envVarStrings[i] );
else if( !bufCharCount )
printError( TEXT("ExpandEnvironmentStrings") );
else
_tprintf( TEXT("\n %s"), infoBuf );
}
_tprintf( TEXT("\n\n"));
}
void printError( TCHAR* msg )
{
DWORD eNum;
TCHAR sysMsg[256];
TCHAR* p;
eNum = GetLastError( );
FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, eNum,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
sysMsg, 256, NULL );
// Trim the end of the line and terminate it with a null
p = sysMsg;
while( ( *p > 31 ) || ( *p == 9 ) )
++p;
do { *p-- = 0; } while( ( p >= sysMsg ) &&
( ( *p == '.' ) || ( *p < 33 ) ) );
// Display the message
_tprintf( TEXT("\n\t%s failed with error %d (%s)"), msg, eNum, sysMsg );
}
该代码假定为C编译器。要使此编译为C++,必须声明<代码> EVVARSTROUNS 为<代码> TCHAR COST*EnVARTROS[]/CUD>,并更改代码<空格PrTrror(TCHAR * MSG)< /C> >签名,以读取<代码> VoT PrTrror(TCHAR COST**MSG)。与C不同,不能将字符串文本分配给C++中的非const指针。
如果只需要用户名,则可以调用而不是读取环境变量。API调用返回与调用线程关联的用户名。代码采用C编译器。要使此编译为C++,必须声明<代码> EVVARSTROUNS 为<代码> TCHAR COST*EnVARTROS[]/CUD>,并更改代码<空格PrTrror(TCHAR * MSG)< /C> >签名,以读取<代码> VoT PrTrror(TCHAR COST**MSG)。与C不同,不能将字符串文本分配给C++中的非const指针。 如果只需要用户名,则可以调用而不是读取环境变量。API调用返回与调用线程关联的用户名。提供所需内容。功能和功能正是您所需要的,使用非常简单。下面是一个有效的示例:
#include <iostream>
#include <string>
#include <windows.h>
#include <Lmcons.h>
std::wstring getUsername() {
wchar_t username[UNLEN + 1];
DWORD username_len = UNLEN + 1;
GetUserNameW(username, &username_len);
return username;
}
std::wstring getComputerName() {
wchar_t computerName[UNLEN + 1];
DWORD computerName_len = UNLEN + 1;
GetComputerNameW(computerName, &computerName_len);
return computerName;
}
int main()
{
std::wcout << L"Username is : " << getUsername() << std::endl;
std::wcout << L"Computer name is : " << getComputerName() << std::endl;
return 0;
}
#包括
#包括
#包括
#包括
std::wstring getUsername(){
wchar_t用户名[UNLEN+1];
DWORD username_len=UNLEN+1;
GetUserNameW(用户名和用户名);
返回用户名;
}
std::wstring getComputerName(){
wchar_t computerName[UNLEN+1];
DWORD computerName_len=UNLEN+1;
GetComputerName w(computerName和computerName_len);
返回计算机名;
}
int main()
{
std::wcout提供您需要的功能。函数和正是您需要的,用法非常简单。下面是一个有效的示例:
#include <iostream>
#include <string>
#include <windows.h>
#include <Lmcons.h>
std::wstring getUsername() {
wchar_t username[UNLEN + 1];
DWORD username_len = UNLEN + 1;
GetUserNameW(username, &username_len);
return username;
}
std::wstring getComputerName() {
wchar_t computerName[UNLEN + 1];
DWORD computerName_len = UNLEN + 1;
GetComputerNameW(computerName, &computerName_len);
return computerName;
}
int main()
{
std::wcout << L"Username is : " << getUsername() << std::endl;
std::wcout << L"Computer name is : " << getComputerName() << std::endl;
return 0;
}
#包括
#包括
#包括
#包括
std::wstring getUsername(){
wchar_t用户名[UNLEN+1];
DWORD username_len=UNLEN+1;
GetUserNameW(用户名和用户名);
返回用户名;
}
std::wstring getComputerName(){
wchar_t computerName[UNLEN+1];
DWORD computerName_len=UNLEN+1;
GetComputerName w(computerName和computerName_len);
返回计算机名;
}
int main()
{
std::wcoutL”用户名是:“
当您使用宽流时。但这并不能回答问题。@IInspectable,粗体的问题是:如何创建函数以及应该包含哪些标头?
。我给填充的函数提供了与之相同的签名。这是问题标题:“获取和显示用户名的C++Microsoft示例未编译”。此外,您为检索计算机名传递了错误的数组大小(请参阅)。然后,没有错误处理。另一方面,由于GetUserNameW()
和GetComputerNameW()
输出最终字符串长度,我建议在构造std::wstring
值时使用这些长度,例如返回std::wstring(username,username\u len-1);
返回std::wstring(computerName,computerName\u len);
这样,std::wstring
就不必重新计算事先已知的长度。L”用户名是:“
当您使用宽流时。但这并不能回答问题。@IInspectable,粗体的问题是:如何创建函数以及应该包含哪些标题?
。我给填充函数的签名与它的签名相同。这是问题标题:“获取和显示用户名的C++Microsoft示例未编译”。此外,您为检索计算机名传递了错误的数组大小(请参阅)。然后,没有错误处理。另一方面,由于GetUserNameW()
和GetComputerNameW()
输出最终字符串长度,我建议在构造std::wstring
值时使用这些长度,例如返回std::wstring(username,username\u len-1);
返回std::wstring(computerName,computerName\u len)
这样,std::wstring
就不必重新计算事先已知的长度了。@ben:的确如此。不过,我不清楚OP想要完成什么。他们也在读取环境变量,毕竟还有%USERNAME%
,所以我加了一条注释,读取环境变量是不可能的't required.FYI,Win32 API具有LPCTSTR
别名,用于const TCHAR*
:LPCTSTR envVarStrings[]=…;无效打印错误(LPCTSTR msg)
@rem:这些别名的主要目的是通过添加一个间接级别来对ABI建模。所讨论的代码不跨ABI,因此使用这些别名不会带来任何好处。在这种情况下,它们只是增加了使用语言级别类型语法的复杂性。通过语法高亮显示,您可以看到*
和const
限定符。与使用像LPCTSTR
这样的别名相比,这减少了读者的心理负担。这种别名将所有内容合并到一个标记中。该标记需要由某个此时不需要分心的人分解为其组成部分。@ben:的确如此。不过,我不清楚OP是什么正在努力完成。他们也在读取环境变量,而且毕竟有%USERNAME%
,所以我添加了一个注释,说明不需要读取环境变量。仅供参考,Win32 API有一个LPCTSTR
别名,用于const TCHAR*
:<
#include <iostream>
#include <string>
#include <windows.h>
#include <Lmcons.h>
std::wstring getUsername() {
wchar_t username[UNLEN + 1];
DWORD username_len = UNLEN + 1;
GetUserNameW(username, &username_len);
return username;
}
std::wstring getComputerName() {
wchar_t computerName[UNLEN + 1];
DWORD computerName_len = UNLEN + 1;
GetComputerNameW(computerName, &computerName_len);
return computerName;
}
int main()
{
std::wcout << L"Username is : " << getUsername() << std::endl;
std::wcout << L"Computer name is : " << getComputerName() << std::endl;
return 0;
}