mfc获取计算机名和用户名

两个函数搞定GetComputerName();和GetUserName();

函数原型:

BOOL WINAPI GetComputerName(
  __out    LPTSTR lpBuffer,
  __inout  LPDWORD lpnSize
);

 
  
BOOL WINAPI GetUserName(
  __out    LPTSTR lpBuffer,
  __inout  LPDWORD lpnSize
);

示例代码:

void CGetSysInfo::GetPCNameAndUserName(CString &strPCName, CString &strUserName)
{

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

	LPWSTR szBuffer;
	szBuffer = (LPWSTR)malloc(500);
	dwNameLen = MAX_BUFFER_LEN;  
	if (!GetComputerName(szBuffer, &dwNameLen))   
		printf("Error  %d\n", GetLastError());  
	else  {
		printf("计算机名为: %S\n", szBuffer);
		strPCName += szBuffer;
	}

	dwNameLen = MAX_BUFFER_LEN;  
	if (!GetUserName(szBuffer, &dwNameLen))  
		printf("Error  %d\n", GetLastError());  
	else  {
		printf("当前用户名为:%S\n", szBuffer);
		strUserName += szBuffer;
		}
	if(szBuffer)
		free(szBuffer);
}
完成!

你可能感兴趣的:(c++)