此指针值在MFC SDI应用程序中已更改

此指针值在MFC SDI应用程序中已更改,mfc,this,sdi,Mfc,This,Sdi,现在我有以下MFC SDI应用程序代码,该代码来自我的view类: void CNew_demo_appView::OnItemUpdate() { // TODO: Add your command handler code here int i=this->GetListCtrl().GetSelectionMark();//get the selected item no this->GetDocument()->unpacker.GetInfor

现在我有以下MFC SDI应用程序代码,该代码来自我的view类:

void CNew_demo_appView::OnItemUpdate()
{
    // TODO: Add your command handler code here
    int i=this->GetListCtrl().GetSelectionMark();//get the selected item no
    this->GetDocument()->unpacker.GetInformation(i,(BYTE*)(&(this->GetDocument()->fpga_info)));
    UpdateFpgaAttrib updatefpgadlg;
    updatefpgadlg.DisplayInfo(this->GetDocument()->fpga_info);
    updatefpgadlg.DoModal();
}

void CNew_demo_appView::SetItemFpgaAttrib(int index,FPGA_INFO info)
{
    this->GetDocument()->fpga_items[0]=info;
}
如您所见,我得到了一个名为UpdateFpgaAttrib的CDialog派生类,我在发出菜单命令时调用的OnItemUpdate函数中实例化它,然后是DoModal() 弹出对话框窗口,在那个对话框上,有一个按钮,点击它会调用 属于视图类的SetItemFpgaAttrib函数

((CNew_demo_appView*)this->GetParent())->SetItemFpgaAttrib(0,info);
这就是问题所在,当这个 SetItemFpgaAttrib使用这个指针引用了一些数据,它总是得到一些访问冲突错误,当我在其他视图类函数中调用这个函数时,它是ok的

void CNew_demo_appView::test()
{
    SetItemFpgaAttrib(0,this->GetDocument()->fpga_info)
}

当被弹出对话框按钮触发时,它会导致问题,我在SetItemFpgaAttrib上设置了断点,我发现这个指针值是正常的0x0041237f东西,但当被按钮触发时,它总是0x00000001,GetDocument调用总是会导致问题。为什么this指针值会发生更改,这是由上下文还是其他原因引起的?我正在使用Vs2008 SP1解决问题,我只想在这里为有一天也遇到这个问题的其他人提供答案。问题在于

((CNew_demo_appView*)this->GetParent())->SetItemFpgaAttrib(0,info);
GetParent()是在CWnd中实现的,它返回一个CWnd*,这就是问题所在,SetItemFpgaAttrib(0,info)是我的CDialog派生类CNew_demo_appView的函数,它不是CWnd的成员,因此返回的CWnd*指针无法获取该函数的代码,如果你像我一样这样做,您将访问一些错误的位置,并将获得违反访问权限的错误等。我需要一个返回原始CNew_demo_appView*指针值的函数,m_pParentWnd中的一个是所需的值(我在进入CWnd::GetParent函数时发现了这一点),而默认的GetParent是这样做的:

return (CWnd*)ptr;
为了解决这个问题,我只需在CDialog派生类中添加另一个函数:

CWnd* UpdateFpgaAttrib::GetParentView(void)
{
    return this->m_pParentWnd; //just return the parent wnd pointer
}
然后调用此函数,而不是默认的GetParent:

CNew_demo_appView* view=(CNew_demo_appView*)this->GetParentView();
那么一切都好了

因此结论:GetParent中的CWnd*强制转换更改了指针的值