如何在 Win32 C++ 项目中使用 _malloca 而不是 _alloca?

How do I use _malloca instead of _alloca in Win32 C++ project?

我正在更新旧的 C++ DLL 项目。对于其中一个导出函数,有

BSTR __stdcall j2cs( const long lJulian, int bDMY, BSTR sDelim ) {
    USES_CONVERSION;
    int iDay, iMonth;
    long lYear;
    char chDate[20];
    char chInt[10];
    char * cDelim = W2A( sDelim );

W2A宏定义为

#define W2A(lpw) (\
    ((_lpw = lpw) == NULL) ? NULL : (\
        (_convert = (static_cast<int>(wcslen(_lpw))+1), \
        (_convert>INT_MAX/2) ? NULL : \
        ATLW2AHELPER((LPSTR) alloca(_convert*sizeof(WCHAR)), _lpw, _convert*sizeof(WCHAR), _acp))))

Visual Studio 2019 用以下编译器警告标记 W2A 宏

Warning C6255   _alloca indicates failure by raising a stack overflow exception.  Consider using _malloca instead.  

我该如何对 W2A 宏进行建议的更改?还是我应该忽略警告?

稍后

我的 USES_CONVERSION 宏定义为

#define USES_CONVERSION int _convert = 0; (_convert); UINT _acp = ATL::_AtlGetConversionACP() /*CP_THREAD_ACP*/; (_acp); LPCWSTR _lpw = NULL; (_lpw); LPCSTR _lpa = NULL; (_lpa)

W2A 无法更改为使用 malloc。您必须在所有使用 W2A 的地方添加 free。理想的替代方案是 std::vector 代替 _alloca

更新宏USES_CONVERSION,使其包含std::vector<WCHAR> _buffer;并更新宏W2A:

#define USES_CONVERSION int _convert = 0; (_convert); std::vector<WCHAR> _buffer; (_buffer); UINT _acp = ATL::_AtlGetConversionACP() /*CP_THREAD_ACP*/; (_acp); LPCWSTR _lpw = NULL; (_lpw); LPCSTR _lpa = NULL; (_lpa)

#define W2A(lpw) (\
    ((_lpw = lpw) == NULL) ? NULL : (\
        (_convert = (static_cast<int>(wcslen(_lpw))+1), \
        (_convert>INT_MAX/2) ? NULL : \
        (_buffer.resize(_convert), \
        ATLW2AHELPER((LPSTR) _buffer.data(), _lpw, _convert*sizeof(WCHAR), _acp)))))