Home > Net >  C 'default argument': cannot convert from 'const wchar_t' to 'BSTR'
C 'default argument': cannot convert from 'const wchar_t' to 'BSTR'

Time:12-28

  1. I start a C Project that uses the "Visual Studio 2017 - Windows XP (v141_xp)" Platform Toolset
  2. Set Runtime Library to "Multi-Threaded (/MT)"
  3. Change "Debug" to "Release"
  4. Add #include "<Wbemidl.h>" to the source.
  5. CTRL F5 (aka Build)

Now, I am aware that platform set is deprecated but, either way, the default x64 build has no issues. However, when I change to "x86" from "x64" (as that's what I want), it then produces numerous errors like the following:

C 'default argument': cannot convert from 'const wchar_t' to 'BSTR'

You might ask what am I trying to do? Maybe some of my code is bad? You should not convert wchar_t to BSTR as it is not defined what will be the result?

But these aren;t my concerns, here. I didn't add any code – just the single header include (I left the "Hello World" part).

I didn't explicitly convert anything as the error is in the Microsoft system headers, not in my Code. I didn't add anything to do with QT nor do I need or want QT so that's not the issue.

I just for now want to compile my "Hello World" Program using the <Wbemidl.h> header. (Of course, I don't need that header to compile it but, if I can compile it without issues, I most likely then can use the library without issues.) I wanted to make this as simple as possible, hence why it just is "Hello World" (a MCVE).

I tried adding the following but without success:

#pragma comment(lib, "Shlwapi.lib")
#pragma comment(lib, "wbemuuid.lib")

My best guess is that I didn't install something and or its just deprecated; the funny thing is "2015 Visual Studio (140_xp)" works.

I just want to compile my C "Hello World" Program with the Header "<Wbemidl.h>" , Compiled in "x86", Toolset of "Visual Studio 2017 - Windows XP (v141_xp)" and Runtime Library as "Multi-Threaded (/MT)"

My code (MCVE):

#include <iostream>
#include <Wbemidl.h>    

int main()
{
    std::cout << "Hello World!\n";
}

Microsofts Code ERROR

Other things I tried:

  1. Strict QT strings tags
  2. Including other stuff
  3. Changing from "Multi-Threaded (/MT)" to "Multi-threaded DLL (/MD)"
  4. Tried making 4 Different projects

CodePudding user response:

Since (IIRC) VS 2019 (and certainly for VS 2022), there is a C compiler setting that (by default) forces much stricter adherence to the language standard. That setting ("Conformance Mode") will prevent the conversion (const w_char[] to BSTR) that is being used in the (older) SDK header files.

This option was already present in VS 2017 but was disabled by default; in VS 2015 (which, you say, works without the errors), the option is simply not present. Thus, when you change the toolset from VS 2022 to VS 2017, you are (unwittingly) enabling that "conformance mode" option.

To disable these errors, you need to turn off conformance mode. In your project settings, go to the "C/C " … "Language" property page and set "Conformance Mode" to "No":

enter image description here

There will very likely still be numerous other warnings but the system headers will, at least, compile without errors.

Note that, when using the newer toolsets, the MSVC compiler uses system header files from a newer SDK; in those headers, the issues have been properly addressed, so conformance mode can be used.

  • Related