windows api获取当前登录帐号

可以通过GetEnvironmentVariableW函数来获取当前登录帐号和登录域

#include <windows.h> 
#include <tchar.h> 
#include <stdio.h> 
#include <stdlib.h> 

wchar_t UserName [0x400]; 
wchar_t UserDomain [0x400]; 

/**
* 将wchar_t* 转成char*
*/
void w2c(char *pcstr,const wchar_t *pwstr, size_t len);

/**
* 将char* 转成 wchar_t*
*/
void c2w(wchar_t *pwstr,const char *str,size_t len);

int main(int agc,char* argv[])
{
	GetEnvironmentVariableW(L"USERNAME", UserName, 0x400); 
	GetEnvironmentVariableW(L"USERDOMAIN", UserDomain, 0x400); 
	wprintf(L"the login userName is %s and the domain is %s\n",UserName,UserDomain);
	char *pUserName = (char *)malloc(sizeof(char)*(2 * wcslen(UserName)+1));
	char *pUserDomain = (char *)malloc(sizeof(char)*(2 * wcslen(UserDomain)+1));
	memset(pUserName , 0 , 2 * wcslen(UserName)+1 );
	w2c(pUserName,UserName,2 * wcslen(UserName)+1) ;
	
	memset(pUserDomain , 0 , 2 * wcslen(UserDomain)+1 );
	w2c(pUserDomain,UserDomain,2 * wcslen(UserDomain)+1) ;
	printf("the login userName is %s and the domain is %s\n",pUserName,pUserDomain);
	free(pUserName);
	free(pUserDomain);
	
	char c[]="this is from ascii";
	wchar_t *wc = (wchar_t*) malloc(sizeof(wchar_t)*(strlen(c)/2)+1);
	memset(wc , 0 , wcslen(wc)+1 );
	c2w(wc,c,wcslen(wc)+1);
	printf("%s\n",c);
	wprintf(L"%s\n",wc);
	free(wc);
	return 0;
}

//将wchar_t* 转成char*的实现函数如下:
void w2c(char *pcstr,const wchar_t *pwstr, size_t len)
{
	int nlength=wcslen(pwstr);
	//获取转换后的长度
	int nbytes = WideCharToMultiByte( 0, // specify the code page used to perform the conversion
		0,         // no special flags to handle unmapped characters
		pwstr,     // wide character string to convert
		nlength,   // the number of wide characters in that string
		NULL,      // no output buffer given, we just want to know how long it needs to be
		0,
		NULL,      // no replacement character given
		NULL );    // we don't want to know if a character didn't make it through the translation
	// make sure the buffer is big enough for this, making it larger if necessary
	if(nbytes>len)   nbytes=len;
	// 通过以上得到的结果,转换unicode 字符为ascii 字符
	WideCharToMultiByte( 0, // specify the code page used to perform the conversion
		0,         // no special flags to handle unmapped characters
		pwstr,     // wide character string to convert
		nlength,   // the number of wide characters in that string
		pcstr, 	   // put the output ascii characters at the end of the buffer
		nbytes,    // there is at least this much space there
		NULL,      // no replacement character given
		NULL );
}

void c2w(wchar_t *pwstr,const char *str,size_t len)
{
	if(str)
    {
		size_t nu = strlen(str);
		size_t n =(size_t)MultiByteToWideChar(0,0,(const char *)str,(int)nu,NULL,0);
		if(n>=len)n=len-1;
		MultiByteToWideChar(0,0,(const char *)str,(int)nu,pwstr,(int)n);
		pwstr[n]=0;
    }
}
这是在网上东找西找的结果哈,由于api需要的参数是wchar_t*类型的,所以在网上找了个wchar_t* TO char*的函数

转载于:https://my.oschina.net/kyleli8628/blog/106973

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值