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]