开发者

Why am I seeing a crash when trying to call CDHtmlDialog::OnInitDialog()

开发者 https://www.devze.com 2022-12-27 17:36 出处:网络
I added a helpAbout menu item to my mfc app.I decided to make the ddlg derive from CDHTMLDialog. I override the OnInitDialog() method in my derived class and the first thing I do is call the parent\'

I added a helpAbout menu item to my mfc app. I decided to make the ddlg derive from CDHTMLDialog.

I override the OnInitDialog() method in my derived class and the first thing I do is call the parent's OnInitDialog() method.

I then put in code that sets the title.

On some machines this works fine, but on others it crashes in the call to

CDHtmlDialog::OnInitDialog() - Trying to read a null pointer.

the call stack has nothing useful - it is in mfc90.dll

Is this a potential problem with mismatches of mfc/win32 dlls?

It works on my vista machines but crashes on a win2003 server box.

BOOL HTMLAboutDlg::OnInitDialog()
{
   // CRASHES on the following line
    CDHtmlDialog::OnInitDialog();
    CString title = "my title";  // e开发者_JAVA百科xample of setting title

     ...        other code

    SetWindowText(title);
    return TRUE;  // return TRUE  unless you set the focus to a control
}

And here is the relevant header file:

class HTMLAboutDlg : public CDHtmlDialog
{
    DECLARE_DYNCREATE(HTMLAboutDlg)

public:
    HTMLAboutDlg(CWnd* pParent = NULL);   // standard constructor
    virtual ~HTMLAboutDlg();
// Overrides
    HRESULT OnButtonOK(IHTMLElement *pElement);
    HRESULT OnButtonCancel(IHTMLElement *pElement);

// Dialog Data
    enum { IDD = IDD_DIALOG_ABOUT, IDH = IDR_HTML_HTMLABOUTDLG };

protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
    virtual BOOL OnInitDialog();

    DECLARE_MESSAGE_MAP()
    DECLARE_DHTML_EVENT_MAP()
};

I can't figure out what is going on - specifically why it works on some machines and crashes on others.

Both have VS2008 installed

Visual Studio reports the following for each machine:

VISTA - no crashes 9.0.30729.1 SP

2003 server: (crashes) 9.0.21022.8 RTM

EDIT - html code

<HTML>
<BODY ID=HTMLAboutDlg BGCOLOR=WHITE>

<TABLE WIDTH=100%>
<TR WIDTH=100% HEIGHT=75>
<TD ALIGN=CENTER VALIGN=TOP>
<font color="#707880">by </font><a ID=LinkCP target=_blank href='http://www.mywebsite.com'><font color="#000000">my</font><font color="#2554C7">web</font><font color="#7093DB">site</font></a>
</TD>
</TR>
<TR>
<TD ALIGN=RIGHT ALIGN=BOTTOM>
<BUTTON STYLE="WIDTH:80" ID="ButtonOK">OK</BUTTON><BR>
</TD>
</TR>
</TABLE>

</BODY>
</HTML>


Try quoting all the ID attribute values in the HTML, and double-check that the the ID attributes match the references in the DHTML_EVENT_MAP.

I'm guessing that there is differing behavior between IE versions with respect to the case sensitivity of ID attributes or the handling of missing HTML elements. The net effect might be that the base OnInitDialog() class cannot hook up to some of the elements in the document object model on those machines with a particular version of Internet Explorer installed.

Given the history of IE, it's quite possible this behavior crept into one version then was subsequently removed.

This has to be just an educated guess: I don't have access to all the earlier versions of IE to confirm this behavior...


I think you need to call SetHostFlags before calling base class OnInitDialog 'CDHtmlDialog::OnInitDialog()'.

eg. SetHostFlags(DOCHOSTUIFLAG_FLAT_SCROLLBAR); //Set the host UI flags as per your need.

General info: CDHtmlDialog is declared in 'afxdhtml.h'. (I'm sure you would know it)

Since DHtmlDialog internally deals with COM, try having

CoInitialize();//at the begining of application launch

CoUninitialize();//at the exit of app.


It might be caused by trying to reference a non-existent html file. Make sure the IDH enum you pass to the base class constructor is valid and actually references an existing HTML page.

HTMLAboutDlg::HTMLAboutDlg(Cwnd *pParent) 
    : CDHTMLDialog(HTMLAboutDlg::IDD, HTMLAboutDlg::IDH, pParent)

So, check your IDR_HTML_HTMLABOUTDLG value is actually assigned a correct HTML page.

If you created your dialog with the wizard I think it generated a page for your with the Name like "MyProjectName.htm", normally this is assigned the IDR_HTML_HTMLABOUTDLG value.

Your resource .rc file should probably have a like a little like:

IDR_HTML_MYPROJECT_DIALOG HTML                    "MyProjectName.htm"

Does your HTMLDialogs OnDocumentComplete method get called before OnInitDialog? This happened to me when I twiddled about with this a bit and due to some bad error checking on my part elsewhere it caused the program to crash.

Could you post a stack trace if this doesn't help, or possibly the cpp, h, html and .rc file.


I had a similar problem which I solved by adding ::AfxOleInit(); near the top of the CMyApp::InitInstance() function. If this is not called before the window is created, it will crash the whole app. Here is what the start of my InitInstance function now looks like:

BOOL CMyApp::InitInstance()
{
    // InitCommonControls() is required on Windows XP if an application
    // manifest specifies use of ComCtl32.dll version 6 or later to enable
    // visual styles.  Otherwise, any window creation will fail.
    InitCommonControls();
    CWinApp::InitInstance();

    ::AfxOleInit();

...
0

精彩评论

暂无评论...
验证码 换一张
取 消