Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

397 lines
13 KiB

  1. //***************************************************************************
  2. //
  3. // (c) 1999 by Microsoft Corporation
  4. //
  5. // MAINDLL.CPP
  6. //
  7. // alanbos 23-Mar-99 Created.
  8. //
  9. // Contains DLL entry points.
  10. //
  11. //***************************************************************************
  12. #include "precomp.h"
  13. #include <wbemdisp.h>
  14. #define WMITHIS L"instance"
  15. //***************************************************************************
  16. //
  17. // CWmiScriptingHost::CWmiScriptingHost
  18. //
  19. // DESCRIPTION:
  20. //
  21. // Constructor
  22. //
  23. //***************************************************************************
  24. CWmiScriptingHost::CWmiScriptingHost()
  25. {
  26. m_lRef = 0;
  27. m_pObject = NULL;
  28. #ifdef TEST
  29. // Grab an object to play with
  30. HRESULT hr = CoGetObject (L"winmgmts:{impersonationLevel=impersonate}!Win32_LogicalDisk=\"C:\"",
  31. NULL,IID_ISWbemObject, (void**)&m_pObject);
  32. #endif
  33. }
  34. //***************************************************************************
  35. //
  36. // CWmiScriptingHost::~CWmiScriptingHost
  37. //
  38. // DESCRIPTION:
  39. //
  40. // Destructor
  41. //
  42. //***************************************************************************
  43. CWmiScriptingHost::~CWmiScriptingHost()
  44. {
  45. if (m_pObject)
  46. {
  47. m_pObject->Release();
  48. m_pObject = NULL;
  49. }
  50. }
  51. //***************************************************************************
  52. //
  53. // CWmiScriptingHost::QueryInterface
  54. // CWmiScriptingHost::AddRef
  55. // CWmiScriptingHost::Release
  56. //
  57. // Purpose: IUnknown method implementations
  58. //
  59. //***************************************************************************
  60. HRESULT STDMETHODCALLTYPE CWmiScriptingHost::QueryInterface(REFIID riid, void** ppv)
  61. {
  62. if(riid == IID_IUnknown || riid == IID_IActiveScriptSite)
  63. *ppv = (IActiveScriptSite*)this;
  64. else if(riid == IID_IActiveScriptSiteWindow)
  65. *ppv = (IActiveScriptSiteWindow*)this;
  66. else
  67. return E_NOINTERFACE;
  68. ((IUnknown*)*ppv)->AddRef();
  69. return S_OK;
  70. }
  71. ULONG STDMETHODCALLTYPE CWmiScriptingHost::AddRef()
  72. {
  73. return InterlockedIncrement(&m_lRef);
  74. }
  75. ULONG STDMETHODCALLTYPE CWmiScriptingHost::Release()
  76. {
  77. long lRef = InterlockedDecrement(&m_lRef);
  78. if(lRef == 0)
  79. delete this;
  80. return lRef;
  81. }
  82. //***************************************************************************
  83. //
  84. // SCODE CWmiScriptingHost::GetLCID
  85. //
  86. // Description:
  87. //
  88. // Retrieves the locale identifier associated with the host's user
  89. // interface. The scripting engine uses the identifier to ensure
  90. // that error strings and other user-interface elements generated
  91. // by the engine appear in the appropriate language. .
  92. //
  93. // Parameters:
  94. //
  95. // plcid
  96. // Address of a variable that receives the locale identifier
  97. // for user-interface elements displayed by the scripting engine
  98. //
  99. // Return Value:
  100. // HRESULT E_NOTIMPL - the system-defined locale should be used
  101. //***************************************************************************
  102. HRESULT STDMETHODCALLTYPE CWmiScriptingHost::GetLCID(
  103. /* [out] */ LCID __RPC_FAR *plcid)
  104. {
  105. return E_NOTIMPL;
  106. }
  107. //***************************************************************************
  108. //
  109. // SCODE CWmiScriptingHost::GetItemInfo
  110. //
  111. // Description:
  112. //
  113. // Allows the scripting engine to obtain information about an item
  114. // added with the IActiveScript::AddNamedItem method.
  115. //
  116. // Parameters:
  117. //
  118. // pstrName
  119. // The name associated with the item, as specified in the
  120. // IActiveScript::AddNamedItem method.
  121. //
  122. // dwReturnMask
  123. // A bit mask specifying what information about the item
  124. // should be returned. The scripting engine should request the
  125. // minimum amount of information possible because some of
  126. // the return parameters (for example,ITypeInfo) can take
  127. // considerable time to load or generate. Can be a combination
  128. // of the following values:
  129. // SCRIPTINFO_IUNKNOWN Return theIUnknown interface for this item.
  130. // SCRIPTINFO_ITYPEINFO Return theITypeInfo interface for this item.
  131. //
  132. // ppunkItem
  133. // Address of a variable that receives a pointer to the IUnknown
  134. // interface associated with the given item. The scripting engine
  135. // can use the IUnknown::QueryInterface method to obtain the IDispatch
  136. // interface for the item. This parameter receives NULL if dwReturnMask
  137. // does not include the SCRIPTINFO_IUNKNOWN value. Also, it receives NULL
  138. // if there is no object associated with the item name; this mechanism is
  139. // used to create a simple class when the named item was added with the
  140. // SCRIPTITEM_CODEONLY flag set in the IActiveScript::AddNamedItem method.
  141. //
  142. // ppTypeInfo
  143. // Address of a variable that receives a pointer to theITypeInfo interface
  144. // associated with the item. This parameter receives NULL if dwReturnMask
  145. // does not include the SCRIPTINFO_ITYPEINFO value, or if type information
  146. // is not available for this item. If type information is not available,
  147. // the object cannot source events, and name binding must be realized with
  148. // the IDispatch::GetIDsOfNames method. Note that the ITypeInfo interface
  149. // retrieved describes the item's coclass (TKIND_COCLASS) because the object
  150. // may support multiple interfaces and event interfaces. If the item supports
  151. // the IProvideMultipleTypeInfo interface, the ITypeInfo interface retrieved
  152. // is the same as the index zero ITypeInfo that would be obtained using the
  153. // IProvideMultipleTypeInfo::GetInfoOfIndex method.
  154. //
  155. // Return Value:
  156. // S_OK Success.
  157. // E_INVALIDARG An argument was invalid.
  158. // E_POINTER An invalid pointer was specified.
  159. // TYPE_E_ELEMENTNOTFOUND An item of the specified name was not found.
  160. //
  161. //***************************************************************************
  162. HRESULT STDMETHODCALLTYPE CWmiScriptingHost::GetItemInfo(
  163. /* [in] */ LPCOLESTR pstrName,
  164. /* [in] */ DWORD dwReturnMask,
  165. /* [out] */ IUnknown __RPC_FAR *__RPC_FAR *ppunkItem,
  166. /* [out] */ ITypeInfo __RPC_FAR *__RPC_FAR *ppTypeInfo)
  167. {
  168. if (NULL == m_pObject)
  169. return TYPE_E_ELEMENTNOTFOUND;
  170. if(_wcsicmp(pstrName, WMITHIS))
  171. return TYPE_E_ELEMENTNOTFOUND;
  172. if(ppTypeInfo)
  173. *ppTypeInfo = NULL;
  174. if(ppunkItem)
  175. *ppunkItem = NULL;
  176. else
  177. return E_POINTER;
  178. if(dwReturnMask & SCRIPTINFO_IUNKNOWN)
  179. m_pObject->QueryInterface(IID_IUnknown, (void**)ppunkItem);
  180. // TODO - host should support SCRIPTINFO_ITYPEINFO
  181. // but we'll need scriptable objects to support IProvideClassInfo
  182. // or just hard code the typelib here
  183. return S_OK;
  184. }
  185. //***************************************************************************
  186. //
  187. // SCODE CWmiScriptingHost::GetDocVersionString
  188. //
  189. // Description:
  190. //
  191. // Retrieves a host-defined string that uniquely identifies the
  192. // current document version. If the related document has changed
  193. // outside the scope of ActiveX Scripting (as in the case of an
  194. // HTML page being edited with NotePad), the scripting engine can
  195. // save this along with its persisted state, forcing a recompile
  196. // the next time the script is loaded.
  197. //
  198. // Parameters:
  199. //
  200. // pstrVersionString
  201. // Address of the host-defined document version string.
  202. //
  203. // Return Value:
  204. // S_OK Success
  205. // E_NOTIMPL The scripting engine should assume that
  206. // the script is in sync with the document
  207. //***************************************************************************
  208. HRESULT STDMETHODCALLTYPE CWmiScriptingHost::GetDocVersionString(
  209. /* [out] */ BSTR __RPC_FAR *pbstrVersion)
  210. {
  211. return E_NOTIMPL;
  212. }
  213. //***************************************************************************
  214. //
  215. // SCODE CWmiScriptingHost::OnScriptTerminate
  216. //
  217. // Description:
  218. //
  219. // Informs the host that the script has completed execution. The
  220. // scripting engine calls this method before the call to the
  221. // IActiveScriptSite::OnStateChange method, with the
  222. // SCRIPTSTATE_INITIALIZED flag set, is completed. This method can
  223. // be used to return completion status and results to the host. Note
  224. // that many script languages, which are based on sinking events from
  225. // the host, have life spans that are defined by the host.
  226. // In this case, this method may never be called.
  227. //
  228. // Parameters:
  229. //
  230. // pvarResult
  231. // Address of a variable that contains the script result,
  232. // or NULL if the script produced no result.
  233. //
  234. // pexcepinfo
  235. // Address of an EXCEPINFO structure that contains exception
  236. // information generated when the script terminated, or NULL
  237. // if no exception was generated.
  238. //
  239. // Return Value:
  240. // S_OK Success
  241. //
  242. //***************************************************************************
  243. HRESULT STDMETHODCALLTYPE CWmiScriptingHost::OnScriptTerminate(
  244. /* [in] */ const VARIANT __RPC_FAR *pvarResult,
  245. /* [in] */ const EXCEPINFO __RPC_FAR *pexcepinfo)
  246. {
  247. return S_OK;
  248. }
  249. //***************************************************************************
  250. //
  251. // SCODE CWmiScriptingHost::OnStateChange
  252. //
  253. // Description:
  254. //
  255. // Informs the host that the scripting engine has changed states.
  256. //
  257. // Parameters:
  258. //
  259. // ssScriptState
  260. // Value that indicates the new script state. See the
  261. // IActiveScript::GetScriptState method for a description of the states.
  262. //
  263. // Return Value:
  264. // S_OK Success
  265. //
  266. //***************************************************************************
  267. HRESULT STDMETHODCALLTYPE CWmiScriptingHost::OnStateChange(
  268. /* [in] */ SCRIPTSTATE ssScriptState)
  269. {
  270. return S_OK;
  271. }
  272. //***************************************************************************
  273. //
  274. // SCODE CWmiScriptingHost::OnScriptError
  275. //
  276. // Description:
  277. //
  278. // Informs the host that an execution error occurred while the engine
  279. // was running the script.
  280. //
  281. // Parameters:
  282. //
  283. // pase
  284. // Address of the error object's IActiveScriptError interface.
  285. // A host can use this interface to obtain information about the
  286. // execution error.
  287. //
  288. // Return Value:
  289. // S_OK Success
  290. //
  291. //***************************************************************************
  292. HRESULT STDMETHODCALLTYPE CWmiScriptingHost::OnScriptError(
  293. /* [in] */ IActiveScriptError __RPC_FAR *pase)
  294. {
  295. HRESULT hres;
  296. EXCEPINFO ei;
  297. hres = pase->GetExceptionInfo(&ei);
  298. if(SUCCEEDED(hres))
  299. {
  300. printf("\nGot Error from source %S", ei.bstrSource);
  301. printf("\nDescription is %S", ei.bstrDescription);
  302. printf("\nThe error code is 0x%x", ei.scode);
  303. DWORD dwLine, dwCookie;
  304. long lChar;
  305. pase->GetSourcePosition(&dwCookie, &dwLine, &lChar);
  306. printf("\nError occured on line %d, character %d", dwLine, lChar);
  307. }
  308. return S_OK;
  309. }
  310. //***************************************************************************
  311. //
  312. // SCODE CWmiScriptingHost::OnEnterScript
  313. //
  314. // Description:
  315. //
  316. // Informs the host that the scripting engine has begun executing the
  317. // script code. The scripting engine must call this method on every
  318. // entry or reentry into the scripting engine. For example, if the
  319. // script calls an object that then fires an event handled by the
  320. // scripting engine, the scripting engine must call
  321. // IActiveScriptSite::OnEnterScript before executing the event, and
  322. // must call the IActiveScriptSite::OnLeaveScript method after executing
  323. // the event but before returning to the object that fired the event.
  324. // Calls to this method can be nested. Every call to this method
  325. // requires a corresponding call to IActiveScriptSite::OnLeaveScript.
  326. //
  327. // Return Value:
  328. // S_OK Success
  329. //
  330. //***************************************************************************
  331. HRESULT STDMETHODCALLTYPE CWmiScriptingHost::OnEnterScript( void)
  332. {
  333. return S_OK;
  334. }
  335. //***************************************************************************
  336. //
  337. // SCODE CWmiScriptingHost::OnLeaveScript
  338. //
  339. // Description:
  340. //
  341. // Informs the host that the scripting engine has returned from
  342. // executing script code. The scripting engine must call this method
  343. // before returning control to a calling application that entered the
  344. // scripting engine. For example, if the script calls an object that
  345. // then fires an event handled by the scripting engine, the scripting
  346. // engine must call the IActiveScriptSite::OnEnterScript method before
  347. // executing the event, and must call IActiveScriptSite::OnLeaveScript
  348. // after executing the event before returning to the object that fired
  349. // the event. Calls to this method can be nested. Every call to
  350. // IActiveScriptSite::OnEnterScript requires a corresponding call to
  351. // this method.
  352. //
  353. // Return Value:
  354. // S_OK Success
  355. //
  356. //***************************************************************************
  357. HRESULT STDMETHODCALLTYPE CWmiScriptingHost::OnLeaveScript( void)
  358. {
  359. return S_OK;
  360. }