PRB: Parameter that Takes a Reference to a String Does Not Change After Failed Call to Unmanaged Method (317533)



The information in this article applies to:

  • Microsoft Windows .NET Framework 1.1
  • Microsoft .NET Framework 1.0

This article was previously published under Q317533

SYMPTOMS

When a managed application calls a method of an unmanaged COM server that takes a reference to a string as a parameter, the changes that are made to the buffer that the managed client passes are not marshaled back to the managed client if the method fails (for example, if the method returns a failed HRESULT such as E_FAIL).

STATUS

This behavior is by design.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Create an unmanaged COM server with one method that has the following implementation:
    //IDL
    [id(1), helpstring("method GetName")] HRESULT GetName([in,out] BSTR *str);
    					
    //Method implementation.
    STDMETHODIMP CEmployee::GetName(BSTR *str)
    {
          *str = ::SysAllocString(L"Test Name");
    
           //Fail here for testing.
           return E_FAIL;
    }
    					
  2. Call this method from a managed client, as follows:
    EmployeeClass obj = new EmployeeClass();
    string empName="No Name";
    obj.GetName(ref empName);
    					
When you call the method, the method generates an exception error because of the failed E_FAIL HRESULT. If you break the program and examine the content of empName, you find that it is not changed. You can see similar behavior if you use the System.Text.StringBuilder class instead of the System.String class. To use the System.Text.StringBuilder class, change the Microsoft intermediate language (MSIL), and then rebuild the runtime callable wrapper.

Modification Type:MinorLast Reviewed:5/28/2003
Keywords:kbCOMInterop kbinterop kbprb KB317533