VC++ 得到计算机名和用户名 GetComputerName GetUserName

转载请标明出处,原文地址:http://blog.csdn.net/morewindows/article/details/8659417

欢迎关注微博:http://weibo.com/MoreWindows

 

在CSDN论坛上看到有帖子在问如何获得计算机名称及用户名。这个其实非常简单。二个函数——GetComputerNameGetUserName就搞定了。其函数原型如下:

一.GetComputerName

//By MoreWindows-(http://blog.csdn.net/MoreWindows)

BOOLWINAPI GetComputerName(

    __out         LPTSTRlpBuffer,

    __in_out      LPDWORDlpnSize

);

二.GetUserName

//By MoreWindows-(http://blog.csdn.net/MoreWindows)

BOOLWINAPI GetUserName(

    __out         LPTSTRlpBuffer,

    __in_out      LPDWORDlpnSize

);

直接上代码算了,这参数光看名字就知道什么意思了。 

  1. // VC++得到计算机名称和用户名称     
  2. // http://blog.csdn.net/morewindows/article/details/8659417   
  3. //By MoreWindows-(http://blog.csdn.net/MoreWindows)     
  4. #include <windows.h>   
  5. #include <stdio.h>   
  6. int main()  
  7. {     
  8.     printf("    VC++得到计算机名称和用户名称 \n");          
  9.     printf(" -- By MoreWindows( http://blog.csdn.net/MoreWindows ) --\n\n");   
  10.   
  11.     const int MAX_BUFFER_LEN = 500;  
  12.     char  szBuffer[MAX_BUFFER_LEN];  
  13.     DWORD dwNameLen;  
  14.   
  15.     dwNameLen = MAX_BUFFER_LEN;  
  16.     if (!GetComputerName(szBuffer, &dwNameLen))   
  17.         printf("Error  %d\n", GetLastError());  
  18.     else  
  19.         printf("计算机名为: %s\n", szBuffer);  
  20.   
  21.     dwNameLen = MAX_BUFFER_LEN;  
  22.     if (!GetUserName(szBuffer, &dwNameLen))  
  23.         printf("Error  %d\n", GetLastError());  
  24.     else  
  25.         printf("当前用户名为:%s\n", szBuffer);  
  26.     return 0;  
  27. }  
// VC++得到计算机名称和用户名称  
// http://blog.csdn.net/morewindows/article/details/8659417
//By MoreWindows-(http://blog.csdn.net/MoreWindows)  
#include <windows.h>
#include <stdio.h>
int main()
{	
	printf("    VC++得到计算机名称和用户名称 \n");        
	printf(" -- By MoreWindows( http://blog.csdn.net/MoreWindows ) --\n\n"); 

	const int MAX_BUFFER_LEN = 500;
	char  szBuffer[MAX_BUFFER_LEN];
	DWORD dwNameLen;

	dwNameLen = MAX_BUFFER_LEN;
	if (!GetComputerName(szBuffer, &dwNameLen)) 
		printf("Error  %d\n", GetLastError());
	else
		printf("计算机名为: %s\n", szBuffer);

	dwNameLen = MAX_BUFFER_LEN;
	if (!GetUserName(szBuffer, &dwNameLen))
		printf("Error  %d\n", GetLastError());
	else
		printf("当前用户名为:%s\n", szBuffer);
	return 0;
}

运行结果如下:

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值