error location in comip.h
void _Release() throw()
{
if (m_pInterface! = NULL) {
m_pInterface – & gt; Release(); // Error reported here
}
}
When I encounter this problem myself, I use m_pInterface->; Release(); Comment out the program no longer reports an error.
Some other ways online:
1.m_pInterface-> Release () into m_pInterface = NULL from http://topic.csdn.net/u/20091120/16/8a9c8d91-9a39-416b-8d8d-8c2cd2a71d3e.html
2. In the program implementation: PTR ->; Release(); To PTR. Release (); From http://topic.csdn.net/u/20091120/16/8a9c8d91-9a39-416b-8d8d-8c2cd2a71d3e.html
3. One statement is that it is a matter of scope. I don’t know much about it. See: http://stackoverflow.com/questions/2653797/why-does-couninitialize-cause-an-error-on-exit
This method has been tried, but it also works. See the following instructions for details.
I’m working on a C++ application to read some data from an Excel file. I’ve got it working, but I’m confused about one part. Here’s the code (simplified to read only the first cell).
//Mostly copied from http://www.codeproject.com/KB/wtl/WTLExcel.aspx
#import "c:\Program Files\Common Files\Microsoft Shared\OFFICE11\MSO.DLL"
#import "c:\Program Files\Common Files\Microsoft Shared\VBA\VBA6\VBE6EXT.OLB"
#import "C:\Program Files\Microsoft Office\Office11\excel.exe" rename ("DialogBox","ExcelDialogBox") rename("RGB","ExcelRGB") rename("CopyFile", "ExcelCopyFile") rename("ReplaceText", "ExcelReplaceText") exclude("IFont", "IPicture")
_variant_t varOption((long) DISP_E_PARAMNOTFOUND, VT_ERROR);
int _tmain(int argc, _TCHAR* argv[])
{
DWORD dwCoInit = 0;
CoInitializeEx(NULL, dwCoInit);
Excel::_ApplicationPtr pExcel;
pExcel.CreateInstance(_T("Excel.Application"));
Excel::_WorkbookPtr pBook;
pBook = pExcel->Workbooks->Open("c:\\test.xls", varOption, varOption, varOption, varOption, varOption, varOption, varOption, varOption, varOption, varOption, varOption, varOption);
Excel::_WorksheetPtr pSheet = pBook->Sheets->Item[1];
Excel::RangePtr pRange = pSheet->GetRange(_bstr_t(_T("A1")));
_variant_t vItem = pRange->Value2;
printf(_bstr_t(vItem.bstrVal));
pBook->Close(VARIANT_FALSE);
pExcel->Quit();
//CoUninitialize();
return 0;
}
I had to comment out the call to CoUninitialize for the program to work. When CoUninitialize is uncommented, I get an access violation in the _Release function in comip.h on program exit.
Here’s the code from comip. h, for what it’s worth.
void _Release() throw()
{
if (m_pInterface != NULL) {
m_pInterface->Release();
}
}
I’m not very experienced with COM programming, so there’s probably something obvious I’m missing.
- Why does the call to CoUninitialize cause an exception? What are the consequences of not calling CoUninitialize? Am I doing something completely wrong here?
ANSWER!
The problem you are having is one of scope. The short answer is to move the CoInit and CoUninit into an outer scope from the Ptrs. For example:
//Mostly copied from http://www.codeproject.com/KB/wtl/WTLExcel.aspx
#import "c:\Program Files\Common Files\Microsoft Shared\OFFICE11\MSO.DLL"
#import "c:\Program Files\Common Files\Microsoft Shared\VBA\VBA6\VBE6EXT.OLB"
#import "C:\Program Files\Microsoft Office\Office11\excel.exe" rename ("DialogBox","ExcelDialogBox") rename("RGB","ExcelRGB") rename("CopyFile", "ExcelCopyFile") rename("ReplaceText", "ExcelReplaceText") exclude("IFont", "IPicture")
_variant_t varOption((long) DISP_E_PARAMNOTFOUND, VT_ERROR);
int _tmain(int argc, _TCHAR* argv[])
{
DWORD dwCoInit = 0;
CoInitializeEx(NULL, dwCoInit);
{
Excel::_ApplicationPtr pExcel;
pExcel.CreateInstance(_T("Excel.Application"));
Excel::_WorkbookPtr pBook;
pBook = pExcel->Workbooks->Open("c:\\test.xls", varOption, varOption, varOption, varOption, varOption, varOption, varOption, varOption, varOption, varOption, varOption, varOption);
Excel::_WorksheetPtr pSheet = pBook->Sheets->Item[1];
Excel::RangePtr pRange = pSheet->GetRange(_bstr_t(_T("A1")));
_variant_t vItem = pRange->Value2;
printf(_bstr_t(vItem.bstrVal));
pBook->Close(VARIANT_FALSE);
pExcel->Quit();
}
CoUninitialize();
return 0;
}
The longer answer is that the Ptrs destructors (which calls Release) are being called on exit from main. This is after CoUnit which, basically, shuts down the communication channel between your app and the COM object.
What are the consequences of not calling CoUnit?For short lived in-process COM servers, there really isn’t any negative consequence.
Read More:
- 0xc0000005: an access conflict occurred while reading location 0x00000020
- Opencv: 0xc0000005: an access conflict occurred while reading location 0x0000000010
- Three possibilities of “unhandled exception: 0xc0000005: access conflict when reading location 0x00000000”
- Bug resolution of 0xc0000005: access conflict occurred when reading location of 0x00000000.
- There is an unhandled exception at: 0xc0000005: an access conflict occurred while reading location 0x00000000.
- Unhandled exception: 0xc0000005: access violation while reading location 0x00000000
- C language problem: 0xc0000005: access conflict occurred when writing to location 0xffffcc.
- 0xc0000005: solution to access conflict when writing to location 0x00000000
- [debug notes] pointer error when STD:: vector is used in VTK “access conflict when reading location XXX”
- C / C + + error handling (document): unhandled exception: 0xc0000005: access violation while reading location 0x00000000
- Memory error unhandled exception: 0xc0000005: read location: 0x00000
- [Java] Java program error: exception_ ACCESS_ VIOLATION (0xc0000005)
- Solve the problem of VC 6.0 fatal error C1033: cannot open program database’.\debug\vc60.pdb’
- Error 1606 could not access network location% SystemDrive% / inetpub / wwwroot /
- Error 1606 Could Not Access Network Location %SystemDrive%/inetpub/wwwroot/
- Spring jacjson version conflict “nosuchme” thodError:com.fasterxml.jackson.databind.ObjectMapper.canSerialize “
- “Securityerror: error ා 2060: Security sandbox conflict: external interface caller XXX cannot access XXX”
- CMake_ Compiling VTK_ 9.0.0 running vtkcommoncolor DLL has access conflict
- CFNetwork internal error (0xc01a:/BuildRoot/Library/Caches/com.apple.xbs/Sources/CFNetwork/CFNetwork
- CFNetwork internal error (0xc01a:/BuildRoot/Library/Caches/com.apple.xbs/Sources/CFNetwork_Sim/CFNet