vc++ Windows 上的 ipconfig cmd 等效项

发布于 2024-08-15 23:56:21 字数 67 浏览 2 评论 0原文

如何在VC++中实现Windows上ipconfig给出的功能?我需要获取机器的本地 ip 信息、主 ip 与其他 ip

How can I achieve the functionality given in ipconfig on Windows in VC++? I need to get local ip info of the machine, primary ip vs.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏末的微笑 2024-08-22 23:56:21

好吧,人们,以前我找不到有用的东西,但我在 此链接上找到了解决方案

// GetLocalIP.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <winsock2.h>

int _tmain(int argc, _TCHAR* argv[])
{
    // Add 'ws2_32.lib' to your linker options

    WSADATA WSAData;

    // Initialize winsock dll
    if(::WSAStartup(MAKEWORD(1, 0), &WSAData))
    {
        // Error handling
    }

    // Get local host name
    char szHostName[128] = "";

    if(::gethostname(szHostName, sizeof(szHostName)))
    {
        // Error handling -> call 'WSAGetLastError()'
    }

    // Get local IP addresses
    struct sockaddr_in SocketAddress;
    struct hostent     *pHost        = 0;

    pHost = ::gethostbyname(szHostName);
    if(!pHost)
    {
        // Error handling -> call 'WSAGetLastError()'
    }

    char aszIPAddresses[10][16]; // maximum of ten IP addresses

    for(int iCnt = 0; ((pHost->h_addr_list[iCnt]) && (iCnt < 10)); ++iCnt)
    {
        memcpy(&SocketAddress.sin_addr, pHost->h_addr_list[iCnt], pHost->h_length);
        strcpy(aszIPAddresses[iCnt], inet_ntoa(SocketAddress.sin_addr));
        //std::cout << aszIPAddresses[iCnt] << endln;
    }

    // Cleanup
    WSACleanup();

    return 0;
}

Well people, formerly I could not find something useful but I found the solution on this link.

// GetLocalIP.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <winsock2.h>

int _tmain(int argc, _TCHAR* argv[])
{
    // Add 'ws2_32.lib' to your linker options

    WSADATA WSAData;

    // Initialize winsock dll
    if(::WSAStartup(MAKEWORD(1, 0), &WSAData))
    {
        // Error handling
    }

    // Get local host name
    char szHostName[128] = "";

    if(::gethostname(szHostName, sizeof(szHostName)))
    {
        // Error handling -> call 'WSAGetLastError()'
    }

    // Get local IP addresses
    struct sockaddr_in SocketAddress;
    struct hostent     *pHost        = 0;

    pHost = ::gethostbyname(szHostName);
    if(!pHost)
    {
        // Error handling -> call 'WSAGetLastError()'
    }

    char aszIPAddresses[10][16]; // maximum of ten IP addresses

    for(int iCnt = 0; ((pHost->h_addr_list[iCnt]) && (iCnt < 10)); ++iCnt)
    {
        memcpy(&SocketAddress.sin_addr, pHost->h_addr_list[iCnt], pHost->h_length);
        strcpy(aszIPAddresses[iCnt], inet_ntoa(SocketAddress.sin_addr));
        //std::cout << aszIPAddresses[iCnt] << endln;
    }

    // Cleanup
    WSACleanup();

    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文