Why does CoUninitialize cause an error on exit?
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.
The meaning of CoInitialize
is to enter your thread into an apartment; and CoUninitialize
removes your thread from the apartment.
Using an interface pointer when you are not in an apartment causes the problem because you are only permitted to use a raw interface pointer in the apartment it was created in. (You can marshal the interface pointer to another apartment in order to use it in another apartment).
When you make a call through the interface pointer, and the object resides in another apartment (which is true in this case), your interface pointer makes calls into a proxy object in the apartment which then communicates via RPC with a stub in the destination apartment. If you had left the apartment (by doing CoUninitialize
) then this transport won't be available any more, causing your error.
If using in-process servers occasionally you can get away with doing CoUninitialize before calling Release because there is no transport layer involved, but it's not a good idea.
BTW, the second argument to CoInitialize
specifies whether you want to enter an STA (i.e. your thread will be the only thread in your apartment; and a new apartment is created when you do this), or the MTA (of which there is one per process).
The options are COINIT_APARTMENTTHREADED
and COINIT_MULTITHREADED
respectively; you specified 0
which is actually COINIT_MULTITHREADED
. IMHO it'd be clearer to use the symbolic name in your code rather than a magic number.
An elegant solution is to put CoInitializeEx and CoUninitialize in their own class. See this Raymond Chen article.