求教 无法从“const char [14]”转换为“wchar_t”

fox513 2009-08-12 10:13:23
我写了一段代码 主要就是想用NetUserGetInfo函数,从返回的参数里判断账号密码是否永不过期,但是在
wchar_t user = "administrator"; LPCWSTR username = &user;的地方遇到编译错误 错误 1 error C2440: “初始化”: 无法从“const char [14]”转换为“wchar_t” 应该如何修改?另外我这段代码是否能判断administrator账号密码永不过期?

#ifndef UNICODE
#define UNICODE
#endif

#include <stdio.h>
#include <windows.h>
#include <lm.h>
#pragma comment(lib , "Netapi32.lib")
int main(int argc, wchar_t *argv[])
{
DWORD dwLevel = 2;
LPUSER_INFO_2 pBuf = NULL;
LPCWSTR servername = NULL;
wchar_t user = "administrator";
LPCWSTR username = &user;
NET_API_STATUS nStatus;



// Call the NetUserGetInfo function; specify level 2.
//
nStatus = NetUserGetInfo(servername,
username,
dwLevel,
(LPBYTE *)&pBuf);
//
// If the call succeeds, print the user information.
//
if (nStatus == NERR_Success)
{
if (pBuf != NULL)
if(pBuf->usri2_flags==UF_DONT_EXPIRE_PASSWD)
printf("The password should never expire on the account\n");
else
printf("The password will expire on the account\n");


}
// Otherwise, print the system error.
//
else
fprintf(stderr, "A system error has occurred: %d\n", nStatus);
//
// Free the allocated memory.
//
if (pBuf != NULL)
NetApiBufferFree(pBuf);
getchar();//使ms-dos窗口停留,以查看程序显示的信息

return 0;
}
...全文
1920 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
liuxianliuxian 2010-09-19
  • 打赏
  • 举报
回复
这个问题真是烦,纠结
glorywu 2009-08-13
  • 打赏
  • 举报
回复
用宽字节字符串就可以了。
会思考的草 2009-08-12
  • 打赏
  • 举报
回复
wchar_t user = "administrator";
LPCWSTR username = &user;

改成:
LPCWSTR username = L"administrator";

错误点:
其一,user被定义成一个宽字符,如何能将一个字符串赋给单个的一个字符?何况还是将窄字符串赋给宽字符;
其二,定义user作为中转完全是不必要的脱裤子放屁。
MoXiaoRab 2009-08-12
  • 打赏
  • 举报
回复
Unicode问题
fox513 2009-08-12
  • 打赏
  • 举报
回复
还要把LPCWSTR username = &user改成LPCWSTR username = user
youyifang 2009-08-12
  • 打赏
  • 举报
回复
用a2cw这个函数,前面记得调用USES_CONVERSION
包含头文件查msdn
skybblue 2009-08-12
  • 打赏
  • 举报
回复
const wchar_t user[] = L"administrator";
LPCWSTR username = user;
skybblue 2009-08-12
  • 打赏
  • 举报
回复
改成:
wchar_t user[] = L"administrator";
LPCWSTR username = user;



唉!!!!!.......
fox513 2009-08-12
  • 打赏
  • 举报
回复
改了 还是无法从“const char [14]”转换为“wchar_t”
skybblue 2009-08-12
  • 打赏
  • 举报
回复
改成:
wchar_t user[] = L"administrator";
skybblue 2009-08-12
  • 打赏
  • 举报
回复
改成:
wchar_t user = L"administrator";

2,640

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 硬件/系统
社区管理员
  • 硬件/系统社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