DumontEXE 0.0.1
cmcApi.cpp
00001 /* ***************************************************************************
00002 **
00003 ** Copyright (C) 2007 Lorimark Solutions, LLC. All rights reserved.
00004 **
00005 ** This file is part of the DumontEXE Scripting Extension Kit
00006 **
00007 ** This file may be used under the terms of the GNU General Public
00008 ** License version 2.0 as published by the Free Software Foundation
00009 ** and appearing in the file LICENSE.GPL included in the packaging of
00010 ** this file.  Please review the following information to ensure GNU
00011 ** General Public Licensing requirements will be met:
00012 ** http://dumont.showoff-db.org/opensource.html
00013 **
00014 ** If you are unsure which license is appropriate for your use, please
00015 ** review the following information:
00016 ** http://dumont.showoff-db.org/licensing.html 
00017 ** or contact the sales department at sales@lorimarksolutions.com.
00018 **
00019 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00020 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00021 **
00022 ** author: Mark Petryk ~ Lorimark Solutions, LLC
00023 ** **************************************************************************/
00024 
00025 #include <typelib.h>
00026 
00027 #include "cmcApi.h"
00028 
00029 namespace cmcDatabaseApi {
00030 
00031 cmcApi::cmcApi( const cmcApi & copy ):
00032   cmcObject( copy )
00033 {
00034 }
00035 
00036 cmcApi::cmcApi( const QString & i_objectName, IDispatch * iDispatch, cmcApplication * application, QObject * parent ):
00037   cmcObject( i_objectName, application, parent )
00038 {
00039   m_dispatch = iDispatch;
00040   AddRef();
00041 }
00042 
00043 long cmcApi::GetServerLong( const char * dispid, WORD wFlags )
00044 {
00045   return( GetServerLong( GetIDofName(dispid), wFlags ) );
00046 }
00047 
00048 long cmcApi::GetServerLong( DISPID dispid, WORD wFlags )
00049 {
00050   if( !m_dispatch ) return( 0 );
00051 
00052   // Prepare the arguments for Fx.
00053   DISPPARAMS dispparamsNoArgs = 
00054   {
00055     NULL, 
00056     NULL, 
00057     0,    // Zero arguments
00058     0     // Zero named arguments
00059   };
00060 
00061   // Allocate a variant for the returned parameter.
00062   VARIANT varResult;
00063   ::VariantInit(&varResult);
00064 
00065   // Invoke the function.
00066   HRESULT hr = Invoke
00067   (
00068     dispid,
00069     IID_NULL,
00070     GetUserDefaultLCID(),
00071     wFlags,
00072     &dispparamsNoArgs,   // &param,
00073     &varResult,
00074     NULL,
00075     NULL
00076   );
00077 
00078   if( FAILED(hr) )
00079   {
00080     return false;
00081   }
00082 
00083   // Display the returned string.
00084   if( varResult.vt == VT_I4 )
00085   {
00086     return varResult.lVal;
00087   }
00088 
00089   return false;
00090 
00091 }
00092 
00093 
00094 bool cmcApi::GetServerBool( const char * dispid, WORD wFlags )
00095 {
00096   return( GetServerBool( GetIDofName(dispid), wFlags ) );
00097 }
00098 
00099 bool cmcApi::GetServerBool( DISPID dispid, WORD wFlags )
00100 {
00101   if( !m_dispatch ) return( false );
00102 
00103   // Prepare the arguments for Fx.
00104   DISPPARAMS dispparamsNoArgs = 
00105   {
00106     NULL, 
00107     NULL, 
00108     0,    // Zero arguments
00109     0     // Zero named arguments
00110   };
00111 
00112   // Allocate a variant for the returned parameter.
00113   VARIANT varResult;
00114   ::VariantInit(&varResult);
00115 
00116   // Invoke the function.
00117   HRESULT hr = Invoke
00118   (
00119     dispid,
00120     IID_NULL,
00121     GetUserDefaultLCID(),
00122     wFlags,
00123     &dispparamsNoArgs, //&param,
00124     &varResult,
00125     NULL,
00126     NULL
00127   );
00128 
00129   if (FAILED(hr))
00130   {
00131     return false;
00132   }
00133 
00134   // Display the returned string.
00135   if (varResult.vt == VT_BOOL)
00136   {
00137     return varResult.boolVal;
00138   }
00139 
00140   return false;
00141 
00142 }
00143 
00144 
00145 IDispatch * cmcApi::GetServerObject
00146 (
00147   const QString & name,
00148   const QString & topic,
00149   const char * dispid,
00150   WORD wFlags,
00151   HRESULT * o_hr
00152 )
00153 {
00154 
00155   VARIANTARG varg[2];
00156   ::VariantInit(&varg[0]);     // Initialize the VARIANT.
00157   ::VariantInit(&varg[1]);
00158 
00159   // Convert the wide-character string to a BSTR.
00160   BSTR appIn = ::SysAllocString((OLECHAR*)name.utf16());
00161   BSTR topIn = ::SysAllocString((OLECHAR*)topic.utf16());
00162 
00163   // load the variant
00164   varg[1].vt = VT_BSTR; varg[1].bstrVal = appIn;
00165   varg[0].vt = VT_BSTR; varg[0].bstrVal = topIn;
00166 
00167   // Fill in the DISPPARAMS structure.
00168   DISPPARAMS param;
00169   param.cArgs             = 2;     // Number of arguments
00170   param.rgvarg            = varg;  // Arguments
00171   param.cNamedArgs        = 0;     // Number of named args
00172   param.rgdispidNamedArgs = NULL;  // Named arguments
00173 
00174   // Allocate a variant for the returned parameter.
00175   VARIANT varResult;
00176   ::VariantInit(&varResult);
00177 
00178   // Invoke the function.
00179   HRESULT hr = Invoke
00180   (
00181     dispid,
00182     IID_NULL,
00183     GetUserDefaultLCID(),
00184     DISPATCH_METHOD,
00185     &param,
00186     &varResult,
00187     NULL,
00188     NULL
00189   );
00190 
00191   ::SysFreeString(appIn);
00192   ::SysFreeString(topIn);
00193 
00194   if( o_hr ) *o_hr = hr;
00195 
00196   if( FAILED(hr) )
00197   {
00198     qDebug( "GetServerObject('%s','%s','%s') hr=%lx", name.toAscii().data(), topic.toAscii().data(), dispid, hr );
00199     return( NULL );
00200   }
00201 
00202   // Display the returned string.
00203   if( varResult.vt == VT_DISPATCH )
00204   {
00205     return( (IDispatch*)varResult.punkVal );
00206   }
00207 
00208   return( NULL );
00209 
00210 }
00211 
00212 
00213 IDispatch * cmcApi::GetServerObject
00214 ( 
00215   const long i_mode,
00216   const QString & i_name,
00217   const long i_flags,
00218   const char * i_dispid,
00219   WORD wFlags,
00220   HRESULT * hr
00221 )
00222 {
00223   return( GetServerObject( i_mode, i_name, i_flags, GetIDofName(i_dispid), wFlags, hr ) );
00224 }
00225 
00226 IDispatch * cmcApi::GetServerObject
00227 ( 
00228   const long i_mode,
00229   const QString & i_name,
00230   const long i_flags,
00231   const long i_dispid,
00232   WORD i_wFlags,
00233   HRESULT * o_hr
00234 )
00235 {
00236 //  if( eventTrace() )
00237 //    qDebug( "Database::getCursor(%ld,'%s')", i_mode, i_name.toAscii().data() );
00238 
00239 //    TRON( "DB::getCursor(" + QString::number(i_mode) + "," + i_name + ")" );
00240 
00241   VARIANTARG varg[3];
00242   ::VariantInit(&varg[0]);     // Initialize the VARIANT.
00243   ::VariantInit(&varg[1]);
00244   ::VariantInit(&varg[2]);
00245 
00246   // Convert the wide-character string to a BSTR.
00247   BSTR bstrIn = ::SysAllocString((OLECHAR*)i_name.utf16());
00248 
00249   // load the variant
00250   varg[2].vt = VT_I4;   varg[2].lVal    = i_mode;
00251   varg[1].vt = VT_BSTR; varg[1].bstrVal = bstrIn;
00252   varg[0].vt = VT_I4;   varg[0].lVal    = i_flags;
00253 
00254   // Fill in the DISPPARAMS structure.
00255   DISPPARAMS param;
00256   param.cArgs             = 3;     // Number of arguments
00257   param.rgvarg            = varg;  // Arguments
00258   param.cNamedArgs        = 0;     // Number of named args
00259   param.rgdispidNamedArgs = NULL;  // Named arguments
00260 
00261   // Allocate a variant for the returned parameter.
00262   VARIANT varResult;
00263   ::VariantInit(&varResult);
00264 
00265   // Invoke the function.
00266   HRESULT hr = Invoke
00267   (
00268     i_dispid,
00269     IID_NULL,
00270     GetUserDefaultLCID(),
00271     DISPATCH_METHOD,
00272     &param,
00273     &varResult,
00274     NULL,
00275     NULL
00276   );
00277 
00278   ::SysFreeString(bstrIn) ;
00279 
00280   if( o_hr ) *o_hr = hr;
00281 
00282   if( FAILED(hr) )
00283   {
00284 //    TROF( "DB::getCursor failed hr=" + QString::number(hr,16) );
00285     return( NULL );
00286   }
00287 
00288   // Display the returned string.
00289   if( varResult.vt == VT_DISPATCH )
00290   {
00291     return( (IDispatch*)varResult.punkVal );
00292   }
00293 
00294   return( NULL );
00295 
00296 }
00297 
00298 
00299 IDispatch * cmcApi::GetServerObject( const char * dispid, WORD wFlags, HRESULT * o_hr )
00300 {
00301   return( GetServerObject( GetIDofName(dispid), wFlags, o_hr ) );
00302 }  
00303 
00304 IDispatch * cmcApi::GetServerObject( DISPID dispid, WORD wFlags, HRESULT * o_hr )
00305 {
00306   if( !m_dispatch ) return( NULL );
00307 
00308   // Prepare the arguments for Fx.
00309   DISPPARAMS dispparamsNoArgs = 
00310   {
00311     NULL, 
00312     NULL, 
00313     0,    // Zero arguments
00314     0     // Zero named arguments
00315   };
00316 
00317   // Allocate a variant for the returned parameter.
00318   VARIANT varResult;
00319   ::VariantInit(&varResult);
00320 
00321   // Invoke the function.
00322   HRESULT hr = Invoke
00323   (
00324     dispid,
00325     IID_NULL,
00326     GetUserDefaultLCID(),
00327     wFlags,
00328     &dispparamsNoArgs, //&param,
00329     &varResult,
00330     NULL,
00331     NULL
00332   );
00333 
00334   if( o_hr ) *o_hr = hr;
00335 
00336   if( FAILED(hr) )
00337   {
00338     return( NULL );
00339   }
00340 
00341   // Display the returned string.
00342   if( varResult.vt == VT_DISPATCH )
00343   {
00344     return( (IDispatch*)varResult.punkVal );
00345   }
00346 
00347   return( NULL );
00348 
00349 }
00350 
00351 QString cmcApi::GetServerString( const char * dispid, const QString & req, WORD wFlags, HRESULT * o_hr )
00352 {
00353   return( GetServerString( GetIDofName(dispid), req, wFlags, o_hr ) );
00354 }
00355 
00356 QString cmcApi::GetServerString( DISPID dispid, const QString & req, WORD wFlags, HRESULT * o_hr )
00357 {
00358   if( !m_dispatch ) return( QString() );
00359 
00360   VARIANTARG varg[1];
00361   ::VariantInit(&varg[0]);     // Initialize the VARIANT.
00362 
00363   // Convert the wide-character string to a BSTR.
00364   BSTR bReq = ::SysAllocString((OLECHAR*)req.utf16());
00365 
00366   // load the variant
00367   varg[0].vt = VT_BSTR; varg[0].bstrVal = bReq;
00368 
00369   // Fill in the DISPPARAMS structure.
00370   DISPPARAMS param;
00371   param.cArgs             = 1;        // Number of arguments
00372   param.rgvarg            = varg;     // Arguments
00373   param.cNamedArgs        = 0;        // Number of named args
00374   param.rgdispidNamedArgs = NULL;     // Named arguments
00375 
00376   // Allocate a variant for the returned parameter.
00377   VARIANT varResult;
00378   ::VariantInit(&varResult);
00379 
00380   // Invoke the function.
00381   HRESULT hr = Invoke
00382   (
00383     dispid,
00384     IID_NULL,
00385     GetUserDefaultLCID(),
00386     wFlags,
00387     &param,
00388     &varResult,
00389     NULL,
00390     NULL
00391   );
00392 
00393   if( o_hr ) *o_hr = hr;
00394 
00395 //  qDebug( "GetServerString dispid=0x%08lx wf=%d hr=0x%08lx vt=%d\n", dispid, wFlags, hr, varResult.vt );
00396 
00397   if( FAILED(hr) )
00398   {
00399     ///\todo Make this code report these errors via the err object.
00400 //    qDebug( "failed GetServerString(%ld,%s)", dispid, qPrintable(req) );
00401 
00402     return QString();
00403   }
00404 
00405   // Display the returned string.
00406   if( varResult.vt == VT_BSTR )
00407   {
00408     QString answer = QString::fromWCharArray( varResult.bstrVal );
00409 
00410     // Free the string.
00411     ::SysFreeString( varResult.bstrVal );
00412 
00413     return answer;
00414   }
00415 
00416   return QString();
00417 
00418 }
00419 
00420 QString cmcApi::GetServerString( const char * dispid, WORD wFlags, HRESULT * o_hr )
00421 {
00422   return( GetServerString( GetIDofName(dispid), wFlags, o_hr ) );
00423 }  
00424 
00425 QString cmcApi::GetServerString( DISPID dispid, WORD wFlags, HRESULT * o_hr )
00426 {
00427   if( !m_dispatch ) return( QString() );
00428 
00429 //  if( dispid > 0 ) return( QString() );
00430 
00431   // Prepare the arguments for Fx.
00432   DISPPARAMS dispparamsNoArgs = 
00433   {
00434     NULL, 
00435     NULL, 
00436     0,    // Zero arguments
00437     0     // Zero named arguments
00438   };
00439 
00440   // Allocate a variant for the returned parameter.
00441   VARIANT varResult;
00442   ::VariantInit(&varResult);
00443 
00444   // Invoke the function.
00445   HRESULT hr = Invoke
00446   (
00447     dispid,
00448     IID_NULL,
00449     GetUserDefaultLCID(),
00450     wFlags,
00451     &dispparamsNoArgs, //&param,
00452     &varResult,
00453     NULL,
00454     NULL
00455   );
00456 
00457   if( o_hr ) *o_hr = hr;
00458 
00459 //  qDebug( "GetServerString dispid=0x%08lx wf=%d hr=0x%08lx vt=%d\n", dispid, wFlags, hr, varResult.vt );
00460 
00461   if( FAILED(hr) )
00462   {
00463     return QString();
00464   }
00465 
00466   // Display the returned string.
00467   if( varResult.vt == VT_BSTR )
00468   {
00469     QString dbName = QString::fromWCharArray( varResult.bstrVal );
00470 
00471     // Free the string.
00472     ::SysFreeString( varResult.bstrVal );
00473 
00474     return dbName;
00475   }
00476 
00477   return QString();
00478 
00479 }
00480 
00481 DISPID cmcApi::GetIDofName( QString & cmdName )
00482 {
00483   DISPID retVal = -1;
00484   if( m_dispatch )
00485   {
00486     OLECHAR * o_cmdName = (OLECHAR *)cmdName.utf16();
00487 
00488     GetIDsOfNames
00489     (
00490       IID_NULL,
00491       &o_cmdName,
00492       1,
00493       GetUserDefaultLCID(),
00494       &retVal
00495     );
00496     
00497   }
00498 
00499   /*
00500   ** This is an internal error and should be reported immediately.
00501   **
00502   */
00503   if( retVal == -1 )
00504     qDebug( "GetIDofName(\"%s\")==-1", cmdName.toAscii().data() );
00505 
00506   return( retVal );
00507 
00508 }  
00509 
00510 void cmcApi::PutServerString( const char * dispid, QString v_newValue, WORD wFlags )
00511 {
00512   return( PutServerString( GetIDofName(dispid), v_newValue, wFlags ) );
00513 }  
00514 
00515 void cmcApi::PutServerString( DISPID dispid, QString v_newValue, WORD wFlags )
00516 {
00517   if( !m_dispatch ) return;
00518 
00519   VARIANTARG varg[1];
00520   ::VariantInit(&varg[0]);     // Initialize the VARIANT.
00521 
00522   // Convert the wide-character string to a BSTR.
00523   BSTR newValue = ::SysAllocString((OLECHAR*)v_newValue.utf16());
00524 
00525   // load the variant
00526   varg[0].vt = VT_BSTR; varg[0].bstrVal = newValue;
00527 
00528   DISPID proput = DISPID_PROPERTYPUT;
00529 
00530   // Fill in the DISPPARAMS structure.
00531   DISPPARAMS param;
00532   param.cArgs             = 1;        // Number of arguments
00533   param.rgvarg            = varg;     // Arguments
00534   param.cNamedArgs        = 1;        // Number of named args
00535   param.rgdispidNamedArgs = &proput;     // Named arguments
00536 
00537   // Allocate a variant for the returned parameter.
00538   VARIANT varResult;
00539   ::VariantInit(&varResult);
00540 
00541   // Invoke the function.
00542   HRESULT hr = Invoke
00543   (
00544     dispid,
00545     IID_NULL,
00546     GetUserDefaultLCID(),
00547     wFlags,
00548     &param,
00549     &varResult,
00550     NULL,
00551     NULL
00552   );
00553 
00554   if( FAILED(hr) )
00555   {
00556     qDebug( "put server string error" );
00557   }
00558 
00559 //  qDebug( "var result type %d", varResult.vt );
00560 
00561   ::SysFreeString(newValue);
00562 
00563   return;
00564 
00565 } // endvoid CommencecmcApi::PutServerString(..)
00566 
00567 
00568 bool cmcApi::SetServerString( DISPID i_dispid, const QString & i_newValue, long i_flags )
00569 {
00570   VARIANTARG varg[2];
00571   ::VariantInit(&varg[0]);     // Initialize the VARIANT.
00572   ::VariantInit(&varg[1]);
00573 
00574   BSTR bstrIn = ::SysAllocString((OLECHAR*)i_newValue.utf16());
00575 
00576   // load the variant
00577   varg[1].vt = VT_BSTR; varg[1].bstrVal = bstrIn;
00578   varg[0].vt = VT_I4;   varg[0].lVal    = i_flags;
00579 
00580   // Fill in the DISPPARAMS structure.
00581   DISPPARAMS param;
00582   param.cArgs             = 2;     // Number of arguments
00583   param.rgvarg            = varg;  // Arguments
00584   param.cNamedArgs        = 0;     // Number of named args
00585   param.rgdispidNamedArgs = NULL;  // Named arguments
00586 
00587   // Allocate a variant for the returned parameter.
00588   VARIANT varResult;
00589   ::VariantInit(&varResult);
00590 
00591   // Invoke the function.
00592   HRESULT hr = Invoke
00593   (
00594     i_dispid,
00595     IID_NULL,
00596     GetUserDefaultLCID(),
00597     DISPATCH_METHOD,
00598     &param,
00599     &varResult,
00600     NULL,
00601     NULL
00602   );
00603 
00604   ::SysFreeString(bstrIn);
00605 
00606   if( FAILED(hr) )
00607   {
00608     return false;
00609   }
00610 
00611   // Display the returned string.
00612   if( varResult.vt == VT_BOOL )
00613   {
00614     return varResult.boolVal;
00615   }
00616 
00617   return false;
00618 
00619 } // endbool QCommenceCursor::SetServerString
00620 
00621 bool cmcApi::SetServerString( const char * i_dispid, const QString & i_newValue, long i_flags )
00622 {
00623   return( SetServerString( GetIDofName(i_dispid), i_newValue, i_flags ) );
00624 }
00625 
00626 
00627 void cmcApi::PutServerLong( const char * dispid, long v_newValue, WORD wFlags )
00628 {
00629   PutServerLong( GetIDofName(dispid), v_newValue, wFlags );
00630 }
00631 
00632 void cmcApi::PutServerLong( DISPID dispid, long v_newValue, WORD wFlags )
00633 {
00634   if( !m_dispatch ) return;
00635 
00636   VARIANTARG varg[1];
00637   ::VariantInit(&varg[0]);     // Initialize the VARIANT.
00638 
00639   // load the variant
00640   varg[0].vt = VT_I4; varg[0].lVal = v_newValue;
00641 
00642   DISPID proput = DISPID_PROPERTYPUT;
00643 
00644   // Fill in the DISPPARAMS structure.
00645   DISPPARAMS param;
00646   param.cArgs             = 1;        // Number of arguments
00647   param.rgvarg            = varg;     // Arguments
00648   param.cNamedArgs        = 1;        // Number of named args
00649   param.rgdispidNamedArgs = &proput;     // Named arguments
00650 
00651   // Allocate a variant for the returned parameter.
00652   VARIANT varResult;
00653   ::VariantInit(&varResult);
00654 
00655   // Invoke the function.
00656   HRESULT hr = Invoke
00657   (
00658     dispid,
00659     IID_NULL,
00660     GetUserDefaultLCID(),
00661     wFlags,
00662     &param,
00663     &varResult,
00664     NULL,
00665     NULL
00666   );
00667 
00668   if( hr )
00669   {
00670     qDebug( "PutServerLong error" );
00671   }
00672 
00673   return;
00674 
00675 } // endvoid CommencecmcApi::PutServerString(..)
00676 
00677 void cmcApi::dumpInterface( IDispatch * dispatch )
00678 {
00679   qDebug( "DumpInterface %p", dispatch );
00680 
00681   if( !dispatch ) dispatch = m_dispatch;
00682 
00683   TypeLib typelib(dispatch);
00684 
00685   typelib.dump();
00686 
00687 }
00688 
00689 } // endnamespace Commence
00690 
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties




~ ~ ~ ~ ~ ~
Source Code without Comments is like a Cranberry Garland
without the berries. Comment your Code!
 
Commence Database User Support Group Forum
http://newsgroup.showoff-db.org/
~ ~ ~ ~ ~ ~
Author: Mark Petryk
Lorimark Solutions, LLC
mark@lorimarksolutions.com