Hi,
I am having a problem of killing the excel process when I am automating through visual c++ .net. I'm not sure if it matters but I am using microsoft office 2003. The main function that is used to implement all the excel tasks is the following:
/**
* The function which does the real job getting and putting data
* Takes the variable number of arguments
* params:
* autoType - is DISPATCH_PROPERTYPUT - put, or DISPATCH_PROPERTYGET - get
* pvResult - retuns data into
* pDisp - dispatch object we invoke the method on
* ptName - method/type of obj to do/get/put
* cArgs - number of argument the method takes (0 if none)
*/
HRESULT CExcelWrap::AutoWrap(int autoType, VARIANT *pvResult, IDispatch *pDisp, LPOLESTR ptName, int cArgs...)
{
// Begin variable-argument list...
va_list marker;
va_start(marker, cArgs);
if(!pDisp) {
MessageBox(NULL, "NULL IDispatch passed to AutoWrap()", "Error", 0x10010);
return -1;
//_exit(0);
}
// Variables used...
DISPPARAMS dp = { NULL, NULL, 0, 0 };
DISPID dispidNamed = DISPID_PROPERTYPUT;
DISPID dispID;
HRESULT hr;
char buf[200];
char szName[200];
// Convert down to ANSI
WideCharToMultiByte(CP_ACP
, 0, ptName, -1, szName, 256, NULL, NULL);
// Get DISPID for name passed...
hr = pDisp->GetIDsOfNames(IID_N
ULL, &ptName, 1, LOCALE_USER_DEFAULT, &dispID);
if(FAILED(hr))
{
sprintf(buf, "IDispatch::GetIDsOfNames(
\"%s\") failed w/err 0x%08lx", szName, hr);
MessageBox(NULL, buf, "AutoWrap()", 0x10010);
//_exit(0);
return hr;
}
// Allocate memory for arguments...
VARIANT *pArgs = new VARIANT[cArgs+1];
// Extract arguments...
for(int i=0; i<cArgs; i++)
{
pArgs[i] = va_arg(marker, VARIANT);
}
// Build DISPPARAMS
dp.cArgs = cArgs;
dp.rgvarg = pArgs;
// Handle special-case for property-puts!
if(autoType & DISPATCH_PROPERTYPUT)
{
dp.cNamedArgs = 1;
dp.rgdispidNamedArgs = &dispidNamed;
}
// Make the call!
hr = pDisp->Invoke(dispID, IID_NULL, LOCALE_SYSTEM_DEFAULT, autoType, &dp, pvResult, NULL, NULL);
if(FAILED(hr))
{
sprintf(buf, "IDispatch::Invoke(\"%s\"=
%08lx) failed w/err 0x%08lx", szName, dispID, hr);
MessageBox(NULL, buf, "AutoWrap()", 0x10010);
return hr;
}
// End variable-argument section...
va_end(marker);
delete [] pArgs;
return hr;
}
But whenever I call the function:
void CExcelWrap::Quit()
{
AutoWrap(DISPATCH_METHOD, NULL, m_pXL, L"Quit", 0);
}
I don't get an error, but the excel process in the task manager never closes. Is there a way to kill the process so that when I run my application a couple of times I don't get a crash.
Thanks,
mpat
Start Free Trial