views:

149

answers:

3
wprintf(L"Selecting Audio Input Device: %s\n", 
                            varName.bstrVal);

if(0 == strcmp(varName.bstrVal, "IP Camera [JPEG/MJPEG]"))...

The above reports :

error C2664: 'strcmp' : cannot convert parameter 1 from 'BSTR' to 'const char *'
+1  A: 

You have to use wcscmp instead:

if(0 == wcscmp(varName.bstrVal, L"IP Camera [JPEG/MJPEG]"))
{
}

Here is a description of the BSTR data type, it has a length prefix and a real string part which is just an array of WCHAR characters. It also has 2 NULL terminators.

The only thing to look out for is that the BSTR data type can contain embedded NULLs in the string portion, so wcscmp will only work in the cases where the BSTR does not contain embedded NULLs (which is probably most cases).

Brian R. Bondy
Why some functions are prefixed with `w` while some are `wc`?
COMer
@COMer: I'm not sure but wcs stands for wide character string.
Brian R. Bondy
@COMer: I'm guessing "str" gets replaced with "wcs" (e.g. strcmp -> wcscmp, strdup -> wcsdup), but functions without "str" get a "w" inserted somewhere (e.g. printf -> wprintf, fopen -> _wfopen).
wj32
A: 

As a richer alternative to the C runtime, you could use the Unicode CompareString or CompareStringEx APIs in Win32. If you don't have charset issues to consider, wcscmp is fine though.

Steve Townsend
A: 

I always construct _bstr_t wrappers around BSTRs. It makes things quite a bit easier and more idiomatic:

if(std::string("IP Camera [JPEG/MJPEG]") ==
                   static_cast<const char*>( _bstr_t(varName.bstrVal) )
{
}
Pedro d'Aquino
Not such a great idea: the _bstr_t constructor makes a full-length copy of the string in varName.bstrVal.
Sebastian