sirroom
驱动大牛
驱动大牛
  • 注册日期2001-07-30
  • 最后登录2018-05-29
  • 粉丝0
  • 关注0
  • 积分6分
  • 威望11点
  • 贡献值1点
  • 好评度0点
  • 原创分0分
  • 专家分0分
阅读:2484回复:14

得到本机ip的几种方法

楼主#
更多 发布于:2002-05-30 11:05
一.注册表.
1. Open registry on the HKEY_LOCAL_MACHINE
2. Find the subKey = SOFTWARE\\Microsoft\\Windows
NT\\CurrentVersion\\NetworkCards\\\"netcard# \"
\"netcard# \" usually is \"1\" but you can control any subkey and find
specific netwrok Adapters
Refer to \"MSDN Registry Entries for Network Adapter Cards\"
3. Get data for the value \"ServiceName\"
4. Find the subKey =
SYSTEM\\CurrentControlSet\\Services\\\"ServiceName\"\\Parameters\\TcpIp\"
Set data for the value \"IpAddress\" to change IP address
6. Set data for the value \"SubnetMask\" to change subnet mask
7. Set data for the value \"DefaultGateway\" to change default gateway
8. Reboot your PC.

二.winsock
Initialise Winsock before executing these steps...
Check errors rigrously after each step...

char HostName[MAX_HOSTNAME_SIZE];
PHOSTENT pHostEnt;
IN_ADDR  inAddr;
char String[20];

