Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. XMLHttpRequestPtr creation failed in thread?

XMLHttpRequestPtr creation failed in thread?

Scheduled Pinned Locked Moved C / C++ / MFC
csharpc++comdebugginghelp
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    ritz1234
    wrote on last edited by
    #1

    Hello Dear Friends, 1) I am creating an application which allows its user to log in to website. 2) For that I call the webservice(Written in C#.NET) from the MFC. 3) To call the webservices I use the XMLHttpRequest and it is working fine if I call the webservice using XMLHttpRequest with the member function of the class, However this method doesn't allow the parent program to continue until I get the webservice response. 4) So I am creating a thread and calling the webservice from the thread the problem is in the thread it won't allow to create the COM object of XMLHttpRequestPtr.Can anybody tell me what could be the reason that the creation of the COM object fails. 5) Please see the code below.

    void LOGINTOWEB(void *Params)
    {
    CMFToolbar *m_pToolbar=(CMFToolbar*)Params;
    try
    {
    CSingleLock m_lock(&(m_pToolbar->m_sema));
    m_lock.Lock();
    m_pToolbar->LoginToWeb1(m_pToolbar->m_strqpWebUser,m_pToolbar->m_strqpWebPass,m_pToolbar->m_strqpWebOrg,m_pToolbar->m_strqpWebUrl);
    m_lock.Unlock();
    _endthread();
    }
    catch(...)
    {
    }
    }

    And here is the member function of the class I've debug the code it fails in the creation of the COM object IXMLHttpRequestPrt

    bool CMFToolbar::LoginToWeb1(CString UserName,CString Password,CString Organization,CString Url)
    {
    bool bFlg=false;
    try
    {
    CString params;
    /* Here the exception is generated when called from the thread. When this function called without any thread it will run.Why it fails in thread?????*/
    MSXML::IXMLHttpRequestPtr httpReq(__uuidof(XMLHTTPRequest));

    	\_bstr\_t  HTTPMethod ;
    	\_variant\_t noAsync = \_variant\_t( (bool)false );
    	\_variant\_t user=\_variant\_t((CString)UserName);
    	\_variant\_t pass=\_variant\_t((CString)Password);
    
    	CString strUrl=Url;
    	strUrl+=QPWEB\_SUFFIX;
    	
    	//MessageBox(strUrl);
    	\_bstr\_t url(strUrl.GetBuffer(strUrl.GetLength())); 
    
    	HTTPMethod = \_bstr\_t("GET");
    	httpReq->open(HTTPMethod,url,noAsync,vtMissing,vtMissing);
    	httpReq->setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    	httpReq->setRequestHeader("Content-Length","0");
    	
    	params.Empty(); 
    	params="UserName=";
    	params+=UserName;
    	params+="&Password=";
    	params+=Password;
    	params+="&Organization=";
    	params+=Organization;
    
    	//MessageBox(params);
    
    	VARIANT vRequest;
    	vRequest.vt = VT\_BSTR;
    	vRequest.bstrVal = params.AllocSysString();
    
    	httpReq->send(vRequest);
    	BSTR strText;
    	int nPos1,nPos2;
    	CSt
    
    C 1 Reply Last reply
    0
    • R ritz1234

      Hello Dear Friends, 1) I am creating an application which allows its user to log in to website. 2) For that I call the webservice(Written in C#.NET) from the MFC. 3) To call the webservices I use the XMLHttpRequest and it is working fine if I call the webservice using XMLHttpRequest with the member function of the class, However this method doesn't allow the parent program to continue until I get the webservice response. 4) So I am creating a thread and calling the webservice from the thread the problem is in the thread it won't allow to create the COM object of XMLHttpRequestPtr.Can anybody tell me what could be the reason that the creation of the COM object fails. 5) Please see the code below.

      void LOGINTOWEB(void *Params)
      {
      CMFToolbar *m_pToolbar=(CMFToolbar*)Params;
      try
      {
      CSingleLock m_lock(&(m_pToolbar->m_sema));
      m_lock.Lock();
      m_pToolbar->LoginToWeb1(m_pToolbar->m_strqpWebUser,m_pToolbar->m_strqpWebPass,m_pToolbar->m_strqpWebOrg,m_pToolbar->m_strqpWebUrl);
      m_lock.Unlock();
      _endthread();
      }
      catch(...)
      {
      }
      }

      And here is the member function of the class I've debug the code it fails in the creation of the COM object IXMLHttpRequestPrt

      bool CMFToolbar::LoginToWeb1(CString UserName,CString Password,CString Organization,CString Url)
      {
      bool bFlg=false;
      try
      {
      CString params;
      /* Here the exception is generated when called from the thread. When this function called without any thread it will run.Why it fails in thread?????*/
      MSXML::IXMLHttpRequestPtr httpReq(__uuidof(XMLHTTPRequest));

      	\_bstr\_t  HTTPMethod ;
      	\_variant\_t noAsync = \_variant\_t( (bool)false );
      	\_variant\_t user=\_variant\_t((CString)UserName);
      	\_variant\_t pass=\_variant\_t((CString)Password);
      
      	CString strUrl=Url;
      	strUrl+=QPWEB\_SUFFIX;
      	
      	//MessageBox(strUrl);
      	\_bstr\_t url(strUrl.GetBuffer(strUrl.GetLength())); 
      
      	HTTPMethod = \_bstr\_t("GET");
      	httpReq->open(HTTPMethod,url,noAsync,vtMissing,vtMissing);
      	httpReq->setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      	httpReq->setRequestHeader("Content-Length","0");
      	
      	params.Empty(); 
      	params="UserName=";
      	params+=UserName;
      	params+="&Password=";
      	params+=Password;
      	params+="&Organization=";
      	params+=Organization;
      
      	//MessageBox(params);
      
      	VARIANT vRequest;
      	vRequest.vt = VT\_BSTR;
      	vRequest.bstrVal = params.AllocSysString();
      
      	httpReq->send(vRequest);
      	BSTR strText;
      	int nPos1,nPos2;
      	CSt
      
      C Offline
      C Offline
      Code o mat
      wrote on last edited by
      #2

      I think i read somewhere that you need to initialize and uninitialize COM in every thread that will use it, so maybe try calling CoInitialize (and CoUninitialize) in your thread and see if it changes anything.

      > The problem with computers is that they do what you tell them to do and not what you want them to do. <

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups