shy_amar
asked on
IHTMLDOMNode appendChild return E_INVALIDARG
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->t ags(v,&pDi sp );
if (hResult == S_OK)
{
hResult = pDisp->QueryInterface(IID_ IHTMLEleme ntCollecti on,(void** )&pIHTMLEl ementColle ction);
if (hResult == S_OK)
{
pIHTMLElementCollection->g et_length( &lCollecti onSize);
v.vt = VT_I4;
v.lVal = 0;
hResult = pIHTMLElementCollection->i tem(v,v,&p Disp);
if (hResult == S_OK && pDisp != NULL)
{
hResult = pDisp->QueryInterface(IID_ IHTMLEleme nt,(void** )&pHTMLEle ment);
if (hResult == S_OK)
{
hResult=pHTMLElement->Quer yInterface (IID_IHTML DOMNode,(v oid**)&pHT MLDOMNode) ;
if (hResult == S_OK)
{
bstrTemp = SysAllocString(L"BASE");
hResult=pIHTMLDocument2->c reateEleme nt(bstrTem p, &pNewHTMLElement);
SysFreeString(bstrTemp);
if (hResult == S_OK)
{
bstrTemp = SysAllocString(L"href");
v.vt = VT_BSTR;
v.bstrVal = strWebPageDomain.GetBuffer ();
hResult=pNewHTMLElement->s etAttribut e(bstrTemp , v, 0);
SysFreeString(bstrTemp);
if (hResult == S_OK)
{
hResult=pNewHTMLElement->Q ueryInterf ace(IID_IH TMLDOMNode ,(void**)& pNewHTMLDO MNode);
if (hResult == S_OK)
{
************************** *********
************************** **********
hResult =pHTMLDOMNode->appendChild (pNewHTMLD OMNode, &pNew2HTMLDOMNode);
************************** **********
************************** *********
}
}
}
}
}
}
}
}
}
}
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->t
if (hResult == S_OK)
{
hResult = pDisp->QueryInterface(IID_
if (hResult == S_OK)
{
pIHTMLElementCollection->g
v.vt = VT_I4;
v.lVal = 0;
hResult = pIHTMLElementCollection->i
if (hResult == S_OK && pDisp != NULL)
{
hResult = pDisp->QueryInterface(IID_
if (hResult == S_OK)
{
hResult=pHTMLElement->Quer
if (hResult == S_OK)
{
bstrTemp = SysAllocString(L"BASE");
hResult=pIHTMLDocument2->c
SysFreeString(bstrTemp);
if (hResult == S_OK)
{
bstrTemp = SysAllocString(L"href");
v.vt = VT_BSTR;
v.bstrVal = strWebPageDomain.GetBuffer
hResult=pNewHTMLElement->s
SysFreeString(bstrTemp);
if (hResult == S_OK)
{
hResult=pNewHTMLElement->Q
if (hResult == S_OK)
{
**************************
**************************
hResult =pHTMLDOMNode->appendChild
**************************
**************************
}
}
}
}
}
}
}
}
}
}
ASKER
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->t ags(v,&pDi sp ) -
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_domai n(&bstrWeb PageDomain );
if (hResult == S_OK)
{
strWebPageDomain = bstrWebPageDomain;
SysFreeString(bstrWebPageD omain);
hResult = pHiddenIHTMLDocument2->get _all(&pIHT MLElementC ollection) ;
if (hResult == S_OK)
{
v.vt = VT_BSTR;
v.bstrVal = SysAllocString(L"HEAD");
hResult = pIHTMLElementCollection->t ags(v,&pDi sp );
if (hResult == S_OK)
{
hResult = pDisp->QueryInterface(IID_ IHTMLEleme ntCollecti on, (void**)&pIHTMLElementColl ection);
if (hResult == S_OK)
{
pIHTMLElementCollection->g et_length( &lCollecti onSize);
v.vt = VT_I4;
v.lVal = 0;
hResult = pIHTMLElementCollection->i tem(v,v,&p Disp);
if (hResult == S_OK && pDisp != NULL)
{
hResult = pDisp->QueryInterface(IID_ IHTMLEleme nt, (void**)&pHTMLElement);
if (hResult == S_OK)
{
hResult = pHTMLElement->QueryInterfa ce(IID_IHT MLDOMNode, (void**)&p HTMLDOMNod e);
if (hResult == S_OK)
{
bstrTemp = SysAllocString(L"BASE");
hResult = pIHTMLDocument2->createEle ment(bstrT emp, &pNewHTMLElement);
SysFreeString(bstrTemp);
if (hResult == S_OK)
{
bstrTemp = SysAllocString(L"href");
v.vt = VT_BSTR;
v.bstrVal = strWebPageDomain.GetBuffer ();
hResult = pNewHTMLElement->setAttrib ute(bstrTe mp, v, 0);
SysFreeString(bstrTemp);
if (hResult == S_OK)
{
hResult = pNewHTMLElement->QueryInte rface(IID_ IHTMLDOMNo de,(void** )&pNewHTML DOMNode);
if (hResult == S_OK)
{
long lType;
pNewHTMLDOMNode->get_nodeT ype(&lType );
if (hResult == S_OK)
{
pNew2HTMLDOMNode = NULL;
hResult = pHTMLDOMNode->appendChild( pNewHTMLDO MNode,&pNe w2HTMLDOMN ode);
if (hResult != S_OK)
{
int a=0;
}
}
else
{
}
}
else
{
}
}
else
{
}
}
else
{
}
}
else
{
}
}
else
{
}
}
else
{
}
}
else
{
}
}
else
{
}
}
else
{
}
}
else
{
}
Although I don't use the getElementByTagName , I use the pIHTMLElementCollection->t
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_domai
if (hResult == S_OK)
{
strWebPageDomain = bstrWebPageDomain;
SysFreeString(bstrWebPageD
hResult = pHiddenIHTMLDocument2->get
if (hResult == S_OK)
{
v.vt = VT_BSTR;
v.bstrVal = SysAllocString(L"HEAD");
hResult = pIHTMLElementCollection->t
if (hResult == S_OK)
{
hResult = pDisp->QueryInterface(IID_
if (hResult == S_OK)
{
pIHTMLElementCollection->g
v.vt = VT_I4;
v.lVal = 0;
hResult = pIHTMLElementCollection->i
if (hResult == S_OK && pDisp != NULL)
{
hResult = pDisp->QueryInterface(IID_
if (hResult == S_OK)
{
hResult = pHTMLElement->QueryInterfa
if (hResult == S_OK)
{
bstrTemp = SysAllocString(L"BASE");
hResult = pIHTMLDocument2->createEle
SysFreeString(bstrTemp);
if (hResult == S_OK)
{
bstrTemp = SysAllocString(L"href");
v.vt = VT_BSTR;
v.bstrVal = strWebPageDomain.GetBuffer
hResult = pNewHTMLElement->setAttrib
SysFreeString(bstrTemp);
if (hResult == S_OK)
{
hResult = pNewHTMLElement->QueryInte
if (hResult == S_OK)
{
long lType;
pNewHTMLDOMNode->get_nodeT
if (hResult == S_OK)
{
pNew2HTMLDOMNode = NULL;
hResult = pHTMLDOMNode->appendChild(
if (hResult != S_OK)
{
int a=0;
}
}
else
{
}
}
else
{
}
}
else
{
}
}
else
{
}
}
else
{
}
}
else
{
}
}
else
{
}
}
else
{
}
}
else
{
}
}
else
{
}
}
else
{
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
hi shy_amar,
Do you need more help? Please communicate. Thanks!
-- Dan
Do you need more help? Please communicate. Thanks!
-- Dan
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
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
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