gethostname(HostName, MAX_HOSTNAME_SIZE);
pHostEnt = gethostbyname(HostName);
memcpy(&inAddr, pHostEnt->h_addr, 4);
wsprintf(String, \"%s\", inet_ntoa(inAddr));


最新喜欢:

baoyibao99baoyib... znsoftznsoft
111
guitar_string
驱动牛犊
驱动牛犊
  • 注册日期2003-06-04
  • 最后登录2004-06-04
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
沙发#
发布于:2003-07-21 17:35
sirroom
    你好,谢谢你全面的总结,但我有两个问题需要请教:
    1、我需要在xpassthru的recv.c中获得本机的IP,请问应该用你说的注册表方法还是IMD方法?注册表函数能否在最底层使用?我有些看不懂IMD方法,难道是解析arp包找到其中的信息!是否有更节约资源的方法?同时要求,一旦用户更改自己的IP(内部网虚拟IP),该方法能获得更新的IP。

    2、另外请教一个问题:malloc怎么不能在recv.c中使用!我的测试是这样的:
    文件开头#include <stdlib.h>
            #include <malloc.h>
    在文件中的函数PtReceive中,有代码:
            p_short = (unsigned short *) malloc (sizeof(unsigned short));
free (p_short);
    编译后出现link错误:
           recv.obj() : error LNK2001: unresolved external symbol __imp__free
           recv.obj() : error LNK2001: unresolved external symbol __imp__malloc

我将malloc换成new竟然出现一大堆错误,
    #include <memory>,错误位new undefined!


谢谢!

  
guard2002
驱动中牛
驱动中牛
  • 注册日期2002-05-21
  • 最后登录2017-03-07
  • 粉丝0
  • 关注0
  • 积分46分
  • 威望65点
  • 贡献值0点
  • 好评度4点
  • 原创分0分
  • 专家分0分
  • 社区居民
板凳#
发布于:2002-06-05 10:02
在方法2中没找到IP地址
在HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\{335C65F0-448E-4832-9BF0-EF7BB3DE4F9A}中有,藏的太深了
sirroom
驱动大牛
驱动大牛
  • 注册日期2001-07-30
  • 最后登录2018-05-29
  • 粉丝0
  • 关注0
  • 积分6分
  • 威望11点
  • 贡献值1点
  • 好评度0点
  • 原创分0分
  • 专家分0分
地板#
发布于:2002-06-04 08:51
95看来也不管用
111
sirroom
驱动大牛
驱动大牛
  • 注册日期2001-07-30
  • 最后登录2018-05-29
  • 粉丝0
  • 关注0
  • 积分6分
  • 威望11点
  • 贡献值1点
  • 好评度0点
  • 原创分0分
  • 专家分0分
地下室#
发布于:2002-06-04 08:41
看来有可能吧.
Requirements
  Windows NT/2000/XP: Included in Windows 2000; Windows XP Pro; and Windows .NET Server.
  Windows 95/98/Me: Included in Windows 98 and later.
  Header: Declared in Iphlpapi.h.
  Library: Use Iphlpapi.lib.

真的没有nt.....
111
sirroom
驱动大牛
驱动大牛
  • 注册日期2001-07-30
  • 最后登录2018-05-29
  • 粉丝0
  • 关注0
  • 积分6分
  • 威望11点
  • 贡献值1点
  • 好评度0点
  • 原创分0分
  • 专家分0分
5楼#
发布于:2002-06-04 08:40
我倒,居然nt又不行啦....
不过没办法,偶没怎么用过nt,那个东东用起来太不方便.....
只有下次做一个nt的系统来试试了.heehee.
不过你说的在nt下不行是指的哪一个,最后一个?我有点怀疑,因为核心的东东就是GetAdaptersInfo嘛
111
guardee
驱动巨牛
驱动巨牛
  • 注册日期2002-11-08
  • 最后登录2010-05-29
  • 粉丝2
  • 关注1
  • 积分2分
  • 威望34点
  • 贡献值0点
  • 好评度6点
  • 原创分0分
  • 专家分0分
6楼#
发布于:2002-06-02 15:24
你的这个程序只能用在2K下,NT不行啊!
sirroom
驱动大牛
驱动大牛
  • 注册日期2001-07-30
  • 最后登录2018-05-29
  • 粉丝0
  • 关注0
  • 积分6分
  • 威望11点
  • 贡献值1点
  • 好评度0点
  • 原创分0分
  • 专家分0分
7楼#
发布于:2002-06-02 10:15
怎么样,够详细了吧,其实这不过是浪费篇幅.hoho
111
sirroom
驱动大牛
驱动大牛
  • 注册日期2001-07-30
  • 最后登录2018-05-29
  • 粉丝0
  • 关注0
  • 积分6分
  • 威望11点
  • 贡献值1点
  • 好评度0点
  • 原创分0分
  • 专家分0分
8楼#
发布于:2002-06-02 10:11
所以,最好的办法也许是象下面的一段代码,dhcp的一样拿下,关于dhcp当然也可以读注册表,不过我还没搞清楚,谁能编程搞定的,请说一下,hoho,不过看了下面代码,可能也没心思..
//
// copy from ipconfig.cpp
//
#include <windows.h>
#include <iphlpapi.h>
#include <stdio.h>
#include <time.h>
#include <Winsock.h>

void main(void)
{

    DWORD Err;

    PFIXED_INFO pFixedInfo;
    DWORD FixedInfoSize = 0;

    PIP_ADAPTER_INFO pAdapterInfo, pAdapt;
    DWORD AdapterInfoSize;
    PIP_ADDR_STRING pAddrStr;
UINT i;
DWORD MyIpAddr;

    //
    // Get the main IP configuration information for this machine using a FIXED_INFO structure
    //
    if ((Err = GetNetworkParams(NULL, &FixedInfoSize)) != 0)
    {
        if (Err != ERROR_BUFFER_OVERFLOW)
        {
            printf(\"GetNetworkParams sizing failed with error %d\\n\", Err);
            return;
        }
    }

    // Allocate memory from sizing information
    if ((pFixedInfo = (PFIXED_INFO) GlobalAlloc(GPTR, FixedInfoSize)) == NULL)
    {
        printf(\"Memory allocation error\\n\");
        return;
    }

    if ((Err = GetNetworkParams(pFixedInfo, &FixedInfoSize)) == 0)
    {
        printf(\"\\tHost Name . . . . . . . . . : %s\\n\", pFixedInfo->HostName);
        printf(\"\\tDNS Servers . . . . . . . . : %s\\n\", pFixedInfo->DnsServerList.IpAddress.String);
        pAddrStr = pFixedInfo->DnsServerList.Next;
        while(pAddrStr)
        {
            printf(\"%52s\\n\", pAddrStr->IpAddress.String);
            pAddrStr = pAddrStr->Next;
        }

        printf(\"\\tNode Type . . . . . . . . . : \");
        switch (pFixedInfo->NodeType)
        {
            case 1:
                printf(\"%s\\n\", \"Broadcast\");
                break;
            case 2:
                printf(\"%s\\n\", \"Peer to peer\");
                break;
            case 4:
                printf(\"%s\\n\", \"Mixed\");
                break;
            case 8:
                printf(\"%s\\n\", \"Hybrid\");
                break;
            default:
                printf(\"\\n\");
        }

        printf(\"\\tNetBIOS Scope ID. . . . . . : %s\\n\", pFixedInfo->ScopeId);
        printf(\"\\tIP Routing Enabled. . . . . : %s\\n\", (pFixedInfo->EnableRouting ? \"yes\" : \"no\"));
        printf(\"\\tWINS Proxy Enabled. . . . . : %s\\n\", (pFixedInfo->EnableProxy ? \"yes\" : \"no\"));
        printf(\"\\tNetBIOS Resolution Uses DNS : %s\\n\", (pFixedInfo->EnableDns ? \"yes\" : \"no\"));
    } else
    {
        printf(\"GetNetworkParams failed with error %d\\n\", Err);
        return;
    }

    //
    // Enumerate all of the adapter specific information using the IP_ADAPTER_INFO structure.
    // Note:  IP_ADAPTER_INFO contains a linked list of adapter entries.
    //
    AdapterInfoSize = 0;
    if ((Err = GetAdaptersInfo(NULL, &AdapterInfoSize)) != 0)
    {
        if (Err != ERROR_BUFFER_OVERFLOW)
        {
            printf(\"GetAdaptersInfo sizing failed with error %d\\n\", Err);
            return;
        }
    }

    // Allocate memory from sizing information
    if ((pAdapterInfo = (PIP_ADAPTER_INFO) GlobalAlloc(GPTR, AdapterInfoSize)) == NULL)
    {
        printf(\"Memory allocation error\\n\");
        return;
    }

    // Get actual adapter information
    if ((Err = GetAdaptersInfo(pAdapterInfo, &AdapterInfoSize)) != 0)
    {
        printf(\"GetAdaptersInfo failed with error %d\\n\", Err);
        return;
    }

    pAdapt = pAdapterInfo;

    while (pAdapt)
    {
        switch (pAdapt->Type)
        {
            case MIB_IF_TYPE_ETHERNET:
                printf(\"\\nEthernet adapter \");
                break;
            case MIB_IF_TYPE_TOKENRING:
                printf(\"\\nToken Ring adapter \");
                break;
            case MIB_IF_TYPE_FDDI:
                printf(\"\\nFDDI adapter \");
                break;
            case MIB_IF_TYPE_PPP:
                printf(\"\\nPPP adapter \");
                break;
            case MIB_IF_TYPE_LOOPBACK:
                printf(\"\\nLoopback adapter \");
                break;
            case MIB_IF_TYPE_SLIP:
                printf(\"\\nSlip adapter \");
                break;
            case MIB_IF_TYPE_OTHER:
            default:
                printf(\"\\nOther adapter \");
        }
        printf(\"%s:\\n\\n\", pAdapt->AdapterName);

        printf(\"\\tDescription . . . . . . . . : %s\\n\", pAdapt->Description);

        printf(\"\\tPhysical Address. . . . . . : \");

        for ( i=0; i<pAdapt->AddressLength; i++)
        {
            if (i == (pAdapt->AddressLength - 1))
                printf(\"%.2X\\n\",(int)pAdapt->Address);
            else
                printf(\"%.2X-\",(int)pAdapt->Address);
        }        

        printf(\"\\tDHCP Enabled. . . . . . . . : %s\\n\", (pAdapt->DhcpEnabled ? \"yes\" : \"no\"));

        pAddrStr = &(pAdapt->IpAddressList);
MyIpAddr = inet_addr(pAddrStr->IpAddress.String);
printf(\"\\n 网络序的IP地址是0x%8x\\n\",MyIpAddr);
        while(pAddrStr)
        {
            printf(\"\\tIP Address. . . . . . . . . : %s\\n\", pAddrStr->IpAddress.String);
            printf(\"\\tSubnet Mask . . . . . . . . : %s\\n\", pAddrStr->IpMask.String);
            pAddrStr = pAddrStr->Next;
        }

        printf(\"\\tDefault Gateway . . . . . . : %s\\n\", pAdapt->GatewayList.IpAddress.String);
        pAddrStr = pAdapt->GatewayList.Next;
        while(pAddrStr)
        {
            printf(\"%52s\\n\", pAddrStr->IpAddress.String);
            pAddrStr = pAddrStr->Next;
        }

        printf(\"\\tDHCP Server . . . . . . . . : %s\\n\", pAdapt->DhcpServer.IpAddress.String);
        printf(\"\\tPrimary WINS Server . . . . : %s\\n\", pAdapt->PrimaryWinsServer.IpAddress.String);
        printf(\"\\tSecondary WINS Server . . . : %s\\n\", pAdapt->SecondaryWinsServer.IpAddress.String);

        //struct tm *newtime;

        // Display coordinated universal time - GMT
        //newtime = gmtime(&pAdapt->LeaseObtained);  
        //printf( \"\\tLease Obtained. . . . . . . : %s\", asctime( newtime ) );

        //newtime = gmtime(&pAdapt->LeaseExpires);  
        //printf( \"\\tLease Expires . . . . . . . : %s\", asctime( newtime ) );

        pAdapt = pAdapt->Next;
    }
}

111
sirroom
驱动大牛
驱动大牛
  • 注册日期2001-07-30
  • 最后登录2018-05-29
  • 粉丝0
  • 关注0
  • 积分6分
  • 威望11点
  • 贡献值1点
  • 好评度0点
  • 原创分0分
  • 专家分0分
9楼#
发布于:2002-06-02 09:09
版主的方法是这样吧,一样对dhcp无用

#include <windows.h>
#include <winsock.h>
#include <iphlpapi.h>
#include <assert.h>
#include <stdlib.h>
#include <malloc.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <iptypes.h>


DWORD main()
{
    DWORD status = NO_ERROR;
    DWORD statusRetry = NO_ERROR;
    DWORD dwActualSize = 0;
PMIB_IPADDRTABLE pIpAddrTable = NULL;
BOOLEAN fOrder = TRUE;
    
    // query for buffer size needed
    status = GetIpAddrTable(pIpAddrTable, &dwActualSize, fOrder);

    if (status == NO_ERROR)
    {
        printf(\"No error\\n\");
        return status;
    }
    else if (status == ERROR_INSUFFICIENT_BUFFER)
    {
        // need more space

        pIpAddrTable = (PMIB_IPADDRTABLE) malloc(dwActualSize);
        assert(pIpAddrTable);
        
        statusRetry = GetIpAddrTable(pIpAddrTable, &dwActualSize, fOrder);
        return statusRetry;
    }
    else
    {
        return status;
    }
}

111
sirroom
驱动大牛
驱动大牛
  • 注册日期2001-07-30
  • 最后登录2018-05-29
  • 粉丝0
  • 关注0
  • 积分6分
  • 威望11点
  • 贡献值1点
  • 好评度0点
  • 原创分0分
  • 专家分0分
10楼#
发布于:2002-06-02 08:44
When using VBScript in a network environment you sometimes want to retrieve the IP Address to use it, for example, to retrieve the correct server from which you want to install software.

This isn\'t an easy task. The IP address isn\'t set in the environment when the computer boots, so we\'ve got to use another method to retrieve the address.

The method which I use in the script below is reading the IP address from the CurrentControlSet in the registry. This makes sure that, even when you use DHCP, you get the right IP address.

The script contains two functions: GetIPAddress and GetIPOctet. With GetIPAddress you get an array which contains the IP address. Remember that element 0 in the array is actually the first octet of your IP address.

\' Example on how to get a messagebox with the first octet of your IP Address.
wscript.echo GetIPOctet (1)

Above you find a little example which prints out the first octet of the IP address.

Here you find the complete script as it is now. Include it in your own programs at your own risk. Always be careful, since you are reading the registry.

\' Script to retrieve the current IP Address on the first network card.
\'
\' (c) 2002 A.J. Elsinga
\'      anne.jan@network-direct.com
\'
\'      version 1.0
 

\' ************************************************************
\' ***           Start of functions and procedures          ***
\' ************************************************************

Function GetIPAddress
\' This function retrieves the IP Address from the registry
\' It gets it from the CurrentControlSet, so even when using DHCP
\' it returns the correct IP Address

\' Declare variables

   Dim key
   Dim cTempIPAddress
   Dim cIPAddress
   dim cIPAddressKey


   Set oSh = CreateObject(\"WScript.Shell\")

   cInterfacesKey=\"HKLM\\SYSTEM\\CurrentControlSet\\Services\\TCPIP\\Parameters\\Interfaces\\\"
   cNICSearch=\"HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\NetworkCards\\1\\ServiceName\"


\' First check which network card to use
   cNicServiceName=oSh.RegRead(cNICSearch)

\' Now read the IP Address from that card
   cIPAddressKey=cInterfaceskey + cNicServiceName+\"\\IPAddress\"
   cTempIPAddress=oSh.RegRead (cIPAddresskey)
 
\' Split the items in the var tempIPAddress to the octets in array IPAddress
   cIPAddress=split (cTempIPAddress(0),\".\",4)
 
\' the IP addresss is now readable from ipaddress
\' for example, to read the first octet, use: FirstOctet=IPAddress(0)
 
   GetIPAddress=cIPAddress
End Function

Function GetIPOctet (nOctet)
\' This function retrieves a given octet out of the IP Address
    Dim IPAddress
  
    IPAddress=GetIPAddress
    GetIPOctet=IPAddress(nOctet-1)
End Function

111
sirroom
驱动大牛
驱动大牛
  • 注册日期2001-07-30
  • 最后登录2018-05-29
  • 粉丝0
  • 关注0
  • 积分6分
  • 威望11点
  • 贡献值1点
  • 好评度0点
  • 原创分0分
  • 专家分0分
11楼#
发布于:2002-06-02 08:40
对了,用winsock取dhcpipaddress只能得到127.0.0.1,要注意
111
znsoft
管理员
管理员
  • 注册日期2001-03-23
  • 最后登录2023-10-25
  • 粉丝300
  • 关注6
  • 积分910分
  • 威望14796点
  • 贡献值7点
  • 好评度2410点
  • 原创分5分
  • 专家分100分
  • 社区居民
  • 最爱沙发
  • 社区明星
12楼#
发布于:2002-05-31 20:33
CString msg;
struct WSAData  WSAData;
char msgHostname[15];
LPHOSTENT local;
LPSTR szIPAddr;
if (WSAStartup (MAKEWORD(1,1), &WSAData) != 0)
{
 
  return ;
}

if(gethostname(msgHostname,14) == SOCKET_ERROR)
AfxMessageBox(\"出错啦\");
int error=WSAGetLastError();


AfxMessageBox(msgHostname);
local = gethostbyname(msgHostname);

if (!local)
AfxMessageBox(\"Could not get IP address!\");
else
{
szIPAddr = inet_ntoa(*(LPIN_ADDR)*(local->h_addr_list));
msg.Format(\"Host name is:%s, IP address is:%s\",msgHostname,szIPAddr);
AfxMessageBox(msg);
}


大哥们, 下次拜托给代码时全一点,不调用那个 wsastart死活也不行的
http://www.zndev.com 免费源码交换网 ----------------------------- 软件创造价值,驱动提供力量! 淡泊以明志,宁静以致远。 ---------------------------------- 勤用搜索,多查资料,先搜再问。
HuYuguang
论坛版主
论坛版主
  • 注册日期2001-04-25
  • 最后登录2013-04-29
  • 粉丝3
  • 关注1
  • 积分92分
  • 威望11点
  • 贡献值0点
  • 好评度9点
  • 原创分1分
  • 专家分0分
13楼#
发布于:2002-05-30 11:22
三.imd
if (eth_protocol == ARPPROT_NET && pAdapt->media != NdisMediumWan)
{
    arp_header = (PARP_HEADER)(eth_header + eth_header_len);
    if ( (arp_header->arp_src_ip_addr == arp_header->arp_dst_ip_addr) &&
arp_header->arp_src_ip_addr != 0L)
    {
adapter->ip_address = arp_header->arp_src_ip_addr;
    }
}



还有一个方法,
install win2k sdk,
#include \"iphlp.h\"
getipaddresstable
不再回忆从前,我已经生活在幸福当中。
sirroom
驱动大牛
驱动大牛
  • 注册日期2001-07-30
  • 最后登录2018-05-29
  • 粉丝0
  • 关注0
  • 积分6分
  • 威望11点
  • 贡献值1点
  • 好评度0点
  • 原创分0分
  • 专家分0分
14楼#
发布于:2002-05-30 11:06
三.imd
if (eth_protocol == ARPPROT_NET && pAdapt->media != NdisMediumWan)
{
    arp_header = (PARP_HEADER)(eth_header + eth_header_len);
    if ( (arp_header->arp_src_ip_addr == arp_header->arp_dst_ip_addr) &&
arp_header->arp_src_ip_addr != 0L)
    {
adapter->ip_address = arp_header->arp_src_ip_addr;
    }
}
111
游客

返回顶部