Loading ...
Sorry, an error occurred while loading the content.

File attachment in C++

Expand Messages
  • Nilay Hazra
    Hi, I m using MS SOAP Toolkit 3.0 and trying to write a client application and a service using VC++ 6.0 for uploading a XML file to the server. For the client
    Message 1 of 1 , Sep 4, 2003
    • 0 Attachment
      Hi,
      I'm using MS SOAP Toolkit 3.0 and trying to write a client application and a service using VC++ 6.0 for uploading a XML file to the server.
       
      For the client application, I'm using the following code. Here I'm calling the service method "ProcessAttachment" (which takes the attachment (MSSOAPLib30.IAttachment) as the first parameter and the file name (BSTR) as the second parameter).
       
      ...
      void UploadFile()
      {
       
      printf(_T("\nStart\n"));
        USES_CONVERSION;
       
        CComPtr<ISoapClient> spSOAPClient;
        HRESULT hr = spSOAPClient.CoCreateInstance(CLSID_SoapClient30);
        CHECK_HR(hr);
       
        printf(_T("\nCreate Client Succeeded\n"));
       
        hr = spSOAPClient->MSSoapInit(_bstr_t(g_lpszWSDL_URL), L"", L"", _bstr_t(g_lpszWSML_URL));
                  // g_lpszWSDL_URL, g_lpszWSML_URL declared somewhere
        CHECK_HR(hr);
       
        printf(_T("\nInit Client Succeeded\n"));
       
        CComPtr<IFileAttachment> spFileAttachIn;
        hr = spFileAttachIn.CoCreateInstance(CLSID_FileAttachment30);
        CHECK_HR(hr);
       
        hr = spFileAttachIn->put_FileName(_bstr_t(g_lpszFileName)); // g_lpszFileName declared somewhere
        CHECK_HR(hr);
       
        //  Call the Web Service method
        WCHAR *pwcMethodName = L"ProcessAttachment";
        DISPID dispidFn = 0;
        hr  = spSOAPClient->GetIDsOfNames(IID_NULL, &pwcMethodName, 1,
          LOCALE_SYSTEM_DEFAULT, &dispidFn);
        CHECK_HR(hr);

        unsigned int uArgErr;
        VARIANT varg[2];
        varg[0].vt = VT_DISPATCH;
        varg[0].pdispVal = spFileAttachIn;
        varg[1].vt = VT_BSTR;
        varg[1].bstrVal = _bstr_t("xyz.xml");
       
        DISPPARAMS params;
        params.cArgs = 2;
        params.rgvarg = varg;
        params.cNamedArgs    = 0;
        params.rgdispidNamedArgs = NULL;
        _variant_t result;
        uArgErr = -1;
       
        EXCEPINFO excepInfo;
        memset(&excepInfo, 0, sizeof(excepInfo));
        hr  = spSOAPClient->Invoke(dispidFn, IID_NULL, LOCALE_SYSTEM_DEFAULT,
          DISPATCH_METHOD, &params, &result, &excepInfo, &uArgErr);
      printf(_T("%s\n"), W2A(spSOAPClient->FaultString));
        CHECK_HR(hr);
        
        return;
      }
      ...
       
      For the service, I'm using the following code.
      ...
      STDMETHODIMP CUpload::ProcessAttachment(VARIANT fileData, BSTR fileName)
      {
       CComPtr<IReceivedAttachment> spReceivedAttachment;
        HRESULT hr = spReceivedAttachment.CoCreateInstance(L"MSSOAPLib30.IReceivedAttachment");
        // if (FAILED(hr)) return error
       
       IReceivedAttachment* pIX = NULL;
       if(fileData.vt==VT_DISPATCH) {
        hr = (fileData.pdispVal)->QueryInterface(IID_IReceivedAttachment, (void **)&pIX);
        // if (FAILED(hr)) return error
       
        spReceivedAttachment = pIX;
        spReceivedAttachment->SaveToFile(fileName, true);
        return S_OK;
       }
       else
        // Not OK, return error
      }
      ...
       
      I've also generated WSDL file using WSDL generator tool provided with the SOAP toolkit. Some part of it (which maps the data type) is in the following.
      ...
      <message name='Upload.ProcessAttachment'>
        <part name='fileData' type='xsd:anyType'/>
        <part name='fileName' type='xsd:string'/>
       </message>
      ...
       
      When I'm running the client application, I'm getting the following Fault String "SoapMapper:Converting data for SoapMapper failed inside the typemapper" from the Invoke method.
       
      How can I fix it? Please help.
       
      nhazra
    Your message has been successfully submitted and would be delivered to recipients shortly.