Note that there are some explanatory texts on larger screens.

plurals
  1. POCOM fail to properly marshal a BSTR from a managed server to native client
    text
    copied!<p>I'm struggling with a scenario where I have a managed interface exposed through COM and consumed by a native client. I've managed to isolate the problem and it basically boils down to a string being improperly marshalled by the interop runtime, to simulate and reproduce this problem I've created a small project that looks like this:</p> <p><strong>Server:</strong></p> <pre><code>[ComVisible(true)] [Guid("5AF8A86E-B129-4FA0-8B6D-E9DF52DFDD84")] public interface IUrlSync { void GetUrl( [MarshalAs(UnmanagedType.BStr)] [Out] out string url ); } [ComVisible(true)] [Guid("5AF8A86E-B129-4FA0-8B6D-E9DF52DFDD85")] public class Urlsync : IUrlSync { private const string AddressHolderPath = "url.txt"; public Urlsync() { // nothing } public void GetUrl(out string url) { url = File.Exists(AddressHolderPath) ? File.ReadAllText(AddressHolderPath) : null; } } </code></pre> <p>after compiling this class and executing regasm + gacutil /i, I've built this small </p> <p><strong>Native Client:</strong></p> <pre><code>#include &lt;Windows.h&gt; #import "../comstr/bin/release/comstr.tlb" int main(int argc, char* argv[]) { CoInitialize(NULL); { BSTR bstr; HRESULT hr = S_OK; comstr::IUrlSyncPtr sync(__uuidof(comstr::Urlsync)); hr = sync-&gt;GetUrl(&amp;bstr); } CoUninitialize(); return 0; } </code></pre> <p>And here the value in hr is S_OK and bstr is set to NULL (0x000000).</p> <p>To make sure that the problem is with the marshalling itself I've build a </p> <p><strong>managed client:</strong></p> <p>That calls the UrlSync class from a different assembly:</p> <pre><code> string bstr; comstr.IUrlSync sync = new comstr.Urlsync(); sync.GetUrl(out bstr); Console.WriteLine("the url is: {0}", bstr); </code></pre> <p>and I'm getting the expected string. what I'm missing here?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload