📄 adapter.cpp
字号:
// Adapter.cpp: implementation of the CAdapter class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "sendpkt.h"
#include "Adapter.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CAdapter::CAdapter()
{
memset(m_AdapterList,0,MAX_NUM_ADAPTER*8192);
memset(m_PacketBuff,0,sizeof(m_PacketBuff));
m_lpAdapter=NULL;
m_lpPacket=NULL;
m_nAdapterNum=0;
m_CurFillPos=0;
}
CAdapter::~CAdapter()
{
}
BOOL CAdapter::Initialize()
{
int i;
DWORD dwVersion;
DWORD dwWindowsMajorVersion;
//UNICODE strings(winnt)
WCHAR AdapterName[8192]; //string that contains a list of the network adapters
WCHAR *temp,*temp1;
//ASCII strings(win95)
char AdapterNamea[8192]; //string that contains a list of the network adapters
char *tempa,*temp1a;
ULONG AdapterLength;
i=0;
//the data returned by PacketGetAdapterNames is different in Win95 and in WinNT.
//We have to check the os on which we are running
dwVersion=GetVersion();
dwWindowsMajorVersion=(DWORD)(LOBYTE(LOWORD(dwVersion)));
if (!(dwVersion >= 0x80000000 && dwWindowsMajorVersion >= 4))
{//Windows NT
AdapterLength=sizeof(AdapterName);
if(PacketGetAdapterNames((PTSTR)AdapterName,&AdapterLength)==FALSE)
{
AfxMessageBox("Unable to retrieve the list of the adapters!");
return FALSE;
}
//parse AdapterName
temp=AdapterName;
temp1=AdapterName;
while ((*temp!='\0')||(*(temp-1)!='\0'))
{
if (*temp=='\0')
{
//由于Windows2000的特殊原因,将UNICODE字符转化为MBCS字符,
//否则,列表框无法完整显示适配器名称。
WCHAR wstrTemp[4096];
memset(wstrTemp,0,sizeof(wstrTemp));
memcpy(wstrTemp,temp1,(temp-temp1)*2);
WideCharToMultiByte(CP_ACP,0,wstrTemp,-1,m_AdapterList[i],lstrlenW(wstrTemp),NULL,NULL);
temp1=temp+1;
i++;
}
temp++;
}
m_nAdapterNum=i;
}
else
{//windows 95
AdapterLength = sizeof(AdapterNamea);
if(PacketGetAdapterNames(AdapterNamea,&AdapterLength)==FALSE)
{
AfxMessageBox("Unable to retrieve the list of the adapters!");
return FALSE;
}
//parse AdapterNamea
tempa=AdapterNamea;
temp1a=AdapterNamea;
while ((*tempa!='\0')||(*(tempa-1)!='\0'))
{
if (*tempa=='\0')
{
memcpy(m_AdapterList[i],temp1a,tempa-temp1a);
temp1a=tempa+1;
i++;
}
tempa++;
}
m_nAdapterNum=i;
}
return TRUE;
}
BOOL CAdapter::Open(int AdapterNum)
{
m_lpAdapter=PacketOpenAdapter(m_AdapterList[AdapterNum]);
if(!m_lpAdapter||(m_lpAdapter->hFile==INVALID_HANDLE_VALUE))
{
CString strMsg;
DWORD dwErrorCode;
dwErrorCode=GetLastError();
strMsg.Format("Unable to open the driver, Error Code : %lx",dwErrorCode);
AfxMessageBox(strMsg);
return FALSE;
}
return TRUE;
}
BOOL CAdapter::AllocatePacket()
{
m_lpPacket=PacketAllocatePacket();
ASSERT(m_lpPacket!=NULL);
if(m_lpPacket==NULL)
{
CString strMsg;
DWORD dwErrorCode;
dwErrorCode=GetLastError();
strMsg.Format("Failed to allocate the LPPACKET structure,Error Code:%lx",dwErrorCode);
AfxMessageBox(strMsg);
return FALSE;
}
return TRUE;
}
void CAdapter::InitPacket()
{
BOOL ret;
ret=AllocatePacket();
ASSERT(ret);
PacketInitPacket(m_lpPacket,m_PacketBuff,m_CurFillPos);
}
BOOL CAdapter::SetNumWrites(int npacks)
{
BOOL retCode=FALSE;
retCode=PacketSetNumWrites(m_lpAdapter,npacks);
if(retCode==FALSE)
{
CString strMsg;
DWORD dwErrorCode;
dwErrorCode=GetLastError();
strMsg.Format("Unable to send more than one packet in a single write,Error Code:%lx",dwErrorCode);
AfxMessageBox(strMsg);
return FALSE;
}
return TRUE;
}
BOOL CAdapter::SendPacket(BOOL bSync)
{
ASSERT(m_lpAdapter!=NULL);
ASSERT(m_lpPacket!=NULL);
BOOL retCode;
retCode=PacketSendPacket(m_lpAdapter,m_lpPacket,bSync);
if(retCode==FALSE)
{
CString strMsg;
DWORD dwErrorCode;
dwErrorCode=GetLastError();
strMsg.Format("Error sending the packets!,Error Code:%lx",dwErrorCode);
AfxMessageBox(strMsg);
return FALSE;
}
return TRUE;
}
void CAdapter::FreePacket()
{
ASSERT(m_lpPacket!=NULL);
if(m_lpPacket!=NULL)
{
PacketFreePacket(m_lpPacket);
m_lpPacket=NULL;
}
}
void CAdapter::CloseAdapter()
{
ASSERT(m_lpAdapter!=NULL);
if(m_lpAdapter!=NULL)
{
PacketCloseAdapter(m_lpAdapter);
m_lpAdapter=NULL;
}
}
int CAdapter::GetAdapterNumber()
{
return m_nAdapterNum;
}
char* CAdapter::GetAdapterName(int nNum)
{
ASSERT(nNum<=m_nAdapterNum);
return m_AdapterList[nNum];
}
int CAdapter::GetCurFillPos()
{
return m_CurFillPos;
}
void CAdapter::SetCurFillPos(int nPos)
{
ASSERT(nPos>=0&&nPos<sizeof(m_PacketBuff));
m_CurFillPos=nPos;
}
void CAdapter::FillPacket(unsigned char *pPacket, int length)
{
ASSERT(pPacket!=NULL);
ASSERT(m_CurFillPos+length<sizeof(m_PacketBuff));
if(m_CurFillPos+length<sizeof(m_PacketBuff))
{
memcpy(&m_PacketBuff[m_CurFillPos],pPacket,length);
m_CurFillPos+=length;
}
else if(m_CurFillPos<sizeof(m_PacketBuff))
{
AfxMessageBox("填充长度太长");
memcpy(&m_PacketBuff[m_CurFillPos],pPacket,sizeof(m_PacketBuff)-m_CurFillPos);
m_CurFillPos=sizeof(m_PacketBuff);
}
else
ASSERT(FALSE);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -