Tareg
Tareg

Reputation: 21

Compiling C++ code that includes <atlbase.h> results in error

I am recompiling a code from an old project that never failed me before:

#include <comutil.h>
#include <atlbase.h>

BSTR strToBSTR(std::string OriginalString)
{
    char *CharString = new char[OriginalString.size() + 1]; // &OriginalString[0];
    strcpy(CharString, OriginalString.c_str());

    BSTR ConvertedString = _com_util::ConvertStringToBSTR(CharString);

    return ConvertedString;
}

int calc(BSTR inputBSTRString, BSTR *outputBSTRString)
{
    char *inputCharString = _com_util::ConvertBSTRToString(inputBSTRString); 
    std::string inputString = inputCharString;                          
    std::string outputString;  

    outputString = inputString; // doing something

    // Returning the outputBSTRString string 
    CComBSTR outputBSTRStringTemp = strToBSTR(outputString);
    *outputBSTRString = SysAllocString(outputBSTRStringTemp.Detach());

    return 0;
} 

Now, whenever I compile the code, I receive tons of errors that all point to:

In file included from C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.37.32822/atlmfc/include/atlbase.h:65,
                 from BSTR.cpp:6,
                 from MainDriver.cpp:2:
error: expected constructor, destructor, or type conversion before '(' token
  675 | #define ATLPREFAST_SUPPRESS(x) __pragma(warning(push)) __pragma(warning(disable: x))

Obviously, the error is related to Microsoft's ATL library. I tried compiling the code from ARM64 Windows VM and also cross-compiling it from a M1 Mac. I also tried using different versions of the ATL library. I always end up with the same errors no matter what I try.

Upvotes: 1

Views: 353

Answers (1)

Tareg
Tareg

Reputation: 21

Answering my question in case someone runs into a similar problem:

  1. Added the following at the top of the code:

    #pragma comment(lib, "comsuppw.lib")
    
  2. Switched from mingw64 to cl.exe and selected the appropriate host-target combination on Windows VM.

Upvotes: 1

Related Questions