首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

指针有关问题没有解决,再开新帖,麻烦诸位大神帮忙

2012-04-17 
指针问题没有解决,再开新帖,麻烦诸位大神帮忙[codeC/C++][/code]pcap_if_t *alldevsvoid packet_handler

指针问题没有解决,再开新帖,麻烦诸位大神帮忙
[code=C/C++][/code]pcap_if_t *alldevs;
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data);
DWORD WINAPI Loop(LPVOID lparam);
pcap_t *tests;

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
class CAboutDlg : public CDialog
{
public:
CAboutDlg();

//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA

// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX);  
//}}AFX_VIRTUAL


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
}

CSnifferDlg::CSnifferDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSnifferDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSnifferDlg)
m_rule = _T("");
m_disdata = _T("");
m_dischar = _T("");
//}}AFX_DATA_INIT
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CSnifferDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSnifferDlg)
DDX_Control(pDX, IDC_LIST, m_ctrlist);
DDX_Control(pDX, IDC_CMB_ADAPTER, m_adapter);
DDX_CBString(pDX, IDC_CB_PROTOCOL, m_rule);
DDX_Text(pDX, IDC_EDIT_DISDATA, m_disdata);
DDX_Text(pDX, IDC_EDIT_DISCHAR, m_dischar);
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSnifferDlg, CDialog)
//{{AFX_MSG_MAP(CSnifferDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_START, OnStart)
ON_BN_CLICKED(IDC_BTN_CANCEL, OnBtnCancel)
ON_NOTIFY(NM_CLICK, IDC_LIST, OnClickList)
ON_BN_CLICKED(IDC_BTN_STOP, OnBtnStop)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
BOOL CSnifferDlg::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);
}
}

SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
pcap_if_t *d;
int i=0;
char errbuf[PCAP_ERRBUF_SIZE];

  if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, &alldevs, errbuf) == -1)
  {
MessageBox(_T("获取网卡信息失败"));
return FALSE;
  }

  for(d=alldevs; d; d=d->next)
  {
m_adapter.InsertString(i,d->name);
i++;
  }
if(i==0)
  {
MessageBox(_T("No interfaces found!"));
  return FALSE;
  }


return TRUE; // return TRUE unless you set the focus to a control
}

void CSnifferDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
HCURSOR CSnifferDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}

void CSnifferDlg::OnStart()  
{
pcap_if_t *d;
int i = 0;
pcap_t *adhandle;
char errbuf[PCAP_ERRBUF_SIZE];
u_int netmask;
struct bpf_program fcode;




int Num = m_adapter.GetCurSel();
if(Num==CB_ERR)
{
MessageBox(_T("请选择网卡"));
return;
}

for(d=alldevs, i=0; i< Num ;d=d->next, i++);

  if ( (adhandle= pcap_open(d->name, // 设备名
65536, // 要捕捉的数据包的部分  
// 65535保证能捕获到不同数据链路层上的每个数据包的全部内容
PCAP_OPENFLAG_PROMISCUOUS, // 混杂模式
1000, // 读取超时时间
NULL, // 远程机器验证
errbuf // 错误缓冲池
) ) == NULL)
  {
MessageBox(_T("Unable to open the adapter"));
  pcap_freealldevs(alldevs);
free(adhandle);
  return;
  }
   

  if(d->addresses != NULL)

  netmask=((struct sockaddr_in *)(d->addresses->netmask))->sin_addr.S_un.S_addr;
  else
  netmask=0xffffff;  

UpdateData();
CString Rule;
if (m_rule == "ALL")
{
Rule="ip";
}
else if (m_rule == "TCP")
{
Rule="ip and tcp";
}

else
{
MessageBox(_T("请输入规则"));
return;
}
GetDlgItem(IDC_START)->EnableWindow(FALSE);
GetDlgItem(IDC_CMB_ADAPTER)->EnableWindow(FALSE);
GetDlgItem(IDC_CB_PROTOCOL)->EnableWindow(FALSE);

  if (pcap_compile(adhandle, &fcode, Rule.GetBuffer(0), 1, netmask) <0 )
  {
MessageBox(_T("Unable to compile the packet filter"));
  pcap_freealldevs(alldevs);
  return;
  }
   
  if (pcap_setfilter(adhandle, &fcode)<0)
  {
MessageBox(_T("Error setting the filter"));
  pcap_freealldevs(alldevs);
  return;
  }  



tests = adhandle;
HANDLE hThread = CreateThread(NULL, 0, Loop, (LPVOID)adhandle, NULL, NULL);
Sleep(1000);
if(hThread)  
CloseHandle(hThread);  

return;
}


DWORD WINAPI Loop(LPVOID lparam)
{
pcap_t *adhandle;
adhandle =(pcap_t *)lparam;
pcap_loop(adhandle, 0, packet_handler, NULL);
return 0;
}
调试了一下发现就是alldevs指针的错误,pcap_if_t结构体下的next,name,address全报错next = CXX0030: 错误: 无法计算表达式的值,name = CXX0030: 错误: 无法计算表达式的值,description = CXX0030: 错误: 无法计算表达式的值,flags = CXX0030: 错误: 无法计算表达式的值,求高手指导

[解决办法]
是否是 pcap_findalldevs_ex() 执行有异常?

不过这个“无法计算表达式的值”的调试报错,
通常是在非变量可见域内查看变量才会出现,
还没遇到你说的这个情况。

可以在watch窗口单独查看一下alldevs->next alldevs->name ......
[解决办法]
代码看起来好迷糊,可以把mfc自己生成的那些代码去掉再贴出来吧。

pcap_if_t *alldevs;

alldevs是一个全局指针变量,提示“错误: 无法计算表达式的值”是正常的吧。

热点排行