Garmin Fleet Management Controller  2.19.0
CWebBrowser2.cpp
Go to the documentation of this file.
1 /*********************************************************************
2 *
3 * HEADER NAME:
4 * CWebBrowser2.cpp
5 *
6 * Copyright 2013 by Garmin Ltd. or its subsidiaries.
7 *---------------------------------------------------------------------
8 * $NoKeywords$
9 *********************************************************************/
10 #include "stdafx.h"
11 #include "CWebBrowser2.h"
12 #include <Shlwapi.h>
13 
14 IMPLEMENT_DYNCREATE(CWebBrowser2, CWnd)
15 
16 //----------------------------------------------------------------------
19 //----------------------------------------------------------------------
20 IHTMLDocument2 *CWebBrowser2::GetDocument()
21 {
22  IHTMLDocument2 *document = NULL;
23 
24  // get browser document's dispatch interface
25 
26  IDispatch *document_dispatch = get_Document();
27 
28  if (document_dispatch != NULL) {
29 
30  // get the actual document interface
31 
32  document_dispatch->QueryInterface(IID_IHTMLDocument2,
33  (void **)&document);
34 
35  // release dispatch interface
36 
37  document_dispatch->Release();
38  }
39 
40  return document;
41 }
42 
43 //----------------------------------------------------------------------
47 //----------------------------------------------------------------------
48 void CWebBrowser2::WriteContent(CString content)
49 {
50  IHTMLDocument2 *document = GetDocument();
51 
52  if (document != NULL) {
53 
54  // construct text to be written to browser as SAFEARRAY
55 
56  SAFEARRAY *safe_array = SafeArrayCreateVector(VT_VARIANT,0,1);
57 
58  VARIANT *variant;
59 
60  SafeArrayAccessData(safe_array,(LPVOID *)&variant);
61 
62  variant->vt = VT_BSTR;
63  variant->bstrVal = content.AllocSysString();
64 
65  SafeArrayUnaccessData(safe_array);
66 
67  // write SAFEARRAY to browser document
68 
69  document->write(safe_array);
70 
71  // cleanup
72 
73  document->Release();
74  document = NULL;
75 
76  ::SysFreeString(variant->bstrVal);
77  variant->bstrVal = NULL;
78 
79  SafeArrayDestroy(safe_array);
80 
81  }
82 }
83 
84 
85 CComVariant CWebBrowser2::InvokeScript(LPCTSTR funcName, const CStringArray * paramArray)
86  {
87  // look up the Script object
88  CComPtr<IDispatch> spScript;
89  HRESULT hr = GetDocument()->get_Script( &spScript );
90  if ( FAILED( hr ) )
91  {
92  return FALSE;
93  }
94 
95  // obtain a pointer to the desired function
96  CComBSTR bstrMember( funcName );
97  DISPID dispid = NULL;
98  hr = spScript->GetIDsOfNames( IID_NULL, &bstrMember, 1, LOCALE_SYSTEM_DEFAULT, &dispid );
99  if ( FAILED( hr ) )
100  {
101  return FALSE;
102  }
103 
104  // convert parameters
105  DISPPARAMS dispparams;
106  memset( &dispparams, 0, sizeof dispparams );
107  dispparams.cArgs = 0;
108  dispparams.cNamedArgs = 0;
109  if ( paramArray )
110  {
111  const int arraySize = (int) paramArray->GetCount();
112  dispparams.cArgs = arraySize;
113  dispparams.rgvarg = new VARIANT[ dispparams.cArgs ];
114  for (int i=0; i< arraySize; i++ )
115  {
116  CComBSTR bstr = paramArray->GetAt( arraySize - 1 - i );
117  bstr.CopyTo( &dispparams.rgvarg[i].bstrVal );
118  dispparams.rgvarg[i].vt = VT_BSTR;
119  }
120  }
121 
122  EXCEPINFO excepInfo;
123  memset( &excepInfo, 0, sizeof excepInfo );
124  CComVariant vaResult;
125  UINT nArgErr = (UINT)-1;
126 
127  // call the function with the supplied paramaters
128  hr = spScript->Invoke( dispid, IID_NULL, 0, DISPATCH_METHOD, &dispparams, &vaResult, &excepInfo, &nArgErr );
129  if ( 0 < dispparams.cArgs )
130  {
131  delete [] dispparams.rgvarg;
132  }
133  if ( FAILED( hr ) )
134  {
135  return FALSE;
136  }
137 
138  return vaResult;
139  }
140 
141 CString CWebBrowser2::URLDecode(CString sIn)
142  {
143  DWORD bufLen = sIn.GetLength()+1;
144  TCHAR * buf = new TCHAR[bufLen];
145  UrlCanonicalize( sIn, buf, &bufLen, URL_UNESCAPE );
146  CString out( buf );
147  delete buf;
148  return out;
149  }
150 
151 CString CWebBrowser2::URLEncode(CString sIn)
152  {
153  DWORD bufLen = sIn.GetLength()+1;
154  TCHAR * buf = new TCHAR[bufLen];
155  UrlCanonicalize( sIn, buf, &bufLen, 0 );
156  CString out( buf );
157  delete buf;
158  return out;
159  }
160 
CComVariant InvokeScript(LPCTSTR funcName, const CStringArray *paramArray=NULL)
#define FALSE
Definition: garmin_types.h:46
static CString URLEncode(CString sIn)
escape any special characters for use in URLs
void WriteContent(CString content)
Dynamically set the HTML content on the control.
IHTMLDocument2 * GetDocument()
Shortcut method for accessing the ITHMLDocument2 instance.
Web Browser ActiveX wrapper, generated by Visual Studio.
Definition: CWebBrowser2.h:24
static CString URLDecode(CString sIn)
translate escape sequences into their original characters