Please what is wrong with this my MFC code, what am I doing wrong?
-
Normally, I code using win32 API. I just moved to MFC. I wrote this MFC code for a wizard but keep getting exceptions thrown. A first chance exception to be precise. My wizard is contained in an MFC extension dll. The wizard is lunch from an handler of CMainFrame. The CMainFrame from which the wizard is lunched is:
void CMainFrame::OnAddClassInfo()
{
CWinApp *pWinApp = AfxGetApp();FirstPage firstPage; ClassNameA className; ClassTerm classTerm; ClassSubjects classSubjects; ClassStaff classStaff; StopPage stopPage; className.SetAccess(m\_szAccessUsername,m\_szAccessPassword); className.SetClassInfo(m\_iClassTypeID, m\_iClassID); stopPage.SetClassInfoCombo(&m\_wndToolBar.m\_ArmName,&m\_wndToolBar.m\_ClassName,&m\_wndToolBar.m\_Session); CString stWizard; stWizard.LoadString(pWinApp->m\_hInstance,IDS\_CLASSINFO\_WIZARD,(WORD)LANGUAGE); ClassInfo ClassInfoWizard(stWizard,this); firstPage.SetPointer(reinterpret\_cast(&ClassInfoWizard)); className.SetPointer(reinterpret\_cast(&ClassInfoWizard)); classTerm.SetPointer(reinterpret\_cast(&ClassInfoWizard)); classSubjects.SetPointer(reinterpret\_cast(&ClassInfoWizard)); classStaff.SetPointer(reinterpret\_cast(&ClassInfoWizard)); stopPage.SetPointer(reinterpret\_cast(&ClassInfoWizard)); ClassInfoWizard.AddPage(&firstPage); ClassInfoWizard.AddPage(&className); ClassInfoWizard.AddPage(&classTerm); ClassInfoWizard.AddPage(&classSubjects); ClassInfoWizard.AddPage(&classStaff); ClassInfoWizard.AddPage(&stopPage); ClassInfoWizard.SetWizardMode(); if(ClassInfoWizard.DoModal() == ID\_WIZFINISH) { m\_eClassInfoView\_State = ENABLE; m\_eClassInfoChange\_State = ENABLE; m\_eStudentInfoAdd\_State = ENABLE; m\_eGradeInfoAdd\_State = ENABLE; }
}
The output window show the following error messages: First-chance exception at 0xfefefefe in SchPedestal.exe: 0xC0000005: Access violation. Unhandled exception at 0xfefefefe in SchPedestal.exe: 0xC0000005: Access violation. The program '[1456] SchPedestal.exe: Native' has exited with code -1073741819 (0xc0000005). Note: ClassInfo inherits from CPropertySheet, while the other objects inherit from CPropertyPage.
-
Normally, I code using win32 API. I just moved to MFC. I wrote this MFC code for a wizard but keep getting exceptions thrown. A first chance exception to be precise. My wizard is contained in an MFC extension dll. The wizard is lunch from an handler of CMainFrame. The CMainFrame from which the wizard is lunched is:
void CMainFrame::OnAddClassInfo()
{
CWinApp *pWinApp = AfxGetApp();FirstPage firstPage; ClassNameA className; ClassTerm classTerm; ClassSubjects classSubjects; ClassStaff classStaff; StopPage stopPage; className.SetAccess(m\_szAccessUsername,m\_szAccessPassword); className.SetClassInfo(m\_iClassTypeID, m\_iClassID); stopPage.SetClassInfoCombo(&m\_wndToolBar.m\_ArmName,&m\_wndToolBar.m\_ClassName,&m\_wndToolBar.m\_Session); CString stWizard; stWizard.LoadString(pWinApp->m\_hInstance,IDS\_CLASSINFO\_WIZARD,(WORD)LANGUAGE); ClassInfo ClassInfoWizard(stWizard,this); firstPage.SetPointer(reinterpret\_cast(&ClassInfoWizard)); className.SetPointer(reinterpret\_cast(&ClassInfoWizard)); classTerm.SetPointer(reinterpret\_cast(&ClassInfoWizard)); classSubjects.SetPointer(reinterpret\_cast(&ClassInfoWizard)); classStaff.SetPointer(reinterpret\_cast(&ClassInfoWizard)); stopPage.SetPointer(reinterpret\_cast(&ClassInfoWizard)); ClassInfoWizard.AddPage(&firstPage); ClassInfoWizard.AddPage(&className); ClassInfoWizard.AddPage(&classTerm); ClassInfoWizard.AddPage(&classSubjects); ClassInfoWizard.AddPage(&classStaff); ClassInfoWizard.AddPage(&stopPage); ClassInfoWizard.SetWizardMode(); if(ClassInfoWizard.DoModal() == ID\_WIZFINISH) { m\_eClassInfoView\_State = ENABLE; m\_eClassInfoChange\_State = ENABLE; m\_eStudentInfoAdd\_State = ENABLE; m\_eGradeInfoAdd\_State = ENABLE; }
}
The output window show the following error messages: First-chance exception at 0xfefefefe in SchPedestal.exe: 0xC0000005: Access violation. Unhandled exception at 0xfefefefe in SchPedestal.exe: 0xC0000005: Access violation. The program '[1456] SchPedestal.exe: Native' has exited with code -1073741819 (0xc0000005). Note: ClassInfo inherits from CPropertySheet, while the other objects inherit from CPropertyPage.
Please edit your post and remove at least the repeated lines, or better all message lines except the first two exceptions. The other lines are not related to your problem.
-
Normally, I code using win32 API. I just moved to MFC. I wrote this MFC code for a wizard but keep getting exceptions thrown. A first chance exception to be precise. My wizard is contained in an MFC extension dll. The wizard is lunch from an handler of CMainFrame. The CMainFrame from which the wizard is lunched is:
void CMainFrame::OnAddClassInfo()
{
CWinApp *pWinApp = AfxGetApp();FirstPage firstPage; ClassNameA className; ClassTerm classTerm; ClassSubjects classSubjects; ClassStaff classStaff; StopPage stopPage; className.SetAccess(m\_szAccessUsername,m\_szAccessPassword); className.SetClassInfo(m\_iClassTypeID, m\_iClassID); stopPage.SetClassInfoCombo(&m\_wndToolBar.m\_ArmName,&m\_wndToolBar.m\_ClassName,&m\_wndToolBar.m\_Session); CString stWizard; stWizard.LoadString(pWinApp->m\_hInstance,IDS\_CLASSINFO\_WIZARD,(WORD)LANGUAGE); ClassInfo ClassInfoWizard(stWizard,this); firstPage.SetPointer(reinterpret\_cast(&ClassInfoWizard)); className.SetPointer(reinterpret\_cast(&ClassInfoWizard)); classTerm.SetPointer(reinterpret\_cast(&ClassInfoWizard)); classSubjects.SetPointer(reinterpret\_cast(&ClassInfoWizard)); classStaff.SetPointer(reinterpret\_cast(&ClassInfoWizard)); stopPage.SetPointer(reinterpret\_cast(&ClassInfoWizard)); ClassInfoWizard.AddPage(&firstPage); ClassInfoWizard.AddPage(&className); ClassInfoWizard.AddPage(&classTerm); ClassInfoWizard.AddPage(&classSubjects); ClassInfoWizard.AddPage(&classStaff); ClassInfoWizard.AddPage(&stopPage); ClassInfoWizard.SetWizardMode(); if(ClassInfoWizard.DoModal() == ID\_WIZFINISH) { m\_eClassInfoView\_State = ENABLE; m\_eClassInfoChange\_State = ENABLE; m\_eStudentInfoAdd\_State = ENABLE; m\_eGradeInfoAdd\_State = ENABLE; }
}
The output window show the following error messages: First-chance exception at 0xfefefefe in SchPedestal.exe: 0xC0000005: Access violation. Unhandled exception at 0xfefefefe in SchPedestal.exe: 0xC0000005: Access violation. The program '[1456] SchPedestal.exe: Native' has exited with code -1073741819 (0xc0000005). Note: ClassInfo inherits from CPropertySheet, while the other objects inherit from CPropertyPage.
If you expect the error to be in the posted code, set a breakpoint on top and use the debugger to step through the instructions until the error occurs. Then check your variables to find out which is generating the access violation.
-
Normally, I code using win32 API. I just moved to MFC. I wrote this MFC code for a wizard but keep getting exceptions thrown. A first chance exception to be precise. My wizard is contained in an MFC extension dll. The wizard is lunch from an handler of CMainFrame. The CMainFrame from which the wizard is lunched is:
void CMainFrame::OnAddClassInfo()
{
CWinApp *pWinApp = AfxGetApp();FirstPage firstPage; ClassNameA className; ClassTerm classTerm; ClassSubjects classSubjects; ClassStaff classStaff; StopPage stopPage; className.SetAccess(m\_szAccessUsername,m\_szAccessPassword); className.SetClassInfo(m\_iClassTypeID, m\_iClassID); stopPage.SetClassInfoCombo(&m\_wndToolBar.m\_ArmName,&m\_wndToolBar.m\_ClassName,&m\_wndToolBar.m\_Session); CString stWizard; stWizard.LoadString(pWinApp->m\_hInstance,IDS\_CLASSINFO\_WIZARD,(WORD)LANGUAGE); ClassInfo ClassInfoWizard(stWizard,this); firstPage.SetPointer(reinterpret\_cast(&ClassInfoWizard)); className.SetPointer(reinterpret\_cast(&ClassInfoWizard)); classTerm.SetPointer(reinterpret\_cast(&ClassInfoWizard)); classSubjects.SetPointer(reinterpret\_cast(&ClassInfoWizard)); classStaff.SetPointer(reinterpret\_cast(&ClassInfoWizard)); stopPage.SetPointer(reinterpret\_cast(&ClassInfoWizard)); ClassInfoWizard.AddPage(&firstPage); ClassInfoWizard.AddPage(&className); ClassInfoWizard.AddPage(&classTerm); ClassInfoWizard.AddPage(&classSubjects); ClassInfoWizard.AddPage(&classStaff); ClassInfoWizard.AddPage(&stopPage); ClassInfoWizard.SetWizardMode(); if(ClassInfoWizard.DoModal() == ID\_WIZFINISH) { m\_eClassInfoView\_State = ENABLE; m\_eClassInfoChange\_State = ENABLE; m\_eStudentInfoAdd\_State = ENABLE; m\_eGradeInfoAdd\_State = ENABLE; }
}
The output window show the following error messages: First-chance exception at 0xfefefefe in SchPedestal.exe: 0xC0000005: Access violation. Unhandled exception at 0xfefefefe in SchPedestal.exe: 0xC0000005: Access violation. The program '[1456] SchPedestal.exe: Native' has exited with code -1073741819 (0xc0000005). Note: ClassInfo inherits from CPropertySheet, while the other objects inherit from CPropertyPage.
Your page variables (i.e., the ones used by
AddPage()
) should be members ofCMainFrame
, not local toOnAddClassInfo()
. What type of object isClassInfo
? Other than that, you need to step through the code using the debugger. You might be better served by letting the MFC framework create a wizard-based app for you. You can customize it from there."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
Please edit your post and remove at least the repeated lines, or better all message lines except the first two exceptions. The other lines are not related to your problem.
-
Your page variables (i.e., the ones used by
AddPage()
) should be members ofCMainFrame
, not local toOnAddClassInfo()
. What type of object isClassInfo
? Other than that, you need to step through the code using the debugger. You might be better served by letting the MFC framework create a wizard-based app for you. You can customize it from there."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
ClassInfo inherits from CPropertyPage, while the other objects inherit from CPropertyPage. The program successfully displays the wizards start page, but once I click next, this access violation occurs Debugging shows that the access violation occurred in the following function:
int ClassName::LoadSchoolName()
{
CComboBox *pSchoolName = (CComboBox *)GetDlgItem(IDC_COMBO1);char szString\[100\]; TCHAR szString1\[100\]; INT\_PTR iSchInfoID; try { //Get database path char szDatabaseFile\[100\]; GetDatabaseA(szDatabaseFile,sizeof(szDatabaseFile)); //open database CppA::CppSQLite3DB db; db.open(szDatabaseFile); StringCbCopyA(szString,sizeof(szString),"SELECT SchInfoID,SchName,SchCity,SchState,SchCountry FROM SchInfo"); CppA::CppSQLite3Query q = db.execQuery(szString); if(q.fieldIsNull(0)) { return 0; } //Gets first row //q.First(); size\_t size; const char \*p; while (!q.eof()) { TCHAR szMySchName\[50\],szMySchCity\[20\],szMySchState\[20\],szMySchCountry\[20\]; long long iSchoolInfoID; for (int fld = 0; fld < q.numFields(); fld++) { switch(fld) { case 0: { iSchoolInfoID = q.getInt64Field(0); } break; case 1: { p = q.getStringField(1); DecryptString(const\_cast(p),szString); mbstowcs\_s(&size,szString1,sizeof szString1,szString,lstrlenA(szString)); StringCbCopy(szMySchName,sizeof(szMySchName),szString1); } break; case 2: { p = q.getStringField(2); DecryptString(const\_cast(p),szString); mbstowcs\_s(&size,szString1,sizeof(szString1),szString,lstrlenA(szString)); StringCbCopy(szMySchCity,sizeof(szMySchCity),szString1); } break; case 3: { p = q.getStringField(3); DecryptString(const\_cast(p),szString); mbstowcs\_s(&size,szString1,sizeof(szString1),szString,lstrlenA(szString)); StringCbCopy(szMySchState,sizeof(szMySchState),szString1); } break; case 4: { p = q.getStringField(4); DecryptString(const\_cast(p),szString); mbstowcs\_s(&size,szString1,sizeof(szString1),szString,lstrlenA(szString)); StringCbCopy(szMySchCountry,sizeof(szMySchCountry),szString1); } break; } } TCHAR szSchoolName\[100\]; StringCbPrintf(szSchoolName,sizeof szSchoolName,\_T("%s,%s,%s,%s"),szMySch
-
Below each post are some links like Reply, Email, .... If you are on your own post, there are two additional links: Edit and Delete.
-
ClassInfo inherits from CPropertyPage, while the other objects inherit from CPropertyPage. The program successfully displays the wizards start page, but once I click next, this access violation occurs Debugging shows that the access violation occurred in the following function:
int ClassName::LoadSchoolName()
{
CComboBox *pSchoolName = (CComboBox *)GetDlgItem(IDC_COMBO1);char szString\[100\]; TCHAR szString1\[100\]; INT\_PTR iSchInfoID; try { //Get database path char szDatabaseFile\[100\]; GetDatabaseA(szDatabaseFile,sizeof(szDatabaseFile)); //open database CppA::CppSQLite3DB db; db.open(szDatabaseFile); StringCbCopyA(szString,sizeof(szString),"SELECT SchInfoID,SchName,SchCity,SchState,SchCountry FROM SchInfo"); CppA::CppSQLite3Query q = db.execQuery(szString); if(q.fieldIsNull(0)) { return 0; } //Gets first row //q.First(); size\_t size; const char \*p; while (!q.eof()) { TCHAR szMySchName\[50\],szMySchCity\[20\],szMySchState\[20\],szMySchCountry\[20\]; long long iSchoolInfoID; for (int fld = 0; fld < q.numFields(); fld++) { switch(fld) { case 0: { iSchoolInfoID = q.getInt64Field(0); } break; case 1: { p = q.getStringField(1); DecryptString(const\_cast(p),szString); mbstowcs\_s(&size,szString1,sizeof szString1,szString,lstrlenA(szString)); StringCbCopy(szMySchName,sizeof(szMySchName),szString1); } break; case 2: { p = q.getStringField(2); DecryptString(const\_cast(p),szString); mbstowcs\_s(&size,szString1,sizeof(szString1),szString,lstrlenA(szString)); StringCbCopy(szMySchCity,sizeof(szMySchCity),szString1); } break; case 3: { p = q.getStringField(3); DecryptString(const\_cast(p),szString); mbstowcs\_s(&size,szString1,sizeof(szString1),szString,lstrlenA(szString)); StringCbCopy(szMySchState,sizeof(szMySchState),szString1); } break; case 4: { p = q.getStringField(4); DecryptString(const\_cast(p),szString); mbstowcs\_s(&size,szString1,sizeof(szString1),szString,lstrlenA(szString)); StringCbCopy(szMySchCountry,sizeof(szMySchCountry),szString1); } break; } } TCHAR szSchoolName\[100\]; StringCbPrintf(szSchoolName,sizeof szSchoolName,\_T("%s,%s,%s,%s"),szMySch
You are formatting quite a lot of things into the school name. Maybe you create more than 100 characters and the StringCbPrintf() doesn't add the terminating \0 character in that case. You could try szSchoolName[99] = 0; before calling AddString() and see if that changes anything.
The good thing about pessimism is, that you are always either right or pleasently surprised.
-
ClassInfo inherits from CPropertyPage, while the other objects inherit from CPropertyPage. The program successfully displays the wizards start page, but once I click next, this access violation occurs Debugging shows that the access violation occurred in the following function:
int ClassName::LoadSchoolName()
{
CComboBox *pSchoolName = (CComboBox *)GetDlgItem(IDC_COMBO1);char szString\[100\]; TCHAR szString1\[100\]; INT\_PTR iSchInfoID; try { //Get database path char szDatabaseFile\[100\]; GetDatabaseA(szDatabaseFile,sizeof(szDatabaseFile)); //open database CppA::CppSQLite3DB db; db.open(szDatabaseFile); StringCbCopyA(szString,sizeof(szString),"SELECT SchInfoID,SchName,SchCity,SchState,SchCountry FROM SchInfo"); CppA::CppSQLite3Query q = db.execQuery(szString); if(q.fieldIsNull(0)) { return 0; } //Gets first row //q.First(); size\_t size; const char \*p; while (!q.eof()) { TCHAR szMySchName\[50\],szMySchCity\[20\],szMySchState\[20\],szMySchCountry\[20\]; long long iSchoolInfoID; for (int fld = 0; fld < q.numFields(); fld++) { switch(fld) { case 0: { iSchoolInfoID = q.getInt64Field(0); } break; case 1: { p = q.getStringField(1); DecryptString(const\_cast(p),szString); mbstowcs\_s(&size,szString1,sizeof szString1,szString,lstrlenA(szString)); StringCbCopy(szMySchName,sizeof(szMySchName),szString1); } break; case 2: { p = q.getStringField(2); DecryptString(const\_cast(p),szString); mbstowcs\_s(&size,szString1,sizeof(szString1),szString,lstrlenA(szString)); StringCbCopy(szMySchCity,sizeof(szMySchCity),szString1); } break; case 3: { p = q.getStringField(3); DecryptString(const\_cast(p),szString); mbstowcs\_s(&size,szString1,sizeof(szString1),szString,lstrlenA(szString)); StringCbCopy(szMySchState,sizeof(szMySchState),szString1); } break; case 4: { p = q.getStringField(4); DecryptString(const\_cast(p),szString); mbstowcs\_s(&size,szString1,sizeof(szString1),szString,lstrlenA(szString)); StringCbCopy(szMySchCountry,sizeof(szMySchCountry),szString1); } break; } } TCHAR szSchoolName\[100\]; StringCbPrintf(szSchoolName,sizeof szSchoolName,\_T("%s,%s,%s,%s"),szMySch
-
You seem to be mixing
char
andTCHAR
somewhat indiscriminately, and you should not be usingTCHAR
as the destination ofmbstowcs_s
. This may well be where you are going wrong.