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.

Recommended Answers

All 4 Replies

Why do you want it to be a pointer?

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);
}

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.