Microsoft KB Archive/317533

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

Article ID: 317533

Article Last Modified on 2/23/2007

-

APPLIES TO


 * Microsoft .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.



Steps to Reproduce the Behavior
  Create an unmanaged COM server with one method that has the following implementation: //IDL [id(1), helpstring(&quot;method GetName&quot;)] HRESULT GetName([in,out] BSTR *str); //Method implementation. STDMETHODIMP CEmployee::GetName(BSTR *str) {     *str = ::SysAllocString(L&quot;Test Name&quot;);

//Fail here for testing. return E_FAIL; }                     Call this method from a managed client, as follows: EmployeeClass obj = new EmployeeClass; string empName=&quot;No Name&quot;; 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.

Keywords: kbcominterop kbinterop kbprb KB317533

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.