| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 756 人关注过本帖
标题:编译出错,获取本地计算机主机名和 IP 地址
只看楼主 加入收藏
shockwave
Rank: 2
等 级:论坛游民
帖 子:13
专家分:42
注 册:2010-10-31
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:2 
编译出错,获取本地计算机主机名和 IP 地址
程序代码:
//Get_IP.c
#include<winsock.h>
#include"resource.h"
int WSA_return;
WSADATA WSAData;
HOSTENT *host_entry;
char host_name[256];
char host_address[256];
BOOL APIENTRY Hostname_ipDlgPro(HWND hDlg,UINT message,WPARAM wParam,LPARAM IParam);
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
    WSA_return = WSAStartup(0x0101,&WSAData);
    if(WSA_return == 0)
    {
        gethostname(host_name,256);
        host_entry = gethostbyname(host_name);
        if(host_entry !=0)
        {
            wsprintf(host_address,"%d.%d.%d.%d",
                (host_entry->h_addr_list[0][0]&0x00ff),
                (host_entry->h_addr_list[0][1]&0x00ff),
                (host_entry->h_addr_list[0][2]&0x00ff),
                (host_entry->h_addr_list[0][3]&0x00ff));
        }
    }
    WSACleanup();
    DialogBox(hInstance,"DIALOG1",NULL,(DLGPROC)Hostname_ipDlgPro);
    return 0;
}
BOOL APIENTRY Hostname_ipDlgPro(HWND hDlg,UINT message,WPARAM wParam,LPARAM IParam)
{
    switch(message)
    {
    case WM_INITDIALOG:
        return(TRUE);
    case WM_COMMAND:
        if(LOWORD(wParam)==IDOK)
        {
                SetDlgItemText(hDlg,IDC_EDIT1,host_name);
                SetDlgItemText(hDlg,IDC_EDIT2,host_address);
                SetDlgItemText(hDlg,IDCANCEL,"确定");
        }
        if(LOWORD(wParam)== IDCANCEL)
            EndDialog(hDlg,TRUE);
            return (TRUE);
            break;
    }
    return(FALSE);
}

--------------------Configuration: ceshi - Win32 Debug--------------------
Linking...
ceshi.obj : error LNK2001: unresolved external symbol __imp__DialogBoxParamA@20
ceshi.obj : error LNK2001: unresolved external symbol __imp__wsprintfA
ceshi.obj : error LNK2001: unresolved external symbol __imp__EndDialog@8
ceshi.obj : error LNK2001: unresolved external symbol __imp__SetDlgItemTextA@12
Debug/ceshi.exe : fatal error LNK1120: 4 unresolved externals

编译器vc++6.0  加了wsock32.lib
不知道为什么无法编译,是那里错了吗
搜索更多相关主题的帖子: message include 计算机 主机 
2014-09-20 01:49
砖家的谎言
Rank: 12Rank: 12Rank: 12
等 级:禁止访问
威 望:30
帖 子:693
专家分:3898
注 册:2013-12-6
收藏
得分:10 
对话框程序有问题

我不是砖家,要努力成为砖家。
2014-09-20 08:36
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9025
专家分:54030
注 册:2011-1-18
收藏
得分:10 
看错误信息,应该是缺少 User32.lib。

而你的代码,不是错误,而且完全一窍不通,外加瞎鸡巴乱写。尤其是 wsprintf(host_address,"%d.%d.%d.%d",

程序代码:
#include <windows.h>
#include "resource.h"
#include <winsock.h>
#pragma comment( lib, "Ws2_32.lib" )
#include <stdio.h>

BOOL CALLBACK Hostname_ipDlgPro( HWND hDlg, UINT message, WPARAM wParam, LPARAM IParam );

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hrevInstance, LPSTR lpCmdLine, int nCmdShow )
{
    DialogBox(hInstance,MAKEINTRESOURCE(IDD_DIALOG1),NULL,(DLGPROC)Hostname_ipDlgPro);
    return 0;
}

BOOL CALLBACK Hostname_ipDlgPro( HWND hDlg, UINT message, WPARAM wParam, LPARAM IParam )
{
    switch(message)
    {
    case WM_INITDIALOG:
        {
            WSADATA WSAData;
            if( WSAStartup(0x0101,&WSAData) == 0 )
            {
                char host_name[256];
                if( gethostname(host_name,sizeof(host_name)/sizeof(host_name[0])) == 0 )
                {
                    HOSTENT* host_entry = gethostbyname(host_name);
                    if( host_entry != NULL )
                    {
                        char host_address[256];
                        sprintf(host_address,"%d.%d.%d.%d",
                            (host_entry->h_addr_list[0][0]&0x00ff),
                            (host_entry->h_addr_list[0][1]&0x00ff),
                            (host_entry->h_addr_list[0][2]&0x00ff),
                            (host_entry->h_addr_list[0][3]&0x00ff));

                        SetDlgItemTextA(hDlg,IDC_EDIT1,host_entry->h_name);
                        SetDlgItemTextA(hDlg,IDC_EDIT2,host_address);
                    }
                }
                WSACleanup();
            }
        }
        return TRUE;
    case WM_COMMAND:
        if(LOWORD(wParam)==IDOK)
            EndDialog(hDlg,TRUE);
        else if(LOWORD(wParam)== IDCANCEL)
            EndDialog(hDlg,FALSE);
        break;
    }
    return FALSE;
}

2014-09-22 09:16
快速回复:编译出错,获取本地计算机主机名和 IP 地址
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.024274 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved