#include <stdio.h>
#include <winsock2.h>
#include "Iphlpapi.h"
#pragma comment(lib,"iphlpapi.lib")
#pragma comment(lib,"wsock32.lib")void main()
{
int k;
char IP[16];
ULONG Mac=0,MacLen=0;
memset(IP,'\0',sizeof(IP));
while(1)
{
printf("Input the IP you wanna check:");
scanf("%s",IP);
k=SendARP(htonl(inet_addr(IP)),0,&Mac,&MacLen);
if(k==NO_ERROR)
printf("The IP has already existed!");
else
printf("SendARP failed\n");
}
}
输入我的内网IP,总是显示sendarp failed,请大侠指教。

解决方案 »

  1.   

    本帖最后由 wenxy1 于 2008-09-17 11:37:44 编辑
      

  2.   

      
      Platform SDK: Internet Protocol Helper 
    SendARP
    The SendARP function sends an ARP request to obtain the physical address that corresponds to the specified destination IP address.DWORD SendARP(
      IPAddr DestIP,     // destination IP address
      IPAddr SrcIP,      // IP address of sender
      PULONG pMacAddr,   // returned physical address
      PULONG PhyAddrLen  // length of returned physical addr.
    );
    Parameters
    DestIP 
    [in] Specifies the destination IP address. The ARP request attempts to obtain the physical address that corresponds to this IP address. 
    SrcIP 
    [in] Specifies the IP address of the sender. This parameter is optional. The caller may specify zero for the parameter. 
    pMacAddr 
    [out] Pointer to an array of ULONG variables. The first six bytes of this array receive the physical address that corresponds to the IP address specified by the DestIP parameter. 
    PhyAddrLen 
    [out] Pointer to a ULONG variable. This variable contains the length of the physical address pointed to by the pMacAddr parameter. 
    Return Values
    If the function succeeds, the return value is NO_ERROR.If the function fails, use FormatMessage to obtain the message string for the returned error.Res
    For information about the IPAddr data type, see Win32 Simple Data Types. To convert an IP address between dotted decimal notation and IPAddr format, use the inet_addr and inet_ntoa functions.Example Code
    The following code demonstrates how to obtain the media access control (MAC) address associated with a specified IP address.//
    // Link with ws2_32.lib and iphlpapi.lib
    //#include <windows.h>
    #include <stdio.h>
    #include <tchar.h>
    #include <iphlpapi.h>
    int __cdecl main()
    {
        HRESULT hr;
        IPAddr  ipAddr;
        ULONG   pulMac[2];
        ULONG   ulLen;    ipAddr = inet_addr ("216.145.25.31");
        memset (pulMac, 0xff, sizeof (pulMac));
        ulLen = 6;
        
        hr = SendARP (ipAddr, 0, pulMac, &ulLen);
        printf ("Return %08x, length %8d\n", hr, ulLen);
        
        size_t i, j;
        char * szMac = new char[ulLen*3];
        PBYTE pbHexMac = (PBYTE) pulMac;    //
        // Convert the binary MAC address into human-readable
        //
        for (i = 0, j = 0; i < ulLen - 1; ++i) {
            j += sprintf (szMac + j, "%02X:", pbHexMac[i]);
        }
        
        sprintf (szMac + j, "%02X", pbHexMac[i]);
        printf ("MAC address %s\n", szMac);
        
        delete [] szMac;    return 0;
    }
    Requirements 
      Windows NT/2000/XP: Included in Windows 2000; Windows XP Pro; and Windows .NET Server.
      Windows 95/98/Me: Unsupported.
      Header: Declared in Iphlpapi.h.
      Library: Use Iphlpapi.lib.See Also
    CreateIpNetEntry, DeleteIpNetEntry, FlushIpNetTable, SetIpNetEntry Platform SDK Release: August 2001  What did you think of this topic?
    Let us know.  Order a Platform SDK CD Online
    (U.S/Canada)   (International)  Requirements 
      Windows NT/2000/XP: Included in Windows 2000; Windows XP Pro; and Windows .NET Server.
      Windows 95/98/Me: Unsupported.
      Header: Declared in Iphlpapi.h.
      Library: Use Iphlpapi.lib.
    See Also
    CreateIpNetEntry, DeleteIpNetEntry, FlushIpNetTable, SetIpNetEntry 
      

  3.   

    我把那一句用你的替换掉之后,编译没通过,报错:
    error C2664: 'SendARP' : cannot convert parameter 3 from 'unsigned long' to 'unsigned long *'
    继续请教。
      

  4.   

    本帖最后由 wenxy1 于 2008-09-17 15:22:44 编辑
      

  5.   

    我换成传地址之后,编译通过后,输入我的局域网ip,还是显示sendarp failed。
    囧了。
      

  6.   

    1, 用超级用户登录windows.
    2, 关闭防火墙与防病毒软件。 
    3,严重怀疑你的代码有问题;
    void main()
    {
    int k;
    char IP[16];
    ULONG Mac=0,MacLen=0;
    memset(IP,'\0',sizeof(IP));
    while(1)
    {
    printf("Input the IP you wanna check:");
    scanf("%s",IP);
    k=SendARP(htonl(inet_addr(IP)),0,&Mac,&MacLen); //src IP,你设置成你的网卡的IP。
    if(k==NO_ERROR)
    printf("The IP has already existed!");
    else
    printf("SendARP failed, [Error=%d]\n", GetLastError()); // show error code, then check error descript by VC++6.0 tools which name is error lookup.
    }