简体   繁体   English

如何使用C ++对Windows计算机上的远程计算机执行ping操作?

[英]How can i ping remote computer on my Windows computer with c++?

I used below code. 我用下面的代码。 It works, but in debug mode in Visual Studio if you stop the debug the computer gave blue screen so it is not useful. 它可以工作,但是在Visual Studio的调试模式下,如果停止调试,则计算机会显示蓝屏,因此它没有用。 I did some research and i found this is a common bug for icmpapi. 我做了一些研究,发现这是icmpapi的常见错误。 Is there a any way to ping computer c++? 有没有办法ping计算机c ++?

        #include <winsock2.h>
        #include <iphlpapi.h>
        #include <icmpapi.h>

        //Declare and initialize variables
        HANDLE hIcmpFile;
        unsigned long ipaddr = INADDR_NONE;
        DWORD dwRetVal       = 0;
        DWORD dwError        = 0;
        char SendData[]      = "Data Buffer";
        LPVOID ReplyBuffer   = NULL;
        DWORD ReplySize      = 0;

        QByteArray ipArray   = computerIt->GetIP().toLocal8Bit();
        const char *hostIP   = ipArray.data();

        ipaddr = inet_addr(hostIP);
        if ( ipaddr == INADDR_NONE ) 
        {
            EventLogger::LogMessage(true, "<%s> Computer in <%s> Computer Group, IP initialization failed. (inet_addr(hostIP))", computerIt->GetName().toUtf8().constData(), computerGroupIt->GetName().toUtf8().constData());
            break;
        }

        hIcmpFile = IcmpCreateFile();
        if ( hIcmpFile == INVALID_HANDLE_VALUE )
        {
            EventLogger::LogMessage(true, "<%s> Computer in <%s> Computer Group, IcmpCreatefile returned error", computerIt->GetName().toUtf8().constData(), computerGroupIt->GetName().toUtf8().constData());
            break;
        }

        // Allocate space for at a single reply
        ReplySize = sizeof (ICMP_ECHO_REPLY) + sizeof (SendData) + 8;
        ReplyBuffer = (VOID *) malloc(ReplySize);
        if ( ReplyBuffer == NULL )
        {
            EventLogger::LogMessage(true, "<%s> Computer in <%s> Computer Group, unable to allocate memory for reply buffer", computerIt->GetName().toUtf8().constData(), computerGroupIt->GetName().toUtf8().constData());
            break;
        }

        // Starting pinging
        dwRetVal = IcmpSendEcho2(hIcmpFile, NULL, NULL, NULL,
                                ipaddr, SendData, sizeof (SendData), NULL,
                                ReplyBuffer, ReplySize, 1000);
        if ( dwRetVal == 0 )
        {
            computerIt->SetAliveStatus(false);
        }
        else
        {
            computerIt->SetAliveStatus(true);
        }

In the above code there is bug in IcmpSendEcho2 method! 在上面的代码中,IcmpSendEcho2方法中有错误! It is only seen in debug mode! 仅在调试模式下可见! (See the link) https://msdn.microsoft.com/en-us/library/windows/desktop/aa366050(v=vs.85).aspx (请参阅链接) https://msdn.microsoft.com/zh-cn/library/windows/desktop/aa366050 ( v= vs.85) .aspx

I used raw ping methods for the ping operation. 我对ping操作使用了原始ping方法。 It is much slower but it is safety! 它慢得多,但是很安全!

You can check raw ping method: http://tangentsoft.net/wskfaq/examples/rawping.html 您可以检查原始ping方法: http : //tangentsoft.net/wskfaq/examples/rawping.html

be careful. 小心。 at: 在:

// https://msdn.microsoft.com/en-us/library/windows/desktop/aa366050(v=vs.85).aspx // https://msdn.microsoft.com/zh-CN/library/windows/desktop/aa366050(v=vs.85).aspx

the code works , BUT is missing an IMPORTANT POINT if you paste/copy this code: MISSING free(ReplyBuffer) 该代码有效,但是如果粘贴/复制此代码,则缺少重要点:MISSING free(ReplyBuffer)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Eclipse中的远程计算机上构建c ++项目? - How to build a c++ project on a remote computer in Eclipse? 在C ++ / Windows中,如何获取我所在计算机的网络名称? - In C++/Windows how do I get the network name of the computer I'm on? 如何从C ++中的Windows注册表中获取计算机制造商和型号? - How to get computer manufacturer and model from windows registry in C++? 如何检查(在C / C ++代码中)我的计算机上是否安装了Openssl? - How to check (in C/C++ code) if Openssl is installed on my computer? 从C ++如何在我的计算机(而非窗口)上的桌面或Word文档中键入字符串 - from C++ how do I type a string to my desktop or word document on my computer (not window) 如何使用C终止远程计算机上的进程? - How do I use C to terminate a process on a remote computer? 通过C ++检查以太网上的Windows计算机 - Check if Windows Computer on Ethernet Via C++ 我的 C++ 程序如何访问计算机的日期和时间以仅打印和使用当年? - How can my C++ program access the computer's date and time to print and use just the current year? 如何将计算机中的任何视频加载并显示到UWP(C ++) - How to load and display any video in my computer to a UWP (C++) C ++:了解我的计算机如何存储字节 - C++: Finding out how my computer stores bytes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM