BOOL CXLAutomation::ExlInvoke(IDispatch *pdisp, LPOLESTR szMember, VARIANTARG * pvargReturn, WORD wInvokeAction, WORD wFlags) { HRESULT hr; DISPPARAMS dispparams; unsigned int uiArgErr; EXCEPINFO excep; // Get the IDs for the member and its arguments. GetIDsOfNames expects the // member name as the first name, followed by argument names (if any). m_alpszArgNames[0] = szMember; hr = pdisp->GetIDsOfNames( IID_NULL, m_alpszArgNames, 1 + m_iNamedArgCount, LOCALE_SYSTEM_DEFAULT, m_aDispIds); if (FAILED(hr)) { if (!(wFlags & DISP_NOSHOWEXCEPTIONS)) ShowException(szMember, hr, NULL, 0); return FALSE; } if (pvargReturn != NULL) ClearVariant(pvargReturn); // if doing a property put(ref), we need to adjust the first argument to have a // named arg of DISPID_PROPERTYPUT. if (wInvokeAction & (DISPATCH_PROPERTYPUT | DISPATCH_PROPERTYPUTREF)) { m_iNamedArgCount = 1; m_aDispIds[1] = DISPID_PROPERTYPUT; pvargReturn = NULL; } dispparams.rgdispidNamedArgs = m_aDispIds + 1; dispparams.rgvarg = m_aVargs; dispparams.cArgs = m_iArgCount; dispparams.cNamedArgs = m_iNamedArgCount; excep.pfnDeferredFillIn = NULL; hr = pdisp->Invoke(m_aDispIds[0], IID_NULL, LOCALE_SYSTEM_DEFAULT, wInvokeAction, &dispparams, pvargReturn, &excep, &uiArgErr); if (wFlags & DISP_FREEARGS) ClearAllArgs(); if (FAILED(hr)) { // display the exception information if appropriate: if (!(wFlags & DISP_NOSHOWEXCEPTIONS)) ShowException(szMember, hr, &excep, uiArgErr); // free exception structure information SysFreeString(excep.bstrSource); SysFreeString(excep.bstrDescription); SysFreeString(excep.bstrHelpFile); return FALSE; } return TRUE; }
bool tTVPApplication::StartApplication( int argc, char* argv[] ) { _set_se_translator(se_translator_function); ArgC = argc; ArgV = argv; for( int i = 0; i < argc; i++ ) { if(!strcmp(argv[i], "-@processohmlog")) { has_map_report_process_ = true; } } TVPTerminateCode = 0; CheckConsole(); // try starting the program! bool engine_init = false; try { if(TVPCheckProcessLog()) return true; // sub-process for processing object hash map log TVPInitScriptEngine(); engine_init = true; // banner TVPAddImportantLog( TVPFormatMessage(TVPProgramStartedOn, TVPGetOSName(), TVPGetPlatformName()) ); // TVPInitializeBaseSystems TVPInitializeBaseSystems(); Initialize(); if(TVPCheckPrintDataPath()) return true; if(TVPExecuteUserConfig()) return true; image_load_thread_ = new tTVPAsyncImageLoader(); TVPSystemInit(); if(TVPCheckAbout()) return true; // version information dialog box; SetTitle( std::wstring(TVPKirikiri) ); TVPSystemControl = new tTVPSystemControl(); #ifndef TVP_IGNORE_LOAD_TPM_PLUGIN TVPLoadPluigins(); // load plugin module *.tpm #endif // Check digitizer CheckDigitizer(); // start image load thread image_load_thread_->Resume(); if(TVPProjectDirSelected) TVPInitializeStartupScript(); Run(); try { // image_load_thread_->ExitRequest(); delete image_load_thread_; image_load_thread_ = NULL; } catch(...) { // ignore errors } try { TVPSystemUninit(); } catch(...) { // ignore errors } } catch( const EAbort & ) { // nothing to do } catch( const Exception &exception ) { TVPOnError(); if(!TVPSystemUninitCalled) ShowException(exception.what()); } catch( const TJS::eTJSScriptError &e ) { TVPOnError(); if(!TVPSystemUninitCalled) ShowException( e.GetMessage().c_str() ); } catch( const TJS::eTJS &e) { TVPOnError(); if(!TVPSystemUninitCalled) ShowException( e.GetMessage().c_str() ); } catch( const std::exception &e ) { ShowException( ttstr(e.what()).c_str() ); } catch( const char* e ) { ShowException( ttstr(e).c_str() ); } catch( const wchar_t* e ) { ShowException( e ); } catch( const SEHException& e ) { PEXCEPTION_RECORD rec = e.ExceptionPointers->ExceptionRecord; std::wstring text(SECodeToMessage(e.Code)); ttstr result = TJSGetStackTraceString( 10 ); PrintConsole( result.c_str(), result.length(), true ); TVPDumpHWException(); ShowException( text.c_str() ); } catch(...) { ShowException( (const tjs_char*)TVPUnknownError ); } if(engine_init) TVPUninitScriptEngine(); if(TVPSystemControl) delete TVPSystemControl; TVPSystemControl = NULL; CloseConsole(); return false; }
/* this is a console application */ int main () { EXCEPINFO excepinfo; unsigned int argerr; SAFEARRAY *psa; DISPID dispid; OLECHAR *name; VARIANT varResult; VARIANTARG varg, varga[2]; BSTR bstrIn; IDispatch *pIDispatch = NULL; int status = 0; HRESULT hr; printf("dspdisp - DADiSP IDispatch Test, Initializing...\n"); hr = OleInitialize(NULL); if (FAILED(hr)) { trace("Failed to OleInitialize"); return(1); } // Get the CLSID for the application. wchar_t progid[] = L"DADiSP.Application"; CLSID clsid; hr = ::CLSIDFromProgID(progid, &clsid); if(FAILED(hr)) { trace("Failed to CLSIDFromProgID"); status = 1; goto exitpt; } // start DADiSP as an automation server hr = ::CoCreateInstance(clsid, NULL, CLSCTX_LOCAL_SERVER, IID_IDispatch, (void**)&pIDispatch); if (FAILED(hr)) { trace("CoCreateInstance Failed"); status = 1; goto exitpt; } trace("CoCreateInstance Succeeded."); // --------------------------- // dadisp.getdata("version") // --------------------------- // First we need to get the ID for the function name ANSIToUnicode("GetData", &name); hr = pIDispatch->GetIDsOfNames(IID_NULL, &name, 1, GetUserDefaultLCID(), &dispid); if (FAILED(hr)) { trace("Query GetIDsOfNames Failed"); status = 1; goto exitpt; } trace("\ndadisp.getdata(\"version\")"); // Convert the wide-character string to a BSTR. CstrToBstr("version", &bstrIn); // Build up the parameters for the invoke call. // Allocate and initialize a VARIANT argument. ::VariantInit(&varg); // Initialize the VARIANT. V_VT(&varg) = VT_BSTR; // Type of VARIANT data V_BSTR(&varg) = bstrIn; // Data for the VARIANT // Fill in the DISPPARAMS structure. DISPPARAMS param; param.cArgs = 1; // Number of arguments param.rgvarg = &varg; // Arguments param.cNamedArgs = 0; // Number of named args param.rgdispidNamedArgs = NULL; // Named arguments // Allocate a variant for the returned parameter. ::VariantInit(&varResult); // Invoke the function. hr = pIDispatch->Invoke(dispid, IID_NULL, GetUserDefaultLCID(), DISPATCH_METHOD, ¶m, &varResult, &excepinfo, &argerr); if (FAILED(hr)) { trace("Invoke Failed"); ShowException(name, hr, &excepinfo, argerr); status = 1; goto exitpt; } // Display the returned value DisplayVariant(&varResult); // ------------------------------------ // dadisp.getdata("max(gnorm(100,1))") // ------------------------------------ ::SysFreeString(bstrIn); CstrToBstr("max(gnorm(100,1))", &bstrIn); // Build up the parameters for the invoke call. // Allocate and initialize a VARIANT argument. ::VariantInit(&varg); // Initialize the VARIANT. V_VT(&varg) = VT_BSTR; // Type of VARIANT data V_BSTR(&varg) = bstrIn; // Data for the VARIANT // Fill in the DISPPARAMS structure. param.cArgs = 1; // Number of arguments param.rgvarg = &varg; // Arguments param.cNamedArgs = 0; // Number of named args param.rgdispidNamedArgs = NULL; // Named arguments // Allocate a variant for the returned parameter. ::VariantInit(&varResult); trace("\ndadisp.getdata(\"max(gnorm(100,1))\")"); // Invoke the function. hr = pIDispatch->Invoke(dispid, IID_NULL, GetUserDefaultLCID(), DISPATCH_METHOD, ¶m, &varResult, &excepinfo, &argerr); if (FAILED(hr)) { trace("Invoke Failed"); ShowException(name, hr, &excepinfo, argerr); status = 1; goto exitpt; } // Display the returned value DisplayVariant(&varResult); // ------------------------------------- // dadisp.putdata("MyVar", RandomArray) // ------------------------------------- free(name); ANSIToUnicode("Putdata", &name); hr = pIDispatch->GetIDsOfNames(IID_NULL, &name, 1, GetUserDefaultLCID(), &dispid); if (FAILED(hr)) { trace("Query GetIDsOfNames Failed"); status = 1; goto exitpt; } // create a random array CreateRandomArray(darray, ARRAY_SIZE); // convert to automation SafeArray psa = DarrayToSafeArray(darray, ARRAY_SIZE, 1); ::SysFreeString(bstrIn); CstrToBstr("MyVar", &bstrIn); // Allocate and initialize a VARIANT argument. // This routine has two arguments, must add in *reverse* order ::VariantInit(&varga[1]); // Initialize the VARIANT. V_VT(&(varga[1])) = VT_BSTR; // DADiSP variable name V_BSTR(&(varga[1])) = bstrIn; // Data for the VARIANT ::VariantInit(&varga[0]); // Initialize the VARIANT. V_VT(&(varga[0])) = VT_ARRAY|VT_R8; // Array of doubles V_ARRAY(&(varga[0])) = psa; // Data for the VARIANT // Fill in the DISPPARAMS structure. param.cArgs = 2; // Number of arguments param.rgvarg = varga; // Arguments param.cNamedArgs = 0; // Number of named args param.rgdispidNamedArgs = NULL; // Named arguments // Allocate a variant for the returned parameter. ::VariantInit(&varResult); // Invoke the function. hr = pIDispatch->Invoke(dispid, IID_NULL, GetUserDefaultLCID(), DISPATCH_METHOD, ¶m, &varResult, &excepinfo, &argerr); if (FAILED(hr)) { trace("Invoke Failed"); ShowException(name, hr, &excepinfo, argerr); status = 1; goto exitpt; } // Display the input array trace("\nRandom Input Array:"); DisplayVariant(&varga[0]); // --------------------------------- // dadisp.getdata("fft(MyVar)") // --------------------------------- free(name); ANSIToUnicode("Getdata", &name); hr = pIDispatch->GetIDsOfNames(IID_NULL, &name, 1, GetUserDefaultLCID(), &dispid); if (FAILED(hr)) { trace("Query GetIDsOfNames Failed"); status = 1; goto exitpt; } ::SysFreeString(bstrIn); CstrToBstr("fft(MyVar)", &bstrIn); // Allocate and initialize a VARIANT argument. ::VariantInit(&varg); // Initialize the VARIANT. V_VT(&varg) = VT_BSTR; // Type of VARIANT data V_BSTR(&varg) = bstrIn; // Data for the VARIANT // Fill in the DISPPARAMS structure. param.cArgs = 1; // Number of arguments param.rgvarg = &varg; // Arguments param.cNamedArgs = 0; // Number of named args param.rgdispidNamedArgs = NULL; // Named arguments // Allocate a variant for the returned parameter. ::VariantInit(&varResult); // Invoke the function. hr = pIDispatch->Invoke(dispid, IID_NULL, GetUserDefaultLCID(), DISPATCH_METHOD, ¶m, &varResult, &excepinfo, &argerr); if (FAILED(hr)) { trace("Invoke Failed"); ShowException(name, hr, &excepinfo, argerr); status = 1; goto exitpt; } // Display the returned array trace("\nFFT Output Array:"); DisplayVariant(&varResult); exitpt: // Release the dispatch interface. if (pIDispatch) pIDispatch->Release(); // Uninitialize the OLE library. OleUninitialize(); return(0); }
// For Put/Get, no named arguments are allowed. bool wxAutomationObject::Invoke(const wxString& member, int action, wxVariant& retValue, int noArgs, wxVariant args[], const wxVariant* ptrArgs[]) const { if (!m_dispatchPtr) return false; int ch = member.Find('.'); if (ch != -1) { // Use dot notation to get the next object wxString member2(member.Left((size_t) ch)); wxString rest(member.Right(member.length() - ch - 1)); wxAutomationObject obj; if (!GetObject(obj, member2)) return false; return obj.Invoke(rest, action, retValue, noArgs, args, ptrArgs); } VARIANTARG vReturn; VariantInit(& vReturn); VARIANTARG* vReturnPtr = & vReturn; // Find number of names args int namedArgCount = 0; int i; for (i = 0; i < noArgs; i++) if ( !INVOKEARG(i).GetName().empty() ) { namedArgCount ++; } int namedArgStringCount = namedArgCount + 1; BSTR* argNames = new BSTR[namedArgStringCount]; argNames[0] = wxConvertStringToOle(member); // Note that arguments are specified in reverse order // (all totally logical; hey, we're dealing with OLE here.) int j = 0; for (i = 0; i < namedArgCount; i++) { if ( !INVOKEARG(i).GetName().empty() ) { argNames[(namedArgCount-j)] = wxConvertStringToOle(INVOKEARG(i).GetName()); j ++; } } // + 1 for the member name, + 1 again in case we're a 'put' DISPID* dispIds = new DISPID[namedArgCount + 2]; HRESULT hr; DISPPARAMS dispparams; unsigned int uiArgErr; // Get the IDs for the member and its arguments. GetIDsOfNames expects the // member name as the first name, followed by argument names (if any). hr = ((IDispatch*)m_dispatchPtr)->GetIDsOfNames(IID_NULL, argNames, 1 + namedArgCount, LOCALE_SYSTEM_DEFAULT, dispIds); if (FAILED(hr)) { ShowException(member, hr); delete[] argNames; delete[] dispIds; return false; } // if doing a property put(ref), we need to adjust the first argument to have a // named arg of DISPID_PROPERTYPUT. if (action & (DISPATCH_PROPERTYPUT | DISPATCH_PROPERTYPUTREF)) { namedArgCount = 1; dispIds[1] = DISPID_PROPERTYPUT; vReturnPtr = NULL; } // Convert the wxVariants to VARIANTARGs VARIANTARG* oleArgs = new VARIANTARG[noArgs]; for (i = 0; i < noArgs; i++) { // Again, reverse args if (!wxConvertVariantToOle(INVOKEARG((noArgs-1) - i), oleArgs[i])) { delete[] argNames; delete[] dispIds; delete[] oleArgs; return false; } } dispparams.rgdispidNamedArgs = dispIds + 1; dispparams.rgvarg = oleArgs; dispparams.cArgs = noArgs; dispparams.cNamedArgs = namedArgCount; EXCEPINFO excep; wxZeroMemory(excep); hr = ((IDispatch*)m_dispatchPtr)->Invoke(dispIds[0], IID_NULL, LOCALE_SYSTEM_DEFAULT, (WORD)action, &dispparams, vReturnPtr, &excep, &uiArgErr); for (i = 0; i < namedArgStringCount; i++) { SysFreeString(argNames[i]); } delete[] argNames; delete[] dispIds; for (i = 0; i < noArgs; i++) VariantClear(& oleArgs[i]) ; delete[] oleArgs; if (FAILED(hr)) { // display the exception information if appropriate: ShowException(member, hr, &excep, uiArgErr); // free exception structure information SysFreeString(excep.bstrSource); SysFreeString(excep.bstrDescription); SysFreeString(excep.bstrHelpFile); if (vReturnPtr) VariantClear(vReturnPtr); return false; } else { if (vReturnPtr) { // Convert result to wxVariant form wxConvertOleToVariant(vReturn, retValue); // Mustn't release the dispatch pointer if (vReturn.vt == VT_DISPATCH) { vReturn.pdispVal = NULL; } VariantClear(& vReturn); } } return true; }