Solved

IHTMLDOMNode appendChild return E_INVALIDARG

Posted on 2003-11-09
6
1,408 Views
Last Modified: 2007-12-19
Hi,

I need some help please.
I tried to figure it out, but with no success so far.
Why do I get  E_INVALIDARG   when I try to use the appendChild Function.
This is my source code:


IHTMLElement* pHTMLElement;
IHTMLElement* pNewHTMLElement;
IHTMLDOMNode* pHTMLDOMNode;
IHTMLDOMNode* pNewHTMLDOMNode;
IHTMLDOMNode* pNew2HTMLDOMNode;
IDispatch* pDisp;


   v.vt = VT_BSTR;
   v.bstrVal =  SysAllocString(L"HEAD");

   hResult = pIHTMLElementCollection->tags(v,&pDisp );

   if (hResult == S_OK)

   {
        hResult = pDisp->QueryInterface(IID_IHTMLElementCollection,(void**)&pIHTMLElementCollection);
        if (hResult == S_OK)
        {
             pIHTMLElementCollection->get_length(&lCollectionSize);

             v.vt = VT_I4;
             v.lVal = 0;

             hResult = pIHTMLElementCollection->item(v,v,&pDisp);

             if (hResult == S_OK && pDisp != NULL)
             {
                  hResult = pDisp->QueryInterface(IID_IHTMLElement,(void**)&pHTMLElement);
                  if (hResult == S_OK)
                  {
                       hResult=pHTMLElement->QueryInterface(IID_IHTMLDOMNode,(void**)&pHTMLDOMNode);

                       if (hResult == S_OK)
                       {
                            bstrTemp = SysAllocString(L"BASE");
                            hResult=pIHTMLDocument2->createElement(bstrTemp, &pNewHTMLElement);
                            SysFreeString(bstrTemp);

                            if (hResult == S_OK)
                            {
                                  bstrTemp = SysAllocString(L"href");
                                  v.vt = VT_BSTR;
                                  v.bstrVal =  strWebPageDomain.GetBuffer();

                                 hResult=pNewHTMLElement->setAttribute(bstrTemp, v, 0);

                                 SysFreeString(bstrTemp);

                                 if (hResult == S_OK)
                                 {
                                      hResult=pNewHTMLElement->QueryInterface(IID_IHTMLDOMNode,(void**)&pNewHTMLDOMNode);
                                      if (hResult == S_OK)
                                      {
                                          ***********************************
************************************
                                                hResult =pHTMLDOMNode->appendChild(pNewHTMLDOMNode, &pNew2HTMLDOMNode);
 
                                          ************************************
***********************************
                                        }
                               }

                          }
                     }
              }
           }
        }
      }
      }
    }
0
Comment
Question by:shy_amar
  • 3
6 Comments
 
LVL 49

Expert Comment

by:DanRollins
ID: 9714412
I did not check your code.
If you can use that code to insert a different type of node (say, a <P>) then the problem is probably related to the the fact that the <BASE> tag gets special treatment.  It must be included in the <HEAD> container before any other URLs are accessed.

You could try getElementByTagName to get the HEAD tag (it is a member of the all collection) and then add the node as a child.  Or get its innerHTML and insert the <base>...</base> text.

But I'm not at all certain that that would work... Something would need to be done to make the browser completely refresh the screen since virtually everything on it could change.

=-=-=-=-=-=-=-=-=-=
If you are having trouble inserting other tags, then let me know and I'll check your code.

-- Dan
0
 

Author Comment

by:shy_amar
ID: 9714790
I did try to insert a different tag but I still get the same error code.
Although I don't use the getElementByTagName , I use the  pIHTMLElementCollection->tags(v,&pDisp ) -
the same principle (this is only partial code, but relevant to the problem).

to make is easy for you, I attached the entire function
I would appreciate your help.

HRESULT                  hResult;
VARIANT                  v;
BSTR                  bstrWebPageDomain;
CString                  strWebPageDomain("");
IHTMLElementCollection*      pIHTMLElementCollection;
IHTMLElement*            pHTMLElement;
IHTMLElement*            pNewHTMLElement;
IHTMLDOMNode*            pHTMLDOMNode;
IHTMLDOMNode*            pNewHTMLDOMNode;
IHTMLDOMNode*            pNew2HTMLDOMNode;
IDispatch*                  pDisp;
long                  lCollectionSize;
BOOL                  bIsOK;
BSTR                  bstrTemp;


hResult                 = S_OK;;
pIHTMLElementCollection = NULL;
pHTMLElement           = NULL;
pNewHTMLElement           = NULL;
pHTMLDOMNode           = NULL;
pNewHTMLDOMNode           = NULL;
pNew2HTMLDOMNode           = NULL;
pDisp                 = NULL;
lCollectionSize           = 0;
bIsOK                 = FALSE;
      

