关于函数“GetUserName()”,参见:https://msdn.microsoft.com/en-us/library/windows/desktop/ms724432(v=vs.85).aspx
IDE: Code::Blocks
操作系统:Windows 7 x64
1 #include <windows.h>
2 #include <stdio.h>
3
4 using namespace std;
5
6 int main()
7 {
8 CHAR cUserNameBuffer[256];
9 DWORD dwUserNameSize = 256;
10
11 /* Retrieves the name of the user associated with the current thread. */
12 if(GetUserName(cUserNameBuffer, &dwUserNameSize)) {
13 printf("The user name is %s \n", cUserNameBuffer);
14 }
15 else {
16 printf("Get user name failed with error: %lu \n", GetLastError());
17 }
18
19 return 0;
20 }
本文介绍了一个简单的C++程序,用于在Windows环境下通过调用GetUserName()函数来获取当前线程所关联用户的名称。程序使用了标准的Windows API,并展示了如何处理可能出现的错误。
447

被折叠的 条评论
为什么被折叠?



