Click here to Skip to main content
15,884,353 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a legacy program written in C that I am having to add calls to a COM object. The problem I am having is passing the data to the functions in the COM module. I can pass literals to the module but how can I convert the variables to BSTR?

Example with literals:

BSTR baseUrl = SysAllocString(L"www.google.com");
BSTR sessionName = SysAllocString(L"");
VARIANT_BOOL returnVal;
result = viewer->pVtbl->launchSession(viewer, baseUrl, sessionName, &returnVal);
if (result != S_OK)
{
    printf("COM call to WebViewer.launchSession failed\n");
    SysFreeString(baseUrl);
    SysFreeString(sessionName);
    return 1;
}


Instead of hard coding "www.google.com" I need to use variable defined as char [215].

What I have tried:

I found
_com_util::ConvertStringToBSTR(lpszText);
but I can't figure out how to use it in C. All the examples are for C++.
Posted
Updated 15-Oct-20 4:27am
Comments
KarstenK 16-Oct-20 5:34am    
converting isnt the correct word because you MUST create an new BSTR as the fellows answered.

The SysAllocString function (oleauto.h) - Win32 apps | Microsoft Docs[^] takes a pointer to a Unicode string. So if you convert the string variable via MultiByteToWideChar function (stringapiset.h) - Win32 apps | Microsoft Docs[^] it should work.
 
Share this answer
 
Comments
CPallini 15-Oct-20 13:32pm    
5.
Greg Atkinson 19-Oct-20 16:26pm    
Apparently there is more to a BSTR data type than just WideChar.
Richard MacCutchan 19-Oct-20 17:05pm    
Am I supposed to make something of that comment?
Greg Atkinson 20-Oct-20 8:22am    
In other words MultiByteToWideChar function does not work consistently.
Richard MacCutchan 20-Oct-20 8:52am    
Always does for me. If you have a problem with some code then please show us the code and explain what went wrong. Just saying, "it doesn't work", tells us nothing.
In C you can use
MultiByteToWideChar
Windows API
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900