===========================================
使用方法:
1、引入以下4个文件到项目:
ping.h
ping.cpp
PingThread.h
PingThread.cpp
2、在StdAfx.h中加入以下代码:
#include // MFC socket extensions
#define WM_MSG_STATUS WM_USER + 0x0100
#define WM_PING_END WM_USER + 0x0101
// string message allocator for posting messages between windows...
static char* AllocBuffer(CString strMsg)
{
int nLen = strMsg.GetLength();
char *pBuffer = new char[nLen+1];
strcpy(pBuffer,(const char*)strMsg);
ASSERT(pBuffer != NULL);
return pBuffer;
}
3、定义一个变量:
CPingThread m_pingThread;
4、定义两个消息响应(WM_MSG_STATUS, WM_PING_END):
LRESULT OnStatus(WPARAM wParam, LPARAM lParam);
LRESULT OnPingEnd(WPARAM wParam, LPARAM lParam);
5、调用:
m_pingThread.StartPing(5, strIPAddr, m_hWnd);
注1:参数5表示ping 5次,实际应用中随意;
6、分析结果
每ping 1次,OnStatus都会收到结果消息,如
失败返回:Request Timed Out
成功返回:Reply[n] from: 192.168.1.109: bytes=32 time=0ms TTL=64
注2:其中n 表示ping的索引(它不重要)
注3:可以在文件ping.cpp修改返回消息(void CPing::Ping(UINT nRetries,LPCSTR pstrHost,HWND hWnd))
1