Hi,
How do I correctly call a function which expect BSTR* as a parameter?

eg:

void FunctionA(BSTR* b)
{
    // Do something
}

Currently, my code is as follow:

void Caller()
{
    BSTR* pbstr;
    FunctionA(pbstr);
}

However, during compilation, I get warning "local variable pbstr' used without having been initialized".
How do I initialize the pbstr variable? Please advice. Thanks.

BSTR is already a pointer, so BSTR* is like writing char**. FunctionA() only needs BSTR* if it plans to allocate the memory for the variable declared in the calling function.

Here is a good explanation of how BSTR works. Scroll down the page and it will show you how to allocate the memory for the BSTR by using SysAllocString(), also that is not the only function that can be used for that purpose.

If you do not have control over how FunctionA() is declared (such as it is a library function that you don't have the source code) then you would pass the BSTR like this:

void Caller()
{
    BSTR pbstr;
    FunctionA(&pbstr);
}

The above assumes FunctionA() is going to allocate memory for the BSTR. If, on the otherhand, FunctionA() expects the BSTR to already be initialized with some string or data, then do something like this:

void Caller()
{
    BSTR pbstr = SysAllocString(L"Hello World");
    FunctionA(&pbstr);
}

Edited 5 Years Ago by Ancient Dragon: n/a

Thank you very much for the explanation and example. Really appreciate it.. Yeah, it is a library and I have no control over the declaration of FunctionA.

Make sure you understand what FunctionA() is going to do with that BSTR. If you allocate the memory like in my second code snippet and FunctionA() reallocates it then that will result in a memory leak.

This article has been dead for over six months. Start a new discussion instead.