用Windows api函数得计算机的用户名之疑问

采石工 2010-12-15 11:43:47
下面的程序要实现的是得到并显示计算机名和用户名,可是在得到用户名时有问题,不知错在哪里,请高手指正?

#include <stdio.h>
#include <windows.h>
#define BUFSIZE 256

void main()
{
TCHAR buffer1[BUFSIZE];
TCHAR buffer2[BUFSIZE];
DWORD nSize = BUFSIZE;

// Get and display the name of the computer.
GetComputerName(buffer1, &nSize);
printf("Computer name: %s\n", buffer1);

// Get and display the user name.
GetUserName(buffer2, &nSize);
sprintf("User name: %s\n", buffer2);
}
...全文
62 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
bdmh 2010-12-15
  • 打赏
  • 举报
回复
如果本机是unicode的,那么用wprintf

#define BUFSIZE 256

void main()
{
TCHAR buffer1[BUFSIZE];
TCHAR buffer2[BUFSIZE];
DWORD nSize = BUFSIZE;

// Get and display the name of the computer.
GetComputerName(buffer1, &nSize);
wprintf(L"Computer name: %s\n", buffer1);

// Get and display the user name.
GetUserName(buffer2, &nSize);
wprintf(L"User name: %s\n", buffer2);
}
dingshaofengbinbin 2010-12-15
  • 打赏
  • 举报
回复
sprintf()用于字符串的操作的!!不是打印的!!
采石工 2010-12-15
  • 打赏
  • 举报
回复
哦,谢谢你的回复,这是我的疏忽。
luciferisnotsatan 2010-12-15
  • 打赏
  • 举报
回复
int printf(
const char *format [,
argument]...
);


int sprintf(
char *buffer,
const char *format [,
argument] ...
);

luciferisnotsatan 2010-12-15
  • 打赏
  • 举报
回复
sprintf("User name: %s\n", buffer2); 去掉s

另外printf是char版本的,WCHAR用wprintf(L"User name: %s\n", buffer2)

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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