Quantcast
Channel: Visual C forum
Viewing all articles
Browse latest Browse all 15302

MSScriptControl throws _com_error on Release().

$
0
0

Hi all, first off: I'm kinda new to this forum.

Second off: I'm kinda new to COM as well. Inspired by the example here (http://support.microsoft.com/kb/229669/), I decided to try running the code in its own thread. Here's the result:

// C/C++ headers #include <iostream> #include <fstream> using namespace std; // Win32 headers #include <Windows.h> // Other headers #include "CThread.h" #include "CScriptThread.h" #import <msscript.ocx> using namespace MSScriptControl; bool CScriptThread::ThreadProcedure(void) { HRESULT hr = CoInitialize(NULL); IScriptControlPtr pScriptControl(__uuidof(ScriptControl)); // Create a VARIANT array of VARIANTs which hold BSTRs LPSAFEARRAY psa; SAFEARRAYBOUND rgsabound[] = {3,0}; // 3 elements, 0-based int i; psa = SafeArrayCreate(VT_VARIANT,1,rgsabound); if(!psa) { cout << "E_OUTOFMEMORY" << endl; // Clean up: SafeArrayDestroy(psa); pScriptControl->Release(); CoUninitialize(); return this->Stop(1); } // if VARIANT vFlavors[3]; for(i = 0; i < 3; i++) { VariantInit(&vFlavors[i]); V_VT(&vFlavors[i]) = VT_BSTR; } // for V_BSTR(&vFlavors[0]) = SysAllocString(OLESTR("Vanilla")); V_BSTR(&vFlavors[1]) = SysAllocString(OLESTR("Chocolate")); V_BSTR(&vFlavors[2]) = SysAllocString(OLESTR("Espresso Chip")); long lZero = 0; long lOne = 1; long lTwo = 2; // Put Elements to the SafeArray: hr = SafeArrayPutElement(psa,&lZero,&vFlavors[0]); hr = SafeArrayPutElement(psa,&lOne,&vFlavors[1]); hr = SafeArrayPutElement(psa,&lTwo,&vFlavors[2]); // Free Elements from the SafeArray: for(i = 0; i < 3; i++) SysFreeString(vFlavors[i].bstrVal); // Read code from file ifstream infile; infile.open("script.js",ios::in); if(!infile.good()) { cout << "Error opening file \"script.js\"" << endl; // Clean up: SafeArrayDestroy(psa); pScriptControl->Release(); CoUninitialize(); return this->Stop(2); } // if infile.seekg(0,ios::end); uint fsize = infile.tellg(); infile.seekg(0,ios::beg);

// I know sizeof(char) == 1 ;) char* code = (char*) malloc(fsize * sizeof(char)); memset(code,0,fsize * sizeof(char)); infile.read(code,fsize * sizeof(char)); //cout << "Code:\n\n\n" << code << "\n\n\n" << flush; // Set up Script control properties pScriptControl->Language = "JScript"; pScriptControl->AllowUI = TRUE; cout << "pScriptControl->AddCode(...): " << pScriptControl->AddCode(code) << endl; free(code); infile.close(); // Call main with the two args: (three?????) _variant_t outpar; try { outpar = pScriptControl->Run("main",&psa); // Convert VARIANT to C string: _bstr_t bstrReturn = (_bstr_t) outpar; char* pResult = (char*) bstrReturn; // Print the result: cout << pResult << endl; } catch(_com_error e) { IScriptErrorPtr err = pScriptControl->Error; cout << err->Source << ": " << err->Description << endl; cout << "At " << err->Line << ":" << err->Column << endl; } // try // Clean up: SafeArrayDestroy(psa); pScriptControl->Release(); CoUninitialize(); try {

// Error occurs here return this->Stop(0); } catch(_com_error e) { cout << "Error: " << e.Description() << endl; return this->Stop(3); } // try } // CScriptThread::ThreadProcedure(...)

The program crashes at the return statement. this->Stop(x) does not cause any trouble.

VS's exception dialog reads "Unhandled exception at 0x00A4A32A in MSScriptControl_test.exe: 0xC0000005: Access violation reading location 0xFEEEFEF6." Does anyone know what I'm doing wrong here? Thanks in advance =)

Edit: clicking the Break button takes me comip.h.



Viewing all articles
Browse latest Browse all 15302

Trending Articles