我如何用c++在我的Windows计算机上ping远程计算机
How can i ping remote computer on my Windows computer with c++?
我使用了下面的代码。它可以工作,但是在Visual Studio调试模式下,如果你停止调试,计算机会给出蓝屏,所以它是没有用的。我做了一些研究,发现这是icmpapi的一个常见错误。有什么办法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);
}
小心。:
//https://msdn.microsoft.com/en-us/library/windows/desktop/aa366050 (v = vs.85) . aspx
代码可以工作,但是如果你粘贴/复制这段代码,就会错过重要的一点:失踪的自由(ReplyBuffer)
相关文章:
- 为什么使用 P/Invoke 调用 dll 时,某些计算机中的 LoadLibrary 失败?
- EvtExportLogneneneba API正在将远程计算机的事件日志保存到远程PC本身.如何将其保存到主机
- 为什么我能够为阵列分配比计算机实际拥有的内存更多的内存
- 我可以使用任何好的逻辑来阻止计算机将 O 放在井字游戏中的现有 X 上
- C++ 计算机猜测用户数量在 7 次猜测以内
- C++为什么我的编译器成功了,但我的计算机给出了调试错误?
- 在 c++ 中连接字符串和整数,以便在 C++ 11 不支持计算机的情况下读取多个文件
- 我的 SDL2 程序需要哪些二进制文件,以便它在另一台未安装 SDL2 的计算机中工作
- 将非常大的 int 转换为双倍,在某些计算机上会损失精度
- 如何在个人计算机和群集 (c++) 上生成相同的随机数
- 系统错误:程序无法启动,因为您的计算机中缺少MSVCP140D.DLL。尝试重新安装该程序以解决此问题
- 预处理的 C/C++ 文件是否特定于计算机?
- Windows桌面程序保存您的计算机会话 - 基于程序崩溃时的恢复会话
- 我会导致太多内存泄漏,以至于我的计算机无响应吗?
- 在多 GPU 计算机中查找空闲 GPU
- 我该如何编码,使计算机知道两个名称条目和三个名称条目之间的区别
- 从C++中的主机名获取计算机名
- 我的C++程序如何访问计算机的日期和时间以打印和使用当前年份?
- 如何使Visual Studio 2017 C++项目在计算机之间更具可移植性
- 我如何用c++在我的Windows计算机上ping远程计算机