1

I am writing an ATL library (I'm very new at this) and I have a function that I want to return BSTR and VARIANT_BOOL. My function looks like this:

STDMETHODIMP myClass::GetResult(BSTR* myString, VARIANT_BOOL* contains) {
    std::pair<BSTR, bool> result = outQ.deQ();
    CComBSTR stringOut(result.first);
    CComVariant cont(result.second);
    *myString= stringOut.Detach();
    // this doesn't work, and I honestly don't know how to make it work
    //*contains = cont.Detach(contains);
    return S_OK;
}

I want to call this function from C# code. I had function working like this only with the BSTR and the C# code got the correct values and all. I just don't know how to return VARIANT_BOOL because there is no CComVariant_BOOL or whatsoever.

Any thoughts?

Everyone
  • 1,751
  • 13
  • 36
  • 1
    Why do you feel you need `CComVariant` here? Just to `*contains = result.second ? VARIANT_TRUE : VARIANT_FALSE;` And `*myString = result.first;`, for that matter. – Igor Tandetnik Mar 12 '17 at 21:51
  • I want to `Detach` it the same way I am detaching the BSTR – Everyone Mar 12 '17 at 21:52
  • @IgorTandetnik I told you I'm very new at this and I know I sound really naïve with these comments, but should it not be detached so I can transfer the ownership? – Everyone Mar 12 '17 at 21:54
  • Do you want to call a specific function regardless of whether there's a reason to or whether it's suitable, or do you want to solve the problem at hand in the most direct way possible? – Igor Tandetnik Mar 12 '17 at 21:54
  • Okay I will try doing it the way you suggested and will get back with the result. Thanks – Everyone Mar 12 '17 at 21:55
  • 2
    A simple boolean doesn't involve any resource management. There's nothing to own, and therefore nothing to transfer ownership of. `BSTR` is a different matter. Your current code makes a copy of the `BSTR` returned by `deQ`, and transfers ownership of that copy over to the caller. It's not clear to me what happens to the original, who owns it now; I suspect it's being leaked. – Igor Tandetnik Mar 12 '17 at 21:57
  • Oh that made it clear thanks. My current `BSTR` is part of a pair that I am creating inside the function, should it not get destroyed as the function returns and it goes out of scope? It isn't `BSTR*` what I am getting in the `pair`. – Everyone Mar 12 '17 at 21:59
  • 1
    `BSTR` is simply a typedef for `wchar_t*` - a dumb pointer. It cannot magically destroy itself. Hence the need for smart wrappers like `CComBSTR`. – Igor Tandetnik Mar 12 '17 at 22:11
  • `VARIANT_BOOL` is an integer type - they don't have ownership – M.M Mar 13 '17 at 03:10
  • @M.M Yeah Igor explained it well, thx – Everyone Mar 14 '17 at 02:00
  • @IgorTandetnik Could you make an answer out of this I want to select it as the answer of the question :) – Everyone Mar 14 '17 at 02:00

0 Answers0