Advertisement

11.28.2005 at 01:28PM PST, ID: 21645531
[x]
Attachment Details

Cannot kill Excel processes when using automation with visual c++ .net

Asked by mit526 in Microsoft Visual C++.Net

Tags: excel

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_NULL, &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
[+][-]11.28.2005 at 04:12PM PST, ID: 15376702

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Microsoft Visual C++.Net
Tags: excel
Sign Up Now!
Solution Provided By: OnegaZhang
Participating Experts: 2
Solution Grade: B
 
 
[+][-]12.01.2005 at 11:17AM PST, ID: 15399038

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]12.02.2005 at 02:17AM PST, ID: 15403539

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12.02.2005 at 05:23AM PST, ID: 15404231

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12.06.2005 at 06:22AM PST, ID: 15427564

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12.15.2005 at 02:44PM PST, ID: 15494429

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]12.15.2005 at 05:10PM PST, ID: 15495209

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32