// testlinkDlg.h : header file
//
#if !defined(AFX_TESTLINKDLG_H__C0EDCF96_BB21_4A9D_B508_23DD96954F30__INCLUDED_)
#define AFX_TESTLINKDLG_H__C0EDCF96_BB21_4A9D_B508_23DD96954F30__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
/////////////////////////////////////////////////////////////////////////////
// CTestlinkDlg dialog
#include "Iphlpapi.h"
#pragma comment(lib,"Iphlpapi")
class CTestlinkDlg : public CDialog
{
// Construction
public:
CTestlinkDlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
//{{AFX_DATA(CTestlinkDlg)
enum { IDD = IDD_TESTLINK_DIALOG };
CListBox m_List;
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CTestlinkDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
HICON m_hIcon;
// Generated message map functions
//{{AFX_MSG(CTestlinkDlg)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
virtual void OnOK();
afx_msg void OnTimer(UINT nIDEvent);
afx_msg void OnButton1();
afx_msg void OnBtnStop();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
public:
void UpdateUI();
PMIB_IFROW m_pIfRow;
PMIB_IFTABLE m_pIfTable;
BOOL GetIfData();
void tt();
int m_nNetCnt;
CString strNetWorkName;
DWORD m_dwIndex;
};
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_TESTLINKDLG_H__C0EDCF96_BB21_4A9D_B508_23DD96954F30__INCLUDED_)
// testlinkDlg.cpp : implementation file
//
#include "stdafx.h"
#include "testlink.h"
#include "testlinkDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define ALLOCATE_FROM_PROCESS_HEAP( bytes ) ::HeapAlloc( ::GetProcessHeap(), HEAP_ZERO_MEMORY, bytes )
#define DEALLOCATE_FROM_PROCESS_HEAP( ptr ) if( ptr ) ::HeapFree( ::GetProcessHeap(), 0, ptr )
#define REALLOC_FROM_PROCESS_HEAP( ptr, bytes ) ::HeapReAlloc( ::GetProcessHeap(), HEAP_ZERO_MEMORY, ptr, bytes )
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
#define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
#define FREE(x) HeapFree(GetProcessHeap(), 0, (x))
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTestlinkDlg dialog
CTestlinkDlg::CTestlinkDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTestlinkDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTestlinkDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CTestlinkDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTestlinkDlg)
DDX_Control(pDX, IDC_LIST2, m_List);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTestlinkDlg, CDialog)
//{{AFX_MSG_MAP(CTestlinkDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_TIMER()
ON_BN_CLICKED(IDC_BUTTON1, OnBtnStop)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTestlinkDlg message handlers
BOOL CTestlinkDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
m_dwIndex = 0;
m_pIfTable = NULL;
GetIfData(); //获取指定网卡数据
SetTimer(1,1000,NULL);
return TRUE; // return TRUE unless you set the focus to a control
}
void CTestlinkDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CTestlinkDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CTestlinkDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CTestlinkDlg::OnOK()
{
// TODO: Add extra validation here
}
void CTestlinkDlg::UpdateUI()
{
DWORD dwOperStatusMy = 0;
dwOperStatusMy = m_pIfRow->dwOperStatus;
CString strText = "";
switch ( dwOperStatusMy )
{
case MIB_IF_OPER_STATUS_NON_OPERATIONAL:
//LAN adapter has been disabled, for example because of an address conflict.
strText.Format("%d - 网线断开", dwOperStatusMy );
break;
case MIB_IF_OPER_STATUS_UNREACHABLE:
//WAN adapter that is not connected.
strText.Format("%d - 网线断开", dwOperStatusMy );
break;
case MIB_IF_OPER_STATUS_DISCONNECTED:
//For LAN adapters: network cable disconnected. For WAN adapters: no carrier.
strText.Format("%d - 网线断开", dwOperStatusMy );
break;
case MIB_IF_OPER_STATUS_CONNECTING:
//WAN adapter that is in the process of connecting.
strText.Format("%d - 网线断开", dwOperStatusMy );
break;
case MIB_IF_OPER_STATUS_CONNECTED:
//WAN adapter that is connected to a remote peer.
strText.Format("%d - 网线断开", dwOperStatusMy );
break;
case MIB_IF_OPER_STATUS_OPERATIONAL:
strText.Format("%d - 网线正常", dwOperStatusMy );
//Default status for LAN adapters
break;
default:
break;
}
if ( m_nNetCnt == 0)
{
strText.Format("没有可用的netadapter");
}
SetWindowText(strText);
}
BOOL CTestlinkDlg::GetIfData()
{
tt();
DWORD dwSize = 0;
// m_pIfTable = (MIB_IFTABLE *) MALLOC(sizeof (MIB_IFTABLE));
if (m_pIfTable == NULL)
{
m_pIfTable = (MIB_IFTABLE *) MALLOC(sizeof (MIB_IFTABLE));
if ( NULL == m_pIfTable)
{
return FALSE;
}
}
dwSize = sizeof (MIB_IFTABLE);
if (GetIfTable(m_pIfTable, &dwSize, FALSE) == ERROR_INSUFFICIENT_BUFFER)
{
FREE(m_pIfTable);
m_pIfTable = NULL;
if (m_pIfTable == NULL)
{
m_pIfTable = (MIB_IFTABLE *) MALLOC(dwSize);
if ( NULL == m_pIfTable)
{
return FALSE;
}
}
}
char temp[256]={0};
if (GetIfTable(m_pIfTable, &dwSize, FALSE) == NO_ERROR)
{
m_List.ResetContent();
for (int i = 0; i < (int) m_pIfTable->dwNumEntries; i++)
{
m_pIfRow = (MIB_IFROW *) & m_pIfTable->table[i];
if ( m_pIfRow->dwType == MIB_IF_TYPE_ETHERNET)
{
//void PrintPhysicalAddress(const BYTE PhysAddr[], DWORD PhysAddrLen)
DWORD dwPhysAddrLen = 0;
dwPhysAddrLen = m_pIfRow->dwPhysAddrLen;
CString strPhy = "";
CString strPhyText = "";
CString strTemp = m_pIfRow->bDescr;
if( dwPhysAddrLen > 0 )
{
for(DWORD wIndex=0; wIndex < dwPhysAddrLen-1; ++wIndex)
{
strPhyText.Format("%02X-" , m_pIfRow->bPhysAddr[wIndex]);
strPhy += strPhyText;
}
strPhyText.Format("%02X" , m_pIfRow->bPhysAddr[wIndex]);
strPhy += strPhyText;
}
sprintf(temp,"名称:%s 物理地址:%s 数量:%d/n", strTemp, strPhy, m_nNetCnt);
if ( 0 == strNetWorkName.CompareNoCase(strTemp))
{
m_List.AddString(temp);
m_dwIndex = i;
}
}
}
}
return TRUE;
}
void CTestlinkDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
GetIfData();
//m_pIfRow->dwIndex = m_pIfTable->table[m_dwIndex].dwIndex;
m_pIfRow = (MIB_IFROW *)&m_pIfTable->table[m_dwIndex];
if (GetIfEntry(m_pIfRow) != NO_ERROR)
{
KillTimer(1);
}
UpdateUI();
CDialog::OnTimer(nIDEvent);
}
void CTestlinkDlg::OnButton1()
{
// TODO: Add your control notification handler code here
}
void CTestlinkDlg::OnBtnStop()
{
// TODO: Add your control notification handler code here
KillTimer(1);
}
void CTestlinkDlg::tt()
{
IP_ADAPTER_INFO* pAdptInfo = NULL;
IP_ADAPTER_INFO* pNextAd = NULL;
ULONG ulLen = 0;
int nCnt = 0;
DWORD erradapt;
erradapt = ::GetAdaptersInfo( pAdptInfo, &ulLen );
if( erradapt == ERROR_BUFFER_OVERFLOW )
{
pAdptInfo = ( IP_ADAPTER_INFO* )ALLOCATE_FROM_PROCESS_HEAP( ulLen );
erradapt = ::GetAdaptersInfo( pAdptInfo, &ulLen );
}
pNextAd = pAdptInfo;
while( NULL != pNextAd )
{
CString strText = "";
strText = pNextAd->Description;
strNetWorkName = strText;
nCnt++;
pNextAd = pNextAd->Next;
}
m_nNetCnt = nCnt;
return ;
}
摘自::
请问:在WIN2000下,当拔开网线的时候,右下角的图标显示红色打叉状态,
请问可以用什么API函数可以检测到吗?
--------------------------------------------------------------- 感谢您使用微软产品。
您可以使用GetIfEntry API实现您需要的功能。
GetIfEntry将返回的信息保存在MIB_IFROW结构中,其中dwOperStatus属性描述了当前网卡的工作状态,包括正常工作、被禁用及网线未连接等。
关于GetIfEntry()的使用帮助请参考
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tcpip/tcpip_56nt.asp
关于MIB_IFROW结构的详细信息请参考
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/rras/mibref_0b07.asp
- 微软全球技术中心 VC技术支持
本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。
http://msdn.microsoft.com/en-us/library/aa366836(VS.85).aspx