result is wrong!

shuaiAWP 2008-10-04 02:17:42
#include <stdio.h>
#include <windows.h>
//#include <scok2.h>
#pragma comment(lib,"ws2_32.lib")
void main(int argc ,char *argv[]) {
char name[100];
wchar_t uname[100];
if(!gethostname(name,100))
printf("error");
mbstowcs(uname,name,100);
printf("hostname = %s\n",uname);
}


...全文
47 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zclever 2008-10-04
  • 打赏
  • 举报
回复
使用gethostname之前要要调用WSAStartup
shuaiAWP 2008-10-04
  • 打赏
  • 举报
回复
成功了
原来没有用WSAStartup()
正确的程序是
WORD wVersionRequested;
/*获得计算机名字
*
*
*/

#include <stdio.h>
#include <windows.h>
//#include <scok2.h>
#pragma comment(lib,"ws2_32.lib")
void main(int argc ,char *argv[]) {
WORD wVersionRequested;
WSADATA wsaData;
int err;

wVersionRequested = MAKEWORD( 2, 2 );

err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
return;
}

/* Confirm that the WinSock DLL supports 2.2.*/
/* Note that if the DLL supports versions greater */
/* than 2.2 in addition to 2.2, it will still return */
/* 2.2 in wVersion since that is the version we */
/* requested. */

if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 2 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
WSACleanup( );
return;
}

char name[100];
//wchar_t uname[100];
if(gethostname(name,100))
{printf("error\n");
return;
} //mbstowcs(uname,name,100);
printf("hostname = %s\n",name);
}
aozhi 2008-10-04
  • 打赏
  • 举报
回复
debug一下吧
aozhi 2008-10-04
  • 打赏
  • 举报
回复
up
shuaiAWP 2008-10-04
  • 打赏
  • 举报
回复
我给改了
还是不行啊
结果是error
qqwx_1986 2008-10-04
  • 打赏
  • 举报
回复
if(!gethostname(name,100))
printf("error");
应该是
if(gethostname(name,100))
printf("error"); 吧
gethostname没错误返回0

69,371

社区成员

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

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