hResult =  pIHTMLDocument2->get_domain(&bstrWebPageDomain);
      if (hResult == S_OK)
      {
            strWebPageDomain = bstrWebPageDomain;
            SysFreeString(bstrWebPageDomain);

            hResult = pHiddenIHTMLDocument2->get_all(&pIHTMLElementCollection);
            if (hResult == S_OK)
            {
                  v.vt = VT_BSTR;
                  v.bstrVal =  SysAllocString(L"HEAD");

                  hResult = pIHTMLElementCollection->tags(v,&pDisp );
                  if (hResult == S_OK)
                  {
                        hResult = pDisp->QueryInterface(IID_IHTMLElementCollection, (void**)&pIHTMLElementCollection);
                        if (hResult == S_OK)
                        {
                              pIHTMLElementCollection->get_length(&lCollectionSize);
                                                
                              v.vt      = VT_I4;
                              v.lVal      = 0;
                              
                              hResult = pIHTMLElementCollection->item(v,v,&pDisp);
                              
                              if (hResult == S_OK && pDisp != NULL)
                              {
                                    hResult = pDisp->QueryInterface(IID_IHTMLElement, (void**)&pHTMLElement);
                                    if (hResult == S_OK)
                                    {
                                          hResult = pHTMLElement->QueryInterface(IID_IHTMLDOMNode,(void**)&pHTMLDOMNode);
                              
                                          if (hResult == S_OK)
                                          {
                                                bstrTemp = SysAllocString(L"BASE");
                                                hResult = pIHTMLDocument2->createElement(bstrTemp, &pNewHTMLElement);
                                                SysFreeString(bstrTemp);

                                                if (hResult == S_OK)
                                                {
                                                      
                                                      bstrTemp = SysAllocString(L"href");
                                                      
                                                      v.vt = VT_BSTR;
                                                      v.bstrVal =  strWebPageDomain.GetBuffer();
                                                      
                                                      hResult = pNewHTMLElement->setAttribute(bstrTemp, v, 0);

                                                      SysFreeString(bstrTemp);

                                                      if (hResult == S_OK)
                                                      {
                                                            hResult = pNewHTMLElement->QueryInterface(IID_IHTMLDOMNode,(void**)&pNewHTMLDOMNode);
                                                            if (hResult == S_OK)
                                                            {
                                                                  long lType;
                                                                  pNewHTMLDOMNode->get_nodeType(&lType);
                                                                  
                                                                  if (hResult == S_OK)
                                                                  {
                                                                        pNew2HTMLDOMNode = NULL;
                                                                        hResult = pHTMLDOMNode->appendChild(pNewHTMLDOMNode,&pNew2HTMLDOMNode);
                                                                        if (hResult != S_OK)
                                                                        {
                                                                              int a=0;
                                                                        }
                                                                  }
      else
      {
                                                                  }
                                                            }
                                                            else
                                                            {
                                                                                                                        }
                                                      }
                                                      else
                                                      {
                                                      }

                                                }
                                                else
                                                {
                                                }
                                                
                                                
                                          }
                                          else
                                          {
                                          }
                                    }
                                    else
                                    {
                                    }

                              }
                              else
                              {
                              }
                                    
                              
                        }
                        else
                        {
                        }
                  }
                  else
                  {

                  }

            }
            else
            {
            }
      }
      else
      {
      }
0
 
LVL 49

Accepted Solution

by:
DanRollins earned 500 total points
ID: 9718817
Here is how I inject elements into an HTML document.  It is much less complicated:
void CDlgWebDlg::OnButton2()
{
      MSHTML::IHTMLDocument2Ptr pDoc= m_ctlBrowser.GetDocument();
      MSHTML::IHTMLElementCollectionPtr pAllElems= pDoc->all;

      long nLength= pAllElems->length;
      for (long j= 0; j<nLength; j++) {
            MSHTML::IHTMLElementPtr pElem= pAllElems->item( _variant_t(j,VT_I4) );
            CString sTag= pElem->getAttribute("tagName",0).bstrVal;
            if ( sTag == "DIV" ) {
                  CString sHtml= pElem->getAttribute(L"innerHTML",0).bstrVal;
                  sHtml.Insert(0,"<B>hi there</B" );
                  pElem->setAttribute(L"innerHTML",_variant_t((LPCSTR)sHtml),0 );
            }
      }
}

=-=-=-=-=-=-=-=-=-=-=-=-=
Note that the innertHTML od the <HEAD> object is documented as being read-only.  When I try
if ( sTag == "HEAD" ) {
      CString sHtml= pElem->getAttribute(L"innerHTML",0).bstrVal;
      sHtml.Insert(0,"<BASE HREF='www.wahtever.com'></BASE" );
      pElem->setAttribute(L"innerHTML",_variant_t((LPCSTR)sHtml),0 );

I also get an error.  I can avoid an error with
if ( sTag == "HEAD" ) {
      HRESULT hr= pElem->insertAdjacentHTML("afterBegin", L"<BASE HREF='http://www.experts-exchange.com'></BASE>");
      CString sHtml= pElem->getAttribute(L"innerHTML",0).bstrVal;

But it seems to have no effect... the
     <IMG SRC="/images/logo.gif">
in my test page does not change.  That is, the browser did not bother to redraw the entire page using the newly-inserted BASE tag.

-- Dan
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 9784182
hi shy_amar,
Do you need more help?  Please communicate.  Thanks!
-- Dan
0
 
LVL 9

Expert Comment

by:tinchos
ID: 10248839
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: DanRollins {http:#9718817}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MSVCR80.dll crash 2 134
c++ mixing int and strings in multi array 8 71
Compile GLUT with Visual Studio 2015 1 111
Issues with C++ Class 19 81
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now