多线称中需要调用一个回调函数,回调函数如何控制主对话框中的控件
主对话框某按钮启动一个线程,线程中要调用一个回调函数,回调函数如何控制主对话框中的控件。
代码如下:
//启动线程
void CWinpCapDemoDlg::OnBtnCapture()
{
// TODO: Add your control notification handler code here
_beginthreadex(NULL,0,&CaptureFunc,this,0,&m_thread_id);//启动线程
}
//线程函数
unsigned int WINAPI CWinpCapDemoDlg::CaptureFunc(LPVOID param)
{
int count = 0;
CWinpCapDemoDlg* pDlg = (CWinpCapDemoDlg*)param;
hWnd = ::FindWindow(NULL,"WinpCapDemo" );
pcap_loop(adHandle,1,ETH_Dispatcher_Handler,NULL);//参数3必须为一个回调函数
}
//回调函数
void ETH_Dispatcher_Handler(u_char *, const pcap_pkthdr* header, const u_char *p)
{
PETHHEADER eth = (PETHHEADER)p;
CWinpCapDemoDlg* dlg = new CWinpCapDemoDlg;//发现句柄为空.所以无法操作主对话框的控件
//CWnd::GetDlgItem(IDD_WINPCAPDEMO_DIALOG,&hWnd);
CString strShost,strDhost,strType;
strShost.Format("%02X ",eth->ether_shost);
strDhost.Format("%02X ",eth->ether_dhost);
strType.Format("%02X",htons(eth->ether_type));
//发现句柄为空.所以无法操作主对话框的控件
// dlg.m_ctrlList.InsertItem(0,strShost);
// dlg.m_ctrlList.SetItemText(0,1,strDhost);
// dlg.m_ctrlList.SetItemText(0,2,strType);
}