Unhandled exception at 0x78F90870 (ucrtbased.dll) in Metrics_Alpha.exe: 0xC0000005: Access violation reading location 0x00000000

-1

Trying to work with Excel files using C++ (as a pretty "less-than-professional" programmer). It seems I brought all the libraries in correctly, etc; if I need to post those and included SDKs, I can. However, I'm running into the above error.

Here is where I am working with the Excel file directly:

void themSheets() {
    cout << endl << endl;
    try {
        Excel::_ApplicationPtr pXL;
        pXL->Workbooks->Open(L"C:/Users/Person/source/repos/Metrics_Alpha/Metrics_Alpha/poooopy.xlsx");
        pXL->PutVisible(0, FALSE);

        Excel::_WorksheetPtr pSheet = pXL->ActiveSheet;
        pSheet->Name = L"Sheet1";
        Excel::RangePtr pRange = pSheet->Cells;

        pRange->Item[1][1] = 5.21;
        double apples = pRange->Item[1][1];
        cout << endl << apples;
        pXL->Workbooks->Close();
    }
    catch (_com_error & error) {
        cout << error.Description() << endl;
    }
}

And here is where the exception happens:

    _NODISCARD static _CONSTEXPR17 size_t length(_In_z_ const _Elem* const _First) noexcept /* strengthened */ {
    // find length of null-terminated string
#if _HAS_CXX17
#ifdef __cpp_char8_t
        if constexpr (is_same_v<_Elem, char8_t>) {
#if _HAS_U8_INTRINSICS
            return __builtin_u8strlen(_First);
#else // ^^^ use u8 intrinsics / no u8 intrinsics vvv
            return _Primary_char_traits::length(_First);
#endif // _HAS_U8_INTRINSICS
        } else
#endif // __cpp_char8_t
        {
            return __builtin_strlen(_First);
        }
#else // _HAS_CXX17
        return _CSTD strlen(reinterpret_cast<const char*>(_First)); **<--Happens right here**
#endif // _HAS_CXX17
    }
c++
excel
windows
visual-c++
asked on Stack Overflow Feb 5, 2021 by Ed2Cute • edited Feb 10, 2021 by IInspectable

1 Answer

0

The major issue was I was missing this block of code in my main:

HRESULT hr = CoInitializeEx(0, COINIT_MULTITHREADED);
    if (FAILED(hr))
    {
        cout << "Failed to initialize COM library. Error code = 0x"
            << hex << hr << endl;
        return hr;
    }

    Excel::_ApplicationPtr pXL;

    if (FAILED(pXL.CreateInstance("Excel.Application")))
    {
        cout << "Failed to initialize Excel::_Application!" << endl;
        return 0;
    }
answered on Stack Overflow Feb 18, 2021 by Ed2Cute

User contributions licensed under CC BY-SA 3.0