vc6.0和vs2008同样代码不同结果

tmwanly 2012-11-28 09:35:59
void ThreadFunc()
{
CTime time;
CString strTime;
m_bRun=TRUE;
while(m_bRun)
{
time=CTime::GetCurrentTime();
strTime=time.Format("%H:%M:%S");
::SetDlgItemText(AfxGetApp()->GetMainWnd()->m_hWnd,IDC_EDIT1,strTime);
//::SetDlgItemText(AfxgetMainWnd()->m_pMainWnd->m_hWnd,IDC_EDIT1,strTime);可以
//::SetDlgItemText(AfxGetMainWnd()->m_hWnd,IDC_EDIT1,strTime);不可以
Sleep(1000);
}
}



为什么::SetDlgItemText(AfxGetMainWnd()->m_hWnd,IDC_EDIT1,strTime);在vc6.0中可以用但是在vs2008不可以用呢,运行后会出现错误,调试运行箭头停在
::SetDlgItemText(AfxGetMainWnd()->m_hWnd,IDC_EDIT1,strTime);
有木有人知道是为什么呢,而且按理说这种错误应该是不会发生的。

...全文
249 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
yisikaipu 2012-11-28
  • 打赏
  • 举报
回复
补充: 查 VC6 的 AfxGetMainWnd,会发现与新版的一致 问题出在其内部调用的 AfxGetThread VC6 MSDN: Pointer to the currently executing thread. VC9(VS2005) MSDN:http://msdn.microsoft.com/en-us/library/0aa1ya36(v=vs.90).aspx Pointer to the currently executing thread; otherwise NULL. 而且有个Note: If you are porting an MFC project calling AfxGetThread from Visual C++ versions 4.2, 5.0, or 6.0, AfxGetThread calls AfxGetApp if no thread is found. In Visual C+ .NET and later, AfxGetThread returns NULL if no thread was found. If you want the application thread, you must call AfxGetApp. VC6会自动调用AfxGetApp,而VC9则返回NULL
shen_wei 2012-11-28
  • 打赏
  • 举报
回复
编译的方式不一样,应该使用最新的编译器。。。跟上时代!!
Cppinsider 2012-11-28
  • 打赏
  • 举报
回复
两个编译器相隔有些时间了,肯定有些不同。 VC6.0,VS2005, VS2010都用过的打酱油路过。
qin_zhu 2012-11-28
  • 打赏
  • 举报
回复
引用 1 楼 qin_zhu 的回复:
Return Value: If the server has an object that is in-place active inside a container, and this container is active, this function returns a pointer to the frame window object that contains the in-pla……
上面说的是 AfxGetMainWnd()。。。
qin_zhu 2012-11-28
  • 打赏
  • 举报
回复
Return Value: If the server has an object that is in-place active inside a container, and this container is active, this function returns a pointer to the frame window object that contains the in-place active document. If there is no object that is in-place active within a container, or your application is not an OLE server, this function simply returns the m_pMainWnd of your application object. Remarks: If your application is not an OLE server, then calling this function is equivalent to directly referring to the m_pMainWnd member of your application object. 根据MSDN所说,在vc6.0、vs2008中你的应用程序性质不同,导致其返回结果也不一样,所以调用的形式也不同。 ps:这两个编译器默认编码方式也不同,所以调用方式上可能也会受影响。。。。
yisikaipu 2012-11-28
  • 打赏
  • 举报
回复
引用 1 楼 qin_zhu 的回复:
根据MSDN所说,在vc6.0、vs2008中你的应用程序性质不同,导致其返回结果也不一样,所以调用的形式也不同。 ps:这两个编译器默认编码方式也不同,所以调用方式上可能也会受影响。。。。
错! 仅仅就是因为 AfxGetMainWnd 函数改变了。旧版的总是返回主线程的主窗口,新版的返回线程的主窗口。
tmwanly 2012-11-28
  • 打赏
  • 举报
回复
换个编译器很简单,但是为什么呢
techinsider 2012-11-28
  • 打赏
  • 举报
回复
用VS2010旗舰版吧。

64,685

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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