Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

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

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
    }
question from:https://stackoverflow.com/questions/66057705/unhandled-exception-at-0x78f90870-ucrtbased-dll-in-metrics-alpha-exe-0xc00000

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...