MFC对话框OnInitDialog()中放入创建线程来实现端口监听,报错local function definitions are illegal

javaPie 2012-05-01 03:41:32
error C2601: 'ThreadFunc' : local function definitions are illegal

BOOL CUdprecvDlg::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
//************************************************************
UINT CUdprecvDlg::ThreadFunc(LPVOID pParam)
{
//线程函数实现

//启动SOCKET库,版本为2.0
WSAStartup(0x0202,&wsdata);
optval=TRUE;
//然后赋值给两个地址,一个用来绑定套接字,一个用来从网络上的广播地址接收消息;
a.sin_family=AF_INET;
a.sin_addr.s_addr=0;
a.sin_port=htons(5050);

from.sin_family=AF_INET;
from.sin_addr.s_addr=INADDR_BROADCAST;
from.sin_port=htons(5050);

int fromlength=sizeof(SOCKADDR);
//用UDP初始化套接字
s=socket(AF_INET,SOCK_DGRAM,0);
//设置该套接字为广播类型,
setsockopt(s,SOL_SOCKET,SO_BROADCAST,(char FAR *)&optval,sizeof(optval));
bind(s,(sockaddr *)&a,sizeof(sockaddr_in));
char buf[256];
CEdit * ce;
ce=(CEdit*)GetDlgItem(IDC_RecvMsg);
while(1)
{//从广播地址接收消息,注意用来绑定的地址和接收消息的地址是不一样的
recvfrom(s,buf,256,0,(struct sockaddr FAR *)&from,(int FAR *)&fromlength);

ce->SetWindowText(buf);
ZeroMemory(buf,256);
}

//*****************************************************************

}
CWinThread *m_pThread; //线程指针
m_pThread = AfxBeginThread(ThreadFunc, NULL); //启动线程
return TRUE; // return TRUE unless you set the focus to a control
}
...全文
283 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
javaPie 2012-05-01
  • 打赏
  • 举报
回复
就是静态的啊 这样申明的呀 static UINT ThreadFunc(LPVOID pParam);[Quote=引用 3 楼 的回复:]

ThreadFunc定义成静态成员函数
[/Quote]
oyljerry 2012-05-01
  • 打赏
  • 举报
回复
ThreadFunc定义成静态成员函数
javaPie 2012-05-01
  • 打赏
  • 举报
回复
放到oninit外出现大量错误ror C2228: left of '.S_un' must have class/struct/union type
F:\tempc\udprecv\udprecvDlg.cpp(267) : error C2228: left of '.S_addr' must have class/struct/union type
F:\tempc\udprecv\udprecvDlg.cpp(268) : error C2228: left of '.sin_port' must have class/struct/union type
F:\tempc\udprecv\udprecvDlg.cpp(270) : error C2228: left of '.sin_family' must have class/struct/union type
F:\tempc\udprecv\udprecvDlg.cpp(271) : error C2228: left of '.sin_addr' must have class/struct/union type
F:\tempc\udprecv\udprecvDlg.cpp(271) : error C2228: left of '.S_un' must have class/struct/union type
F:\tempc\udprecv\udprecvDlg.cpp(271) : error C2228: left of '.S_addr' must have class/struct/union type[Quote=引用 1 楼 的回复:]

不能在函数体内定义函数.
把UINT CUdprecvDlg::ThreadFunc(LPVOID pParam)放到OnInitDialog函数体外定义
[/Quote]
xiaohuh421 2012-05-01
  • 打赏
  • 举报
回复
不能在函数体内定义函数.
把UINT CUdprecvDlg::ThreadFunc(LPVOID pParam)放到OnInitDialog函数体外定义

16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