When i use GetMenu()->GetSubMenu(2)-> 'xxx', there is an error:
Exception thrown at 0x00007FFEF70E4FD3 (mfc140d.dll) (in Menu.exe): 0xC0000005: An access violation occurred while reading location 0x0000000000008.
I added the above code at the end of the OnCreate function in the CMainFrame class.
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct){
GetMenu()->GetSubMenu(2)->CheckMenuItem(0, MF_BYPOSITION | MF_CHECKED);
}
3.Class "Cxxxx" does not have member "GetContextMenuManager" 'GetContextMenuManager': is not a member of'Cxxxx' After analysis, it is found that the current class Cxxx is inherited from CWinApp, which does not have the GetContextMenuManager function. If you select Use Menu Bar and Toolbar when creating a project, no error is reported. After analysis, it is found that Cxxx is derived from CWinAppEx. It has the GetContextMenuManager() member method. Therefore, do not report an error, manually modify the derived class.
It's true that GetMenu() got a null pointer. I found two solutions:
Change the value of CMFCMenuBar m_wndMenuBar in Mainfrm.h mainfrm.cpp. Delete the code of the. The compilation is successful.
When creating a project, select Use a classic menu.
Related
I created a check box in a dialog box and trying to access its condition whether checked or not. This is my code:
CButton *m_ctlCheckBlack = (CButton *)GetDlgItem(IDC_BLACK);
int chkBoxBlack = m_ctlCheckBlack->GetCheck();
As I run through this code, it pops up an exception saying :
Exception thrown at 0x0FA45564 (mfc140d.dll) in braille_obr.exe: 0xC0000005:
Access violation reading location 0x00000020.
any help will be appreciated. thank you.
There is rarely a good reason to use GetDlgItem. In your resource editor, right click the button and 'Add Variable..'
It will default as a control. Give it a name. You will get a member in the dialog class:
CButton myButtonName;
Now it should be safe to:
myButtonName.GetChecked( );
as it will have been created and properly subclassed.
Perhaps, I doubt it tried to get the state of button when the Button was not yet constructed.
int chkBoxBlack = m_ctlCheckBlack != NULL ? m_ctlCheckBlack->GetCheck() : 0;
Or, do you try to operate from outside dialog?
If it is so, you had better try to do this.
■CButton *m_ctlCheckBlack = (CButton *)yourdlg.GetDlgItem(IDC_BLACK);
■FindWindowEx(yourdlg.GetSafeHwnd(), NULL, NULL, "(your button caption)");
I'm trying to access control size properties inside a CDialog::OnSize event handler. Code compiles and runs, but seems to fail at GetWindowRect. No run time error is thrown that I can see, but the code including and following the GetWindowRect call silently fails to run.
I have ran in release and debug mode, with breakpoints in appropriate positions (which are not hit GetWindowRect). I have also used code that modifies some member variables following GetWindowRect as an additional test, which I can also see is not running.
virtual void CMainFormDialog::OnSize(UINT nType, int cx, int cy)
{
....
auto pOutputEdit = (CEdit*) GetDlgItem(CE_OutputEdit);
CRect pOutputEditRect;
// No code is executed following this statement...
pOutputEdit->GetWindowRect(&pOutputEditRect);
....
}
Is there something wrong with the way I'm trying to access properties of my CEdit control here, and why does it fail silently?
Thanks.
OnSize is likely called as part of the creation of the dialog, at a point where CE_OutputEditmay not yet have been created. CE_OutputEdit is created and bound as part of OnInitDialog. Check the return value of GetDlgItem and only perform whatever you need to do when it returns non NULL.
If needed use a variable that's set to true after you call CDialog::OnInitDialog and done other necessary initialization. Then use this variable in OnSize to determine if you should do your processing. Regardless of this, you should still check the return value of GetDlgItem
I am trying to call the function GetParent() of CWnd class.
Every time there is an exception thrown.
I'm calling the function from a working thread.
This is the line that causes the exception:
CWnd* parent = this->GetParent();
I've also tried "GetParent()->PostMessage(........);", and still the exception is thrown.
I'm using this method in a CDialog.
I have noticed that the CWnd member m_hWnd is sometimes 0x00000000 or 0x00000001.
In a different computer I don't get this exception.
Is it a problem in the project settings or in my code?
You receive an assertion and not an exception.
Here is the implementation from VS 2010:
ASSERT(::IsWindow(m_hWnd)); return CWnd::FromHandle(::GetParent(m_hWnd));
Your m_hWnd is not a window...
Now I have the following MFC SDI application code,this code comes from my view class:
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;
}
As you can see, I got a CDialog Derived class called UpdateFpgaAttrib, I instantialize it in the OnItemUpdate function which is called when a menu command is issued, then DoModal()
Popup the Dialog window, on that dialog, there is a button, when clicked it will call the
SetItemFpgaAttrib function which belongs to the View Class,
((CNew_demo_appView*)this->GetParent())->SetItemFpgaAttrib(0,info);
here is the problem, when this
SetItemFpgaAttrib references some data using this pointer, it always got some Access Violation Error, when I invoke this function in other View class function, it is ok,
void CNew_demo_appView::test()
{
SetItemFpgaAttrib(0,this->GetDocument()->fpga_info)
}
when triggered by the popup dialog button, it cause problem, I set break point on the SetItemFpgaAttrib , I found the this pointer value is normal 0x0041237f thing, but when triggered by the button ,it is always 0x00000001, the the GetDocument call alway cause problem. Why is the this pointer value changed,is that caused by the context or something else? I am using Vs2008 SP1
Problem solved, I just want to put the answer here for somebody else who also got this problem someday. The Problem is the
((CNew_demo_appView*)this->GetParent())->SetItemFpgaAttrib(0,info);
the GetParent() is implemented in CWnd, and it returns a CWnd*, that's the problem, the SetItemFpgaAttrib(0,info) is a function of my CDialog-Derived class CNew_demo_appView, it's not the member of CWnd, so the returned CWnd* pointer can't get the code to that function, if you do that like me, you will access some wrong place and will got Access violated error etc. I Need a function that return the original CNew_demo_appView* pointer value, the one in the m_pParentWnd is the value needed(I figure this out when I step into the CWnd::GetParent function), whilethe default GetParent did this:
return (CWnd*)ptr;
to solve this problem, I just add another function to my CDialog-Derived Class:
CWnd* UpdateFpgaAttrib::GetParentView(void)
{
return this->m_pParentWnd; //just return the parent wnd pointer
}
then call this instead of the default GetParent:
CNew_demo_appView* view=(CNew_demo_appView*)this->GetParentView();
Then everything is ok.
So conclusion: the CWnd* cast in the GetParent changed the value of the pointer.
I'm writing a test app that simulates key presses of another application. For every key press I have to check if the right window/form is shown. So what I do is get the pointer of the window being shown and get it's window title. However, not all the windows/forms shown window titles. So I'm thinking it would be better to get the name of the class instead. How can I get the name of the class?
QWidget *pWin = QApplication::activeWindow();
when I try:
pWin->className();
to get the name of the class, I'm getting:
"error: class QWidget has no member named 'className' "
Can somebody show me the right way?
Try using the metaobject.
pWin->metaObject()->className();
You could also check the typeinfo header. Using the typeid operator on you object you get a type_info instance which describes the type of your object.
Check out: http://www.cplusplus.com/reference/std/typeinfo/type_info/