Note that there are some explanatory texts on larger screens.

plurals
  1. POUse C++ Component Object Model in C#
    text
    copied!<p>I am trying to build a COM Library in C++, using a C# project for testing. Some methods need to return strings to the caller. On calling these methods from C# I get this: "Access violation reading at location ..."</p> <p>This is the C++ code from my testproject (apart from all the stuff generated by VS 2010 ATL)</p> <pre><code>//COMTest.idl [id(1)] HRESULT Test([out,retval] BSTR* ret); //Program2.h STDMETHOD(Test)(BSTR* ret); //Program2.cpp STDMETHODIMP CProgram2::Test(BSTR* ret) { BSTR tmp = (BSTR)CoTaskMemAlloc(sizeof(wchar_t) * 2); tmp[0] = L'H'; tmp[1] = L'\0'; *ret = (BSTR)tmp; return S_OK; } </code></pre> <p><br/> In C# I just referenced the DLL from the COM-Tab, turned "Embed Interop Types" off, because it caused errors, and ran this:</p> <pre><code>static void Main(string[] args) { COMTestLib.Program2Class instance = new COMTestLib.Program2Class(); string tmp = instance.Test(); //Where the error occurs Console.WriteLine(tmp); //This is not reached Console.Read(); } </code></pre> <p>The error occurs after leaving the Test-Method. I debugged the C++ code from within my C# project and the values are placed in the correct locations. I do not get the error if I try to return 0 (gives null in C#), even if I still allocate memory like in the example.</p> <p>I can not make sense of the address, which the access violation complains about. It is neither the address I am allocating nor any other address used in the method. What also seems weird to me is that the CoTaskMemAlloc-Function always returns addresses with the first byte set to zero (0x00XXXXXX), but that might just be a COM thing.</p> <p><br/> I ran out of ideas and I cant find much information on this (except for basic COM tutorials) anywhere. Can anyone help?</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