[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.programming.threads

mfc multithreaded excel automation

rootd

8/9/2004 6:24:00 AM

Hello,

I have written this Excel Automation app ( multithreaded).
it crashes at the 3rd last line ( bleh->Quit();) with an
accessviolation. I have been playing around with it for a few days now
and I just dont know what to do anymore. could someone tell me please
how to fix it?

thanks,
hans

[code starts here]

#include "stdafx.h"

#include "automationmfc.h"
#include "automationmfcDlg.h"
#include ".\automationmfcdlg.h"
#include "CApplication.h"







COleVariant
covTrue((short)TRUE),
covFalse((short)FALSE),
covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);

DWORD WINAPI closeInstanceExcel(LPVOID);


void CautomationmfcDlg::OnBnClickedOk()
{

if(RunThreadInst((LPTHREAD_START_ROUTINE)closeInstanceExcel, 50000))
{
//something is wrong
}


OnOK();
}

bool CautomationmfcDlg::RunThreadInst(LPTHREAD_START_ROUTINE
myfunction, int Countcycles)
{

CApplication app;

CoInitialize(app);


if(!app.CreateDispatch("Excel.Application"))
{
// not started properly
}
else
{
// started ok
app.put_Visible(TRUE);

}



HRESULT hr;

IStream *gStream =NULL;
CApplication *myapp = NULL;

hr = CoInitialize(NULL);

if( FAILED(hr))
{
AfxMessageBox("failed");
}

HRESULT hr;
IStream *gStream;

hr = CoMarshalInterThreadInterfaceInStream(IID_IUnknown,app,&gStream);


m_ahThread[0] = CreateThread(0, 0, myfunction, &gStream, 0, 0);


UpdateData(TRUE);

for (int a = 0; a <= Countcycles; a++)
{
WaitForSingleObject(m_ahThread[0], 100);

DWORD dwWaitReturn;
GetExitCodeThread(m_ahThread[0], &dwWaitReturn);

if(STILL_ACTIVE == dwWaitReturn)
{
if (a == Countcycles)
{
// CloseHandle(m_ahThread[0]);
return true;
}
// m_csWorkerThread[0] = " Running when wait completed";
}
else
{
// CloseHandle(m_ahThread[0]);
break;
}
}


return false;

}



DWORD WINAPI CreateInstanceExcel(LPVOID params) //# Thread
Function
{
CoInitialize(NULL);

IStream ** ppStream = (IStream **) params;

HRESULT hr;
CApplication * bleh;
hr = CoGetInterfaceAndReleaseStream(*ppStream , IID_IUnknown,
reinterpret_cast<void**>( &bleh ) );

bleh->Quit();

return 0;

}

[code ends here]
1 Answer

Thomas Rydzynski

8/9/2004 5:49:00 PM

0

Nieznacznie u?miechaj?c sie pod nosem hans werner napisa3(a):
> Hello,
>
> I have written this Excel Automation app ( multithreaded).
> it crashes at the 3rd last line ( bleh->Quit();) with an
> accessviolation.
>
> [code starts here]
> [ cut ]
> #include "CApplication.h"
> [ cut ]
> DWORD WINAPI CreateInstanceExcel(LPVOID params)
> Function
> {
> CoInitialize(NULL);
>
> IStream ** ppStream = (IStream **) params;
>
> HRESULT hr;
> CApplication * bleh;
> hr = CoGetInterfaceAndReleaseStream(
> *ppStream ,
> IID_IUnknown,
> reinterpret_cast<void**>( &bleh )
> );
>
> bleh->Quit();
>
> return 0;
>
> }
> [code ends here]

Why don't you check value stored at '&bleh'?

Access Violation, AFAIK, means that you're touching not your memory.

Print address of CApplication object after creating it and later compare
that address with the value given by CoGetInterfaceAndReleaseStream.

Okay. And if I say something stupid or obvious don't be angry. I'm still
only human. ;)

Regards,
--
} Tomek Rydzynski {.} - - Moloch - - - {
} qsorix poczta onet pl {?} - http://www.mol... -- -{
} Moloch Developer {!} - - - - - - -- {