2

If i call my COM-Method with something like this

d.someMethod(string, doule, ref string);

I get the error mentioned above. the method thats called is something like this

STDMETHODIMP SomeClass::someMethod(BSTR, DOUBLE, BSTR*)

As long as i dont assign some value to BSTR* it works fine.

EDIT: IDL Description

interface IDistanceClass : IDispatch{
    [id(1), helpstring("some helpstring")] HRESULT someMethod([in] BSTR firstarg, [in] DOUBLE secondarg, [in,out] BSTR* returnme);
};

EDIT2: As long as i pass only 1 Character like *returnme = "T" it works fine. But when it needs to be a string it throws, even if i assign a pointer to a string i get an error.

Andreas
  • 51
  • 1
  • 7
  • You have to document your question better. Post the declaration in your IDL and what you see for the method when you open the interop library in ildasm.exe. The attributes matter a great deal. – Hans Passant Dec 14 '12 at 13:01
  • I am sorry but i dont know what you mean with that ildasm.exe ? – Andreas Dec 14 '12 at 13:06
  • 2
    Next show how you used SysAllocString() to allocate the string you return and how you assigned it to the argument. – Hans Passant Dec 14 '12 at 13:37
  • 1
    as Hans mentioned, you should use SysAllocString function to allocate memory for returned string: `*returnme = SysAllocString(L"String");` – Serik Dec 14 '12 at 16:53
  • @Serik Thanks for the advice, but you should post that as an answer so that i can reward you :-) Another question: how do i assign a string to be recognized in SysAllocString. If i try something like *returnme = SysAllocString((BSTR)somestring.c_str()); it gets me an error. – Andreas Dec 17 '12 at 08:02
  • @Andreas It seems that somestring is of `string` type, please try `wstring` instead of it. If it is required the CA2W macro from ATL could be used for conversion. – Serik Dec 17 '12 at 08:50

1 Answers1

0

The solution is quite simple after you guys helping me out :-) As it turns out i need to do something like this:

string someString = "TestME";
_bstr_t s(someString.c_str());
*returnme = SysAllocString((BSTR)s);

Works well for me.

I am answering this myself so that maybe someone else with this issue wont need to search any longer. Hopefully this will help others too.

Andreas
  • 51
  • 1
  • 7