Problems with a static library
Jon McCulloch -- sensei@anglianet.co.uk Friday, November 01, 1996 Environment: NT 3.51, Visual C++ 4.0 I have an applicatiion for which I'm trying to create and use an static library. At the moment all I'm trying to do is call a GetPassword() function from the library. The code looks like this: Application: CString strPassword; CString strPrincipal; // username strPassword = GetPassword(strPrincipal); where the function prototype is: CString GetPassword(const CString&). Library: GetPassword() looks like this: CString GetPassword(const CString& strUsername) { CString strPassword; CGetPasswordDlg dlg; dlg.DoModal(); return strPassword; } // end of GetPassword() All I'm trying to do at the momebnt is display the bloody dialog box, never mind exchange andy data. CGetPasswordDlg is just a dialog-box class: // GetPasswordDlg.cpp : implementation file // #include "stdafx.h" #include "sdiwin32.h" #include "resource.h" #include "GetPasswordDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CGetPasswordDlg dialog CGetPasswordDlg::CGetPasswordDlg(CWnd* pParent /*=NULL*/) : CDialog(CGetPasswordDlg::IDD, pParent) { //{{AFX_DATA_INIT(CGetPasswordDlg) m_Password = _T(""); //}}AFX_DATA_INIT } void CGetPasswordDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CGetPasswordDlg) DDX_Text(pDX, IDC_GET_PASSWORD, m_Password); DDV_MaxChars(pDX, m_Password, 10); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CGetPasswordDlg, CDialog) //{{AFX_MSG_MAP(CGetPasswordDlg) // NOTE: the ClassWizard will add message map macros here //}}AFX_MSG_MAP END_MESSAGE_MAP() And the header file looks like this: // // GetPasswordDlg.h : header file // ///////////////////////////////////////////////////////////////////////////// // CGetPasswordDlg dialog class CGetPasswordDlg : public CDialog { // Construction public: CGetPasswordDlg(CWnd* pParent = NULL); // standard constructor // Dialog Data //{{AFX_DATA(CGetPasswordDlg) enum { IDD = IDD_GETPASSWORD }; CString m_Password; //}}AFX_DATA // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CGetPasswordDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: // Generated message map functions //{{AFX_MSG(CGetPasswordDlg) // NOTE: the ClassWizard will add member functions here //}}AFX_MSG DECLARE_MESSAGE_MAP() }; I can compile and link the App. with the calls to GetPassword() with nary a blip, but when I run it, it bombs out. I've traced the problem into CWnd::CreateDlgIndirect(LPCDLGTEMPLATE lpDialogTemplate, CWnd* pParentWnd) in dlgcore.cpp, specifically, the call HWND hWnd = ::CreateDialogIndirect(AfxGetInstanceHandle(), lpDialogTemplate, pParentWnd->GetSafeHwnd(), AfxDlgProc); For some reason that I cannot fathom, this is giving me a run-time error of "unhandled exception in login.exe (MFC40D.DLL): 0x0000005: Access Violation". Can anyone suggest what is wrong? The truly annoying thing is that it worked fine earlier today... Thanks, Jon
Dong Chen -- d_chen@ix.netcom.com Sunday, November 03, 1996 [Mini-digest: 2 responses] At 03:07 PM 11/1/96 +0000, you wrote: >Environment: NT 3.51, Visual C++ 4.0 > >I have an applicatiion for which I'm trying to create and use an static library. > >At the moment all I'm trying to do is call a GetPassword() function from the >library. > >The code looks like this: > >Application: > >CString strPassword; >CString strPrincipal; // username > >strPassword = GetPassword(strPrincipal); > > >where the function prototype is: CString GetPassword(const CString&). > >Library: > >GetPassword() looks like this: > >CString GetPassword(const CString& strUsername) >{ > CString strPassword; > CGetPasswordDlg dlg; > > dlg.DoModal(); > > return strPassword; >} // end of GetPassword() > (snip) My guess is that you probably didn't add the AFX_MANAGE_STATE macro. But what confused me is that you said it worked earlier. Maybe others can spot the exact problem. -----From: "Doug Brubacher"Jon, Since I do not recognize the error message you talk about, it is pretty hard to tell what is wrong from the information you have provided, none the less I still have a few suggestions: 1. Problem one is, where is the static library getting its resources from? Any application that uses your static library is going to require all the resources (dialogs resources, string tables, etc.) that the classes in your library require. Ensure this is true for the current app, otherwise stick all the static library resources in their own DLL. and anywhere in the library that requires a resource do a AfxSetResourceHandle( hStaticLibraryResourceHandle ) before the resource is referenced. Don't forget to restore the apps resource handle when your done. 2. You say it worked earlier today? A Rebuild All never hurts, or more likely have you changed something? With dialogs if you rename one of the controls that your class is performing Dynamic Data Exchange with you always need to delete the member variable and then re-add it with class wizard or edit the appropriate DDX macro in DoDataExchange. Regards, Doug Brubacher DouglasB@msn.com
| Вернуться в корень Архива |