1

I'm working in the MS cluster resource API and trying to initialize a property table. Here is the example code (all C++) right from MS docs:

//  Private property data.
//  Only MyTypeAlpha defines private properties.
    
    #define PROP_NAME__MAXUSERS L"MaxUsers"
    #define PROP_MIN__MAXUSERS        (-1)
    #define PROP_MAX__MAXUSERS        (256)
    #define PROP_DEFAULT__MAXUSERS    (8)
    
    typedef struct _PARAM_BLOCK_ALPHA
    {
        LONG nMaxUsers;
    } 
    PARAM_BLOCK_ALPHA, * PPARAM_BLOCK_ALPHA;
    

    RESUTIL_PROPERTY_ITEM PROP_TABLE_ALPHA[] =
    {
        {    
            PROP_NAME__MAXUSERS, 
            NULL, 
            CLUSPROP_FORMAT_LONG, 
            (DWORD) PROP_DEFAULT__MAXUSERS, 
            (DWORD) PROP_MIN__MAXUSERS, 
            (DWORD) PROP_MAX__MAXUSERS, 
            RESUTIL_PROPITEM_REQUIRED, 
            FIELD_OFFSET( PARAM_BLOCK_ALPHA, 
                          nMaxUsers ) 
        },

        { 0 }
    };

When I paste this code into my project I get the compiler error:

E0144 a value of type "const wchar_t *" cannot be used to initialize an entity of type "LPWSTR"

It doesn't like the PROP_NAME__MAXUSERS being passed in to the name data member which is a LPWSTR type.

I see this all over M/S examples, so I must be missing something fundamental.

Any ideas?

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Brian
  • 11
  • 2

1 Answers1

1

Errors like this are caused by the fact that the latest versions of Visual Studio's C++ compiler (since VS 2017, IIRC) use, by default, a stricter compliance with the C++ Standards than their compilers did when the examples such as the code you have posted were written. In particular, the use of a const wchar_t* (literal) type to directly initialize a (non-const) wchar_t* member variable is forbidden according to those strict standards.

There are two different approaches to tackle this problem. First, you can make 'new' variables, which are the required non-const type, like in the code below, where the (possibly local) ncPROP_NAME__MAXUSERS[] array is initialized with a copy of the data from the literal.


WCHAR ncPROP_NAME__MAXUSERS[] = PROP_NAME__MAXUSERS; // Initialze non-const array with the literal...

RESUTIL_PROPERTY_ITEM PROP_TABLE_ALPHA[] =
{
    {
        ncPROP_NAME__MAXUSERS, // ... and use a pointer to THAT array for the member
        NULL,
        CLUSPROP_FORMAT_LONG,
        (DWORD)PROP_DEFAULT__MAXUSERS,
        (DWORD)PROP_MIN__MAXUSERS,
        (DWORD)PROP_MAX__MAXUSERS,
        RESUTIL_PROPITEM_REQUIRED,
        FIELD_OFFSET(PARAM_BLOCK_ALPHA,
                      nMaxUsers)
    },

    { 0 }
};

Alternatively, if that would involve a great deal of code modification, you can set the compiler options in your project(s) to use the older, less strict standard compliance. Right-click on the project in the "Solution Explorer" and select "Properties;" then, on the "C/C++" Settings page, select the "Language" tag and set the "Conformance Mode" option to "No", as here:

enter image description here

For more information on the use of, and/or disabling of, this Conformance Mode option, see this document.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • 1
    Note, that the code in question is [valid C code](https://stackoverflow.com/q/2245664/1889329). Compiling as C would be another option, e.g. by changing the file extension to .c. – IInspectable Sep 05 '20 at 06:49
  • @IInspectable Interesting observation. However, both the OP and the M/S example [here](https://learn.microsoft.com/en-us/previous-versions/windows/desktop/mscs/defining-structures-and-constants) state that the code is C++. – Adrian Mole Sep 05 '20 at 07:54
  • All sample code on Microsoft's documentation site is tagged *"C++"*. The mundane reason is that the documentation system doesn't provide dedicated support for C. With most C code being valid C++ this is rarely an issue. This sample is a case where the distinction matters. – IInspectable Sep 05 '20 at 08:05
  • Hi,if this answer did help to you, please feel free to mark it to help people with the same issue. – Zeus Sep 10 '20 at 09:15