Source code of Windows XP (NT5)
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.

4958 lines
136 KiB

  1. #include "gptext.h"
  2. #include <initguid.h>
  3. #include "scripts.h"
  4. #include "smartptr.h"
  5. #include "wbemtime.h"
  6. #include <Psapi.h>
  7. //
  8. // Result pane items for the GPE Scripts node
  9. //
  10. RESULTITEM g_GPEScriptsRoot[] =
  11. {
  12. { 1, 0, 0, 0, 0, {0} },
  13. };
  14. RESULTITEM g_GPEScriptsUser[] =
  15. {
  16. { 2, 1, IDS_LOGON, IDS_SCRIPTS_LOGON, 3, {0} },
  17. { 3, 1, IDS_LOGOFF, IDS_SCRIPTS_LOGOFF, 3, {0} },
  18. };
  19. RESULTITEM g_GPEScriptsMachine[] =
  20. {
  21. { 4, 2, IDS_STARTUP, IDS_SCRIPTS_STARTUP, 3, {0} },
  22. { 5, 2, IDS_SHUTDOWN, IDS_SCRIPTS_SHUTDOWN, 3, {0} },
  23. };
  24. //
  25. // Namespace (scope) items
  26. //
  27. NAMESPACEITEM g_GPEScriptsNameSpace[] =
  28. {
  29. { 0, -1, 0, IDS_SCRIPTS_DESC, 1, {0}, 0, g_GPEScriptsRoot, &NODEID_ScriptRoot }, // Scripts Root
  30. { 1 , 0, IDS_SCRIPTS_NAME_USER, IDS_SCRIPTS_USER_DESC, 0, {0}, 2, g_GPEScriptsUser, &NODEID_ScriptRootUser }, // Scripts node (user)
  31. { 2 , 0, IDS_SCRIPTS_NAME_MACHINE, IDS_SCRIPTS_COMPUTER_DESC, 0, {0}, 2, g_GPEScriptsMachine, &NODEID_ScriptRootMachine } // Scripts node (machine)
  32. };
  33. //
  34. // Result pane items for the RSOP Scripts node
  35. //
  36. RESULTITEM g_RSOPScriptsRoot[] =
  37. {
  38. { 1, 0, 0, 0, 0, {0} },
  39. };
  40. RESULTITEM g_RSOPScriptsUser[] =
  41. {
  42. { 2, 1, 0, 0, 0, {0} },
  43. };
  44. RESULTITEM g_RSOPScriptsMachine[] =
  45. {
  46. { 3, 2, 0, 0, 0, {0} },
  47. };
  48. RESULTITEM g_RSOPScriptsLogon[] =
  49. {
  50. { 4, 3, 0, 0, 0, {0} },
  51. };
  52. RESULTITEM g_RSOPScriptsLogoff[] =
  53. {
  54. { 5, 4, 0, 0, 0, {0} },
  55. };
  56. RESULTITEM g_RSOPScriptsStartup[] =
  57. {
  58. { 6, 5, 0, 0, 0, {0} },
  59. };
  60. RESULTITEM g_RSOPScriptsShutdown[] =
  61. {
  62. { 7, 6, 0, 0, 0, {0} },
  63. };
  64. //
  65. // Namespace (scope) items
  66. //
  67. NAMESPACEITEM g_RSOPScriptsNameSpace[] =
  68. {
  69. { 0, -1, 0, IDS_SCRIPTS_DESC, 1, {0}, 0, g_RSOPScriptsRoot, &NODEID_RSOPScriptRoot }, // Scripts Root
  70. { 1 , 0, IDS_SCRIPTS_NAME_USER, IDS_SCRIPTS_USER_DESC, 2, {0}, 0, g_RSOPScriptsUser, &NODEID_RSOPScriptRootUser }, // Scripts node (user)
  71. { 2 , 0, IDS_SCRIPTS_NAME_MACHINE, IDS_SCRIPTS_COMPUTER_DESC, 2, {0}, 0, g_RSOPScriptsMachine, &NODEID_RSOPScriptRootMachine }, // Scripts node (machine)
  72. { 3 , 1, IDS_LOGON, IDS_SCRIPTS_LOGON, 0, {0}, 0, g_RSOPScriptsLogon, &NODEID_RSOPLogon }, // Logon node
  73. { 4 , 1, IDS_LOGOFF, IDS_SCRIPTS_LOGOFF, 0, {0}, 0, g_RSOPScriptsLogoff, &NODEID_RSOPLogoff }, // Logoff node
  74. { 5 , 2, IDS_STARTUP, IDS_SCRIPTS_STARTUP, 0, {0}, 0, g_RSOPScriptsStartup, &NODEID_RSOPStartup }, // Startup node
  75. { 6 , 2, IDS_SHUTDOWN, IDS_SCRIPTS_SHUTDOWN, 0, {0}, 0, g_RSOPScriptsShutdown, &NODEID_RSOPShutdown } // Shutdown node
  76. };
  77. //
  78. // Script types
  79. //
  80. typedef enum _SCRIPTINFOTYPE {
  81. ScriptType_Logon = 0,
  82. ScriptType_Logoff,
  83. ScriptType_Startup,
  84. ScriptType_Shutdown
  85. } SCRIPTINFOTYPE, *LPSCRIPTINFOTYPE;
  86. //
  87. // Structure passed to a script dialog
  88. //
  89. typedef struct _SCRIPTINFO
  90. {
  91. CScriptsSnapIn * pCS;
  92. SCRIPTINFOTYPE ScriptType;
  93. } SCRIPTINFO, *LPSCRIPTINFO;
  94. //
  95. // Structure passed to a Add / edit script dialog
  96. //
  97. typedef struct _SCRIPTEDITINFO
  98. {
  99. LPSCRIPTINFO lpScriptInfo;
  100. BOOL bEdit;
  101. LPTSTR lpName;
  102. LPTSTR lpArgs;
  103. } SCRIPTEDITINFO, *LPSCRIPTEDITINFO;
  104. //
  105. // Structure stored in listview item
  106. //
  107. typedef struct _SCRIPTITEM
  108. {
  109. LPTSTR lpName;
  110. LPTSTR lpArgs;
  111. } SCRIPTITEM, *LPSCRIPTITEM;
  112. //
  113. // Scripts directory and ini file names in GPO
  114. //
  115. #define SCRIPTS_DIR_NAME TEXT("Scripts")
  116. #define SCRIPTS_FILE_NAME TEXT("scripts.ini")
  117. //
  118. // Help ids
  119. //
  120. DWORD aScriptsHelpIds[] =
  121. {
  122. IDC_SCRIPT_TITLE, IDH_SCRIPT_TITLE,
  123. IDC_SCRIPT_HEADING, IDH_SCRIPT_HEADING,
  124. IDC_SCRIPT_LIST, IDH_SCRIPT_LIST,
  125. IDC_SCRIPT_UP, IDH_SCRIPT_UP,
  126. IDC_SCRIPT_DOWN, IDH_SCRIPT_DOWN,
  127. IDC_SCRIPT_ADD, IDH_SCRIPT_ADD,
  128. IDC_SCRIPT_EDIT, IDH_SCRIPT_EDIT,
  129. IDC_SCRIPT_REMOVE, IDH_SCRIPT_REMOVE,
  130. IDC_SCRIPT_SHOW, IDH_SCRIPT_SHOW,
  131. 0, 0
  132. };
  133. DWORD aScriptsEditHelpIds[] =
  134. {
  135. IDC_SCRIPT_NAME, IDH_SCRIPT_NAME,
  136. IDC_SCRIPT_ARGS, IDH_SCRIPT_ARGS,
  137. IDC_SCRIPT_BROWSE, IDH_SCRIPT_BROWSE,
  138. 0, 0
  139. };
  140. ///////////////////////////////////////////////////////////////////////////////
  141. // //
  142. // CScriptsComponentData object implementation //
  143. // //
  144. ///////////////////////////////////////////////////////////////////////////////
  145. CScriptsComponentData::CScriptsComponentData(BOOL bUser, BOOL bRSOP)
  146. {
  147. m_cRef = 1;
  148. m_bUserScope = bUser;
  149. m_bRSOP = bRSOP;
  150. InterlockedIncrement(&g_cRefThisDll);
  151. m_hwndFrame = NULL;
  152. m_pScope = NULL;
  153. m_pConsole = NULL;
  154. m_hRoot = NULL;
  155. m_pGPTInformation = NULL;
  156. m_pRSOPInformation = NULL;
  157. m_pScriptsDir = NULL;
  158. m_pszNamespace = NULL;
  159. if (bRSOP)
  160. {
  161. m_pNameSpaceItems = g_RSOPScriptsNameSpace;
  162. m_dwNameSpaceItemCount = ARRAYSIZE(g_RSOPScriptsNameSpace);
  163. }
  164. else
  165. {
  166. m_pNameSpaceItems = g_GPEScriptsNameSpace;
  167. m_dwNameSpaceItemCount = ARRAYSIZE(g_GPEScriptsNameSpace);
  168. }
  169. m_pRSOPLogon = NULL;
  170. m_pRSOPLogoff = NULL;
  171. m_pRSOPStartup = NULL;
  172. m_pRSOPShutdown = NULL;
  173. }
  174. CScriptsComponentData::~CScriptsComponentData()
  175. {
  176. FreeRSOPScriptData();
  177. if (m_pScriptsDir)
  178. {
  179. LocalFree (m_pScriptsDir);
  180. }
  181. if (m_pszNamespace)
  182. {
  183. LocalFree (m_pszNamespace);
  184. }
  185. if (m_pScope)
  186. {
  187. m_pScope->Release();
  188. }
  189. if (m_pConsole)
  190. {
  191. m_pConsole->Release();
  192. }
  193. if (m_pGPTInformation)
  194. {
  195. m_pGPTInformation->Release();
  196. }
  197. if (m_pRSOPInformation)
  198. {
  199. m_pRSOPInformation->Release();
  200. }
  201. InterlockedDecrement(&g_cRefThisDll);
  202. }
  203. ///////////////////////////////////////////////////////////////////////////////
  204. // //
  205. // CScriptsComponentData object implementation (IUnknown) //
  206. // //
  207. ///////////////////////////////////////////////////////////////////////////////
  208. HRESULT CScriptsComponentData::QueryInterface (REFIID riid, void **ppv)
  209. {
  210. if (IsEqualIID(riid, IID_IComponentData) || IsEqualIID(riid, IID_IUnknown))
  211. {
  212. *ppv = (LPCOMPONENTDATA)this;
  213. m_cRef++;
  214. return S_OK;
  215. }
  216. else if (IsEqualIID(riid, IID_IPersistStreamInit))
  217. {
  218. *ppv = (LPPERSISTSTREAMINIT)this;
  219. m_cRef++;
  220. return S_OK;
  221. }
  222. else if (IsEqualIID(riid, IID_ISnapinHelp))
  223. {
  224. *ppv = (LPSNAPINHELP)this;
  225. m_cRef++;
  226. return S_OK;
  227. }
  228. else
  229. {
  230. *ppv = NULL;
  231. return E_NOINTERFACE;
  232. }
  233. }
  234. ULONG CScriptsComponentData::AddRef (void)
  235. {
  236. return ++m_cRef;
  237. }
  238. ULONG CScriptsComponentData::Release (void)
  239. {
  240. if (--m_cRef == 0) {
  241. delete this;
  242. return 0;
  243. }
  244. return m_cRef;
  245. }
  246. ///////////////////////////////////////////////////////////////////////////////
  247. // //
  248. // CScriptsComponentData object implementation (IComponentData) //
  249. // //
  250. ///////////////////////////////////////////////////////////////////////////////
  251. STDMETHODIMP CScriptsComponentData::Initialize(LPUNKNOWN pUnknown)
  252. {
  253. HRESULT hr;
  254. HBITMAP bmp16x16;
  255. LPIMAGELIST lpScopeImage;
  256. //
  257. // QI for IConsoleNameSpace
  258. //
  259. hr = pUnknown->QueryInterface(IID_IConsoleNameSpace, (LPVOID *)&m_pScope);
  260. if (FAILED(hr))
  261. {
  262. DebugMsg((DM_WARNING, TEXT("CScriptsComponentData::Initialize: Failed to QI for IConsoleNameSpace.")));
  263. return hr;
  264. }
  265. //
  266. // QI for IConsole
  267. //
  268. hr = pUnknown->QueryInterface(IID_IConsole2, (LPVOID *)&m_pConsole);
  269. if (FAILED(hr))
  270. {
  271. DebugMsg((DM_WARNING, TEXT("CScriptsComponentData::Initialize: Failed to QI for IConsole.")));
  272. m_pScope->Release();
  273. m_pScope = NULL;
  274. return hr;
  275. }
  276. m_pConsole->GetMainWindow (&m_hwndFrame);
  277. //
  278. // Query for the scope imagelist interface
  279. //
  280. hr = m_pConsole->QueryScopeImageList(&lpScopeImage);
  281. if (FAILED(hr))
  282. {
  283. DebugMsg((DM_WARNING, TEXT("CScriptsComponentData::Initialize: Failed to QI for scope imagelist.")));
  284. m_pScope->Release();
  285. m_pScope = NULL;
  286. m_pConsole->Release();
  287. m_pConsole=NULL;
  288. return hr;
  289. }
  290. // Load the bitmaps from the dll
  291. bmp16x16=LoadBitmap(g_hInstance, MAKEINTRESOURCE(IDB_16x16));
  292. // Set the images
  293. lpScopeImage->ImageListSetStrip(reinterpret_cast<LONG_PTR *>(bmp16x16),
  294. reinterpret_cast<LONG_PTR *>(bmp16x16),
  295. 0, RGB(255, 0, 255));
  296. lpScopeImage->Release();
  297. return S_OK;
  298. }
  299. STDMETHODIMP CScriptsComponentData::Destroy(VOID)
  300. {
  301. return S_OK;
  302. }
  303. STDMETHODIMP CScriptsComponentData::CreateComponent(LPCOMPONENT *ppComponent)
  304. {
  305. HRESULT hr;
  306. CScriptsSnapIn *pSnapIn;
  307. DebugMsg((DM_VERBOSE, TEXT("CScriptsComponentData::CreateComponent: Entering.")));
  308. //
  309. // Initialize
  310. //
  311. *ppComponent = NULL;
  312. //
  313. // Create the snapin view
  314. //
  315. pSnapIn = new CScriptsSnapIn(this);
  316. if (!pSnapIn)
  317. {
  318. DebugMsg((DM_WARNING, TEXT("CScriptsComponentData::CreateComponent: Failed to create CScriptsSnapIn.")));
  319. return E_OUTOFMEMORY;
  320. }
  321. //
  322. // QI for IComponent
  323. //
  324. hr = pSnapIn->QueryInterface(IID_IComponent, (LPVOID *)ppComponent);
  325. pSnapIn->Release(); // release QI
  326. return hr;
  327. }
  328. STDMETHODIMP CScriptsComponentData::QueryDataObject(MMC_COOKIE cookie, DATA_OBJECT_TYPES type,
  329. LPDATAOBJECT* ppDataObject)
  330. {
  331. HRESULT hr = E_NOINTERFACE;
  332. CScriptsDataObject *pDataObject;
  333. LPSCRIPTDATAOBJECT pScriptDataObject;
  334. //
  335. // Create a new DataObject
  336. //
  337. pDataObject = new CScriptsDataObject(this); // ref == 1
  338. if (!pDataObject)
  339. return E_OUTOFMEMORY;
  340. //
  341. // QI for the private GPTDataObject interface so we can set the cookie
  342. // and type information.
  343. //
  344. hr = pDataObject->QueryInterface(IID_IScriptDataObject, (LPVOID *)&pScriptDataObject);
  345. if (FAILED(hr))
  346. {
  347. pDataObject->Release();
  348. return (hr);
  349. }
  350. pScriptDataObject->SetType(type);
  351. pScriptDataObject->SetCookie(cookie);
  352. pScriptDataObject->Release();
  353. //
  354. // QI for a normal IDataObject to return.
  355. //
  356. hr = pDataObject->QueryInterface(IID_IDataObject, (LPVOID *)ppDataObject);
  357. pDataObject->Release(); // release initial ref
  358. return hr;
  359. }
  360. STDMETHODIMP CScriptsComponentData::Notify(LPDATAOBJECT lpDataObject, MMC_NOTIFY_TYPE event, LPARAM arg, LPARAM param)
  361. {
  362. HRESULT hr = S_OK;
  363. switch(event)
  364. {
  365. case MMCN_EXPAND:
  366. if (arg == TRUE)
  367. if (m_bRSOP)
  368. {
  369. if (!m_pRSOPInformation)
  370. {
  371. lpDataObject->QueryInterface(IID_IRSOPInformation, (LPVOID *)&m_pRSOPInformation);
  372. if (m_pRSOPInformation)
  373. {
  374. m_pszNamespace = (LPOLESTR) LocalAlloc (LPTR, 350 * sizeof(TCHAR));
  375. if (m_pszNamespace)
  376. {
  377. if (m_pRSOPInformation->GetNamespace((m_bUserScope ? GPO_SECTION_USER : GPO_SECTION_MACHINE),
  378. m_pszNamespace, 350) == S_OK)
  379. {
  380. InitializeRSOPScriptsData();
  381. if (LOWORD(dwDebugLevel) == DL_VERBOSE)
  382. {
  383. DumpRSOPScriptsData(m_pRSOPLogon);
  384. DumpRSOPScriptsData(m_pRSOPLogoff);
  385. DumpRSOPScriptsData(m_pRSOPStartup);
  386. DumpRSOPScriptsData(m_pRSOPShutdown);
  387. }
  388. }
  389. else
  390. {
  391. DebugMsg((DM_WARNING, TEXT("CScriptsComponentData::Notify: Failed to query for namespace")));
  392. LocalFree (m_pszNamespace);
  393. m_pszNamespace = NULL;
  394. }
  395. }
  396. }
  397. }
  398. if (m_pszNamespace)
  399. {
  400. BOOL bEnum = TRUE;
  401. if (m_bUserScope)
  402. {
  403. if (!m_pRSOPLogon && !m_pRSOPLogoff)
  404. {
  405. bEnum = FALSE;
  406. }
  407. }
  408. else
  409. {
  410. if (!m_pRSOPStartup && !m_pRSOPShutdown)
  411. {
  412. bEnum = FALSE;
  413. }
  414. }
  415. if (bEnum)
  416. {
  417. hr = EnumerateScopePane(lpDataObject, (HSCOPEITEM)param);
  418. }
  419. }
  420. }
  421. else
  422. {
  423. if (!m_pGPTInformation)
  424. {
  425. lpDataObject->QueryInterface(IID_IGPEInformation, (LPVOID *)&m_pGPTInformation);
  426. }
  427. if (m_pGPTInformation)
  428. {
  429. if (!m_pScriptsDir)
  430. {
  431. m_pScriptsDir = (LPTSTR) LocalAlloc (LPTR, MAX_PATH * sizeof(TCHAR));
  432. if (m_pScriptsDir)
  433. {
  434. if (SUCCEEDED(m_pGPTInformation->GetFileSysPath(m_bUserScope ? GPO_SECTION_USER : GPO_SECTION_MACHINE,
  435. m_pScriptsDir, MAX_PATH * sizeof(TCHAR))))
  436. {
  437. LPTSTR lpEnd;
  438. //
  439. // Create the Scripts directory
  440. //
  441. lpEnd = CheckSlash (m_pScriptsDir);
  442. lstrcpy (lpEnd, SCRIPTS_DIR_NAME);
  443. if (!CreateNestedDirectory(m_pScriptsDir, NULL))
  444. {
  445. DebugMsg((DM_WARNING, TEXT("CScriptsComponentData::Notify: Failed to create scripts sub-directory with %d."),
  446. GetLastError()));
  447. LocalFree (m_pScriptsDir);
  448. m_pScriptsDir = NULL;
  449. break;
  450. }
  451. //
  452. // Create the appropriate sub directories
  453. //
  454. lpEnd = CheckSlash (m_pScriptsDir);
  455. if (m_bUserScope)
  456. lstrcpy (lpEnd, TEXT("Logon"));
  457. else
  458. lstrcpy (lpEnd, TEXT("Startup"));
  459. if (!CreateNestedDirectory(m_pScriptsDir, NULL))
  460. {
  461. DebugMsg((DM_WARNING, TEXT("CScriptsComponentData::Notify: Failed to create scripts sub-directory with %d."),
  462. GetLastError()));
  463. LocalFree (m_pScriptsDir);
  464. m_pScriptsDir = NULL;
  465. break;
  466. }
  467. if (m_bUserScope)
  468. lstrcpy (lpEnd, TEXT("Logoff"));
  469. else
  470. lstrcpy (lpEnd, TEXT("Shutdown"));
  471. if (!CreateNestedDirectory(m_pScriptsDir, NULL))
  472. {
  473. DebugMsg((DM_WARNING, TEXT("CScriptsComponentData::Notify: Failed to create scripts sub-directory with %d."),
  474. GetLastError()));
  475. LocalFree (m_pScriptsDir);
  476. m_pScriptsDir = NULL;
  477. break;
  478. }
  479. *(lpEnd - 1) = TEXT('\0');
  480. }
  481. else
  482. {
  483. DebugMsg((DM_WARNING, TEXT("CScriptsComponentData::Notify: Failed to get file system path.")));
  484. LocalFree (m_pScriptsDir);
  485. m_pScriptsDir = NULL;
  486. }
  487. }
  488. }
  489. if (m_pScriptsDir)
  490. {
  491. hr = EnumerateScopePane(lpDataObject, (HSCOPEITEM)param);
  492. }
  493. }
  494. }
  495. break;
  496. default:
  497. break;
  498. }
  499. return hr;
  500. }
  501. STDMETHODIMP CScriptsComponentData::GetDisplayInfo(LPSCOPEDATAITEM pItem)
  502. {
  503. DWORD dwIndex;
  504. if (pItem == NULL)
  505. return E_POINTER;
  506. for (dwIndex = 0; dwIndex < m_dwNameSpaceItemCount; dwIndex++)
  507. {
  508. if (m_pNameSpaceItems[dwIndex].dwID == (DWORD) pItem->lParam)
  509. break;
  510. }
  511. if (dwIndex == m_dwNameSpaceItemCount)
  512. pItem->displayname = NULL;
  513. else
  514. {
  515. pItem->displayname = m_pNameSpaceItems[dwIndex].szDisplayName;
  516. }
  517. return S_OK;
  518. }
  519. STDMETHODIMP CScriptsComponentData::CompareObjects(LPDATAOBJECT lpDataObjectA, LPDATAOBJECT lpDataObjectB)
  520. {
  521. HRESULT hr = S_FALSE;
  522. LPSCRIPTDATAOBJECT pScriptDataObjectA, pScriptDataObjectB;
  523. MMC_COOKIE cookie1, cookie2;
  524. if (lpDataObjectA == NULL || lpDataObjectB == NULL)
  525. return E_POINTER;
  526. //
  527. // QI for the private GPTDataObject interface
  528. //
  529. if (FAILED(lpDataObjectA->QueryInterface(IID_IScriptDataObject,
  530. (LPVOID *)&pScriptDataObjectA)))
  531. {
  532. return S_FALSE;
  533. }
  534. if (FAILED(lpDataObjectB->QueryInterface(IID_IScriptDataObject,
  535. (LPVOID *)&pScriptDataObjectB)))
  536. {
  537. pScriptDataObjectA->Release();
  538. return S_FALSE;
  539. }
  540. pScriptDataObjectA->GetCookie(&cookie1);
  541. pScriptDataObjectB->GetCookie(&cookie2);
  542. if (cookie1 == cookie2)
  543. {
  544. hr = S_OK;
  545. }
  546. pScriptDataObjectA->Release();
  547. pScriptDataObjectB->Release();
  548. return hr;
  549. }
  550. ///////////////////////////////////////////////////////////////////////////////
  551. // //
  552. // CScriptsComponentData object implementation (IPersistStreamInit) //
  553. // //
  554. ///////////////////////////////////////////////////////////////////////////////
  555. STDMETHODIMP CScriptsComponentData::GetClassID(CLSID *pClassID)
  556. {
  557. if (!pClassID)
  558. {
  559. return E_FAIL;
  560. }
  561. if (m_bUserScope)
  562. *pClassID = CLSID_ScriptSnapInUser;
  563. else
  564. *pClassID = CLSID_ScriptSnapInMachine;
  565. return S_OK;
  566. }
  567. STDMETHODIMP CScriptsComponentData::IsDirty(VOID)
  568. {
  569. return S_FALSE;
  570. }
  571. STDMETHODIMP CScriptsComponentData::Load(IStream *pStm)
  572. {
  573. return S_OK;
  574. }
  575. STDMETHODIMP CScriptsComponentData::Save(IStream *pStm, BOOL fClearDirty)
  576. {
  577. return S_OK;
  578. }
  579. STDMETHODIMP CScriptsComponentData::GetSizeMax(ULARGE_INTEGER *pcbSize)
  580. {
  581. DWORD dwSize = 0;
  582. if (!pcbSize)
  583. {
  584. return E_FAIL;
  585. }
  586. ULISet32(*pcbSize, dwSize);
  587. return S_OK;
  588. }
  589. STDMETHODIMP CScriptsComponentData::InitNew(void)
  590. {
  591. return S_OK;
  592. }
  593. ///////////////////////////////////////////////////////////////////////////////
  594. // //
  595. // CScriptsComponentData object implementation (ISnapinHelp) //
  596. // //
  597. ///////////////////////////////////////////////////////////////////////////////
  598. STDMETHODIMP CScriptsComponentData::GetHelpTopic(LPOLESTR *lpCompiledHelpFile)
  599. {
  600. LPOLESTR lpHelpFile;
  601. lpHelpFile = (LPOLESTR) CoTaskMemAlloc (MAX_PATH * sizeof(WCHAR));
  602. if (!lpHelpFile)
  603. {
  604. DebugMsg((DM_WARNING, TEXT("CScriptsComponentData::GetHelpTopic: Failed to allocate memory.")));
  605. return E_OUTOFMEMORY;
  606. }
  607. ExpandEnvironmentStringsW (L"%SystemRoot%\\Help\\gptext.chm",
  608. lpHelpFile, MAX_PATH);
  609. *lpCompiledHelpFile = lpHelpFile;
  610. return S_OK;
  611. }
  612. ///////////////////////////////////////////////////////////////////////////////
  613. // //
  614. // CScriptsComponentData object implementation (Internal functions) //
  615. // //
  616. ///////////////////////////////////////////////////////////////////////////////
  617. HRESULT CScriptsComponentData::EnumerateScopePane (LPDATAOBJECT lpDataObject, HSCOPEITEM hParent)
  618. {
  619. SCOPEDATAITEM item;
  620. HRESULT hr;
  621. DWORD dwIndex, i;
  622. if (!m_hRoot)
  623. m_hRoot = hParent;
  624. if (m_hRoot == hParent)
  625. dwIndex = 0;
  626. else
  627. {
  628. item.mask = SDI_PARAM;
  629. item.ID = hParent;
  630. hr = m_pScope->GetItem (&item);
  631. if (FAILED(hr))
  632. return hr;
  633. dwIndex = (DWORD)item.lParam;
  634. }
  635. for (i = 0; i < m_dwNameSpaceItemCount; i++)
  636. {
  637. if (m_pNameSpaceItems[i].dwParent == dwIndex)
  638. {
  639. BOOL bAdd = TRUE;
  640. //
  641. // Need to special case the 2 main root nodes
  642. //
  643. if (dwIndex == 0)
  644. {
  645. if (m_bUserScope)
  646. {
  647. if (i == 2)
  648. {
  649. bAdd = FALSE;
  650. }
  651. }
  652. else
  653. {
  654. if (i == 1)
  655. {
  656. bAdd = FALSE;
  657. }
  658. }
  659. }
  660. //
  661. // Don't show a node if it has no data
  662. //
  663. if ((i == 3) && !m_pRSOPLogon)
  664. {
  665. bAdd = FALSE;
  666. }
  667. else if ((i == 4) && !m_pRSOPLogoff)
  668. {
  669. bAdd = FALSE;
  670. }
  671. else if ((i == 5) && !m_pRSOPStartup)
  672. {
  673. bAdd = FALSE;
  674. }
  675. else if ((i == 6) && !m_pRSOPShutdown)
  676. {
  677. bAdd = FALSE;
  678. }
  679. if (bAdd)
  680. {
  681. item.mask = SDI_STR | SDI_STATE | SDI_IMAGE | SDI_OPENIMAGE | SDI_PARAM | SDI_CHILDREN;
  682. item.displayname = MMC_CALLBACK;
  683. item.nImage = (i > 2) ? 0 : 3;
  684. item.nOpenImage = (i > 2) ? 1 : 3;
  685. item.nState = 0;
  686. item.cChildren = m_pNameSpaceItems[i].cChildren;
  687. item.lParam = m_pNameSpaceItems[i].dwID;
  688. item.relativeID = hParent;
  689. m_pScope->InsertItem (&item);
  690. }
  691. }
  692. }
  693. return S_OK;
  694. }
  695. BOOL CScriptsComponentData::AddRSOPScriptDataNode(LPTSTR lpCommandLine, LPTSTR lpArgs,
  696. LPTSTR lpGPOName, LPTSTR lpDate, UINT uiScriptType)
  697. {
  698. DWORD dwSize;
  699. LPRSOPSCRIPTITEM lpItem, lpTemp;
  700. //
  701. // Calculate the size of the new registry item
  702. //
  703. dwSize = sizeof (RSOPSCRIPTITEM);
  704. if (lpCommandLine) {
  705. dwSize += ((lstrlen(lpCommandLine) + 1) * sizeof(TCHAR));
  706. }
  707. if (lpArgs) {
  708. dwSize += ((lstrlen(lpArgs) + 1) * sizeof(TCHAR));
  709. }
  710. if (lpGPOName) {
  711. dwSize += ((lstrlen(lpGPOName) + 1) * sizeof(TCHAR));
  712. }
  713. if (lpDate) {
  714. dwSize += ((lstrlen(lpDate) + 1) * sizeof(TCHAR));
  715. }
  716. //
  717. // Allocate space for it
  718. //
  719. lpItem = (LPRSOPSCRIPTITEM) LocalAlloc (LPTR, dwSize);
  720. if (!lpItem) {
  721. DebugMsg((DM_WARNING, TEXT("CScriptsComponentData::AddRSOPRegistryDataNode: Failed to allocate memory with %d"),
  722. GetLastError()));
  723. return FALSE;
  724. }
  725. //
  726. // Fill in item
  727. //
  728. if (lpCommandLine)
  729. {
  730. lpItem->lpCommandLine = (LPTSTR)(((LPBYTE)lpItem) + sizeof(RSOPSCRIPTITEM));
  731. lstrcpy (lpItem->lpCommandLine, lpCommandLine);
  732. }
  733. if (lpArgs)
  734. {
  735. if (lpCommandLine)
  736. {
  737. lpItem->lpArgs = lpItem->lpCommandLine + lstrlen (lpItem->lpCommandLine) + 1;
  738. }
  739. else
  740. {
  741. lpItem->lpArgs = (LPTSTR)(((LPBYTE)lpItem) + sizeof(RSOPSCRIPTITEM));
  742. }
  743. lstrcpy (lpItem->lpArgs, lpArgs);
  744. }
  745. if (lpGPOName)
  746. {
  747. if (lpArgs)
  748. {
  749. lpItem->lpGPOName = lpItem->lpArgs + lstrlen (lpItem->lpArgs) + 1;
  750. }
  751. else
  752. {
  753. if (lpCommandLine)
  754. {
  755. lpItem->lpGPOName = lpItem->lpCommandLine + lstrlen (lpItem->lpCommandLine) + 1;
  756. }
  757. else
  758. {
  759. lpItem->lpGPOName = (LPTSTR)(((LPBYTE)lpItem) + sizeof(RSOPSCRIPTITEM));
  760. }
  761. }
  762. lstrcpy (lpItem->lpGPOName, lpGPOName);
  763. }
  764. if (lpDate)
  765. {
  766. if (lpGPOName)
  767. {
  768. lpItem->lpDate = lpItem->lpGPOName + lstrlen (lpItem->lpGPOName) + 1;
  769. }
  770. else
  771. {
  772. if (lpArgs)
  773. {
  774. lpItem->lpDate = lpItem->lpArgs + lstrlen (lpItem->lpArgs) + 1;
  775. }
  776. else
  777. {
  778. if (lpCommandLine)
  779. {
  780. lpItem->lpDate = lpItem->lpCommandLine + lstrlen (lpItem->lpCommandLine) + 1;
  781. }
  782. else
  783. {
  784. lpItem->lpDate = (LPTSTR)(((LPBYTE)lpItem) + sizeof(RSOPSCRIPTITEM));
  785. }
  786. }
  787. }
  788. lstrcpy (lpItem->lpDate, lpDate);
  789. }
  790. //
  791. // Add item to the appropriate link list
  792. //
  793. switch (uiScriptType)
  794. {
  795. case 1:
  796. if (m_pRSOPLogon)
  797. {
  798. lpTemp = m_pRSOPLogon;
  799. while (lpTemp->pNext)
  800. {
  801. lpTemp = lpTemp->pNext;
  802. }
  803. lpTemp->pNext = lpItem;
  804. }
  805. else
  806. {
  807. m_pRSOPLogon = lpItem;
  808. }
  809. break;
  810. case 2:
  811. if (m_pRSOPLogoff)
  812. {
  813. lpTemp = m_pRSOPLogoff;
  814. while (lpTemp->pNext)
  815. {
  816. lpTemp = lpTemp->pNext;
  817. }
  818. lpTemp->pNext = lpItem;
  819. }
  820. else
  821. {
  822. m_pRSOPLogoff = lpItem;
  823. }
  824. break;
  825. case 3:
  826. if (m_pRSOPStartup)
  827. {
  828. lpTemp = m_pRSOPStartup;
  829. while (lpTemp->pNext)
  830. {
  831. lpTemp = lpTemp->pNext;
  832. }
  833. lpTemp->pNext = lpItem;
  834. }
  835. else
  836. {
  837. m_pRSOPStartup = lpItem;
  838. }
  839. break;
  840. case 4:
  841. if (m_pRSOPShutdown)
  842. {
  843. lpTemp = m_pRSOPShutdown;
  844. while (lpTemp->pNext)
  845. {
  846. lpTemp = lpTemp->pNext;
  847. }
  848. lpTemp->pNext = lpItem;
  849. }
  850. else
  851. {
  852. m_pRSOPShutdown = lpItem;
  853. }
  854. break;
  855. }
  856. return TRUE;
  857. }
  858. VOID CScriptsComponentData::FreeRSOPScriptData(VOID)
  859. {
  860. LPRSOPSCRIPTITEM lpTemp;
  861. if (m_pRSOPLogon)
  862. {
  863. do {
  864. lpTemp = m_pRSOPLogon->pNext;
  865. LocalFree (m_pRSOPLogon);
  866. m_pRSOPLogon = lpTemp;
  867. } while (lpTemp);
  868. }
  869. if (m_pRSOPLogoff)
  870. {
  871. do {
  872. lpTemp = m_pRSOPLogoff->pNext;
  873. LocalFree (m_pRSOPLogoff);
  874. m_pRSOPLogoff = lpTemp;
  875. } while (lpTemp);
  876. }
  877. if (m_pRSOPStartup)
  878. {
  879. do {
  880. lpTemp = m_pRSOPStartup->pNext;
  881. LocalFree (m_pRSOPStartup);
  882. m_pRSOPStartup = lpTemp;
  883. } while (lpTemp);
  884. }
  885. if (m_pRSOPShutdown)
  886. {
  887. do {
  888. lpTemp = m_pRSOPShutdown->pNext;
  889. LocalFree (m_pRSOPShutdown);
  890. m_pRSOPShutdown = lpTemp;
  891. } while (lpTemp);
  892. }
  893. }
  894. HRESULT CScriptsComponentData::InitializeRSOPScriptsData(VOID)
  895. {
  896. BSTR pLanguage = NULL, pQuery = NULL;
  897. BSTR pScriptList = NULL, pScript = NULL, pArgs = NULL, pExecutionTime = NULL;
  898. BSTR pGPOid = NULL, pNamespace = NULL;
  899. IEnumWbemClassObject * pEnum = NULL;
  900. IWbemClassObject *pObjects[2], *pObject;
  901. HRESULT hr;
  902. ULONG ulRet;
  903. VARIANT varScriptList, varScript, varArgs, varExecutionTime;
  904. VARIANT varGPOid;
  905. SAFEARRAY * pSafeArray;
  906. IWbemLocator *pIWbemLocator = NULL;
  907. IWbemServices *pIWbemServices = NULL;
  908. LPTSTR lpGPOName;
  909. TCHAR szQuery[100];
  910. UINT uiOrder, uiScriptType, uiIndex;
  911. LONG lIndex;
  912. IUnknown *pItem;
  913. DebugMsg((DM_VERBOSE, TEXT("CScriptsComponentData::InitializeRSOPScriptsData: Entering")));
  914. //
  915. // Allocate BSTRs for the query language and for the query itself
  916. //
  917. pLanguage = SysAllocString (TEXT("WQL"));
  918. if (!pLanguage)
  919. {
  920. DebugMsg((DM_WARNING, TEXT("CScriptsComponentData::InitializeRSOPScriptsData: Failed to allocate memory for language")));
  921. hr = HRESULT_FROM_WIN32(ERROR_OUTOFMEMORY);
  922. goto Exit;
  923. }
  924. //
  925. // Allocate BSTRs for the property names we want to retreive
  926. //
  927. pScriptList = SysAllocString (TEXT("scriptList"));
  928. if (!pScriptList)
  929. {
  930. DebugMsg((DM_WARNING, TEXT("CScriptsComponentData::InitializeRSOPScriptsData: Failed to allocate memory for scriptList")));
  931. hr = HRESULT_FROM_WIN32(ERROR_OUTOFMEMORY);
  932. goto Exit;
  933. }
  934. pGPOid = SysAllocString (TEXT("GPOID"));
  935. if (!pGPOid)
  936. {
  937. DebugMsg((DM_WARNING, TEXT("CScriptsComponentData::InitializeRSOPScriptsData: Failed to allocate memory for GPO id")));
  938. hr = HRESULT_FROM_WIN32(ERROR_OUTOFMEMORY);
  939. goto Exit;
  940. }
  941. pScript = SysAllocString (TEXT("script"));
  942. if (!pScript)
  943. {
  944. DebugMsg((DM_WARNING, TEXT("CScriptsComponentData::InitializeRSOPScriptsData: Failed to allocate memory for script")));
  945. hr = HRESULT_FROM_WIN32(ERROR_OUTOFMEMORY);
  946. goto Exit;
  947. }
  948. pArgs = SysAllocString (TEXT("arguments"));
  949. if (!pArgs)
  950. {
  951. DebugMsg((DM_WARNING, TEXT("CScriptsComponentData::InitializeRSOPScriptsData: Failed to allocate memory for arguments")));
  952. hr = HRESULT_FROM_WIN32(ERROR_OUTOFMEMORY);
  953. goto Exit;
  954. }
  955. pExecutionTime = SysAllocString (TEXT("executionTime"));
  956. if (!pExecutionTime)
  957. {
  958. DebugMsg((DM_WARNING, TEXT("CScriptsComponentData::InitializeRSOPScriptsData: Failed to allocate memory for execution time")));
  959. hr = HRESULT_FROM_WIN32(ERROR_OUTOFMEMORY);
  960. goto Exit;
  961. }
  962. //
  963. // Create an instance of the WMI locator service
  964. //
  965. hr = CoCreateInstance(CLSID_WbemLocator, NULL, CLSCTX_INPROC_SERVER,
  966. IID_IWbemLocator, (LPVOID *) &pIWbemLocator);
  967. if (FAILED(hr))
  968. {
  969. DebugMsg((DM_WARNING, TEXT("CScriptsComponentData::InitializeRSOPScriptsData: CoCreateInstance failed with 0x%x"), hr));
  970. goto Exit;
  971. }
  972. //
  973. // Allocate a BSTR for the namespace
  974. //
  975. pNamespace = SysAllocString (m_pszNamespace);
  976. if (!pNamespace)
  977. {
  978. DebugMsg((DM_WARNING, TEXT("CScriptsComponentData::InitializeRSOPScriptsData: Failed to allocate memory for namespace")));
  979. hr = HRESULT_FROM_WIN32(ERROR_OUTOFMEMORY);
  980. goto Exit;
  981. }
  982. //
  983. // Connect to the server
  984. //
  985. hr = pIWbemLocator->ConnectServer(pNamespace, NULL, NULL, 0L, 0L, NULL, NULL,
  986. &pIWbemServices);
  987. if (FAILED(hr))
  988. {
  989. DebugMsg((DM_WARNING, TEXT("CScriptsComponentData::InitializeRSOPScriptsData: ConnectServer failed with 0x%x"), hr));
  990. goto Exit;
  991. }
  992. //
  993. // We need to read two sets of data. Either logon & logoff scripts
  994. // or startup & shutdown scripts.
  995. //
  996. for (uiIndex = 0; uiIndex < 2; uiIndex++)
  997. {
  998. //
  999. // Set the uiScriptType to the correct value. These values are defined
  1000. // in rsop.mof
  1001. //
  1002. if (m_bUserScope)
  1003. {
  1004. if (uiIndex == 0)
  1005. {
  1006. uiScriptType = 1; //Logon
  1007. }
  1008. else
  1009. {
  1010. uiScriptType = 2; //Logoff
  1011. }
  1012. }
  1013. else
  1014. {
  1015. if (uiIndex == 0)
  1016. {
  1017. uiScriptType = 3; //Startup
  1018. }
  1019. else
  1020. {
  1021. uiScriptType = 4; //Shutdown
  1022. }
  1023. }
  1024. //
  1025. // Loop through the items bumping the order number by 1 each time
  1026. //
  1027. uiOrder = 1;
  1028. while (TRUE)
  1029. {
  1030. //
  1031. // Build the query
  1032. //
  1033. wsprintf (szQuery, TEXT("SELECT * FROM RSOP_ScriptPolicySetting WHERE scriptType=\"%d\" AND scriptOrder=\"%d\""),
  1034. uiScriptType, uiOrder);
  1035. pQuery = SysAllocString (szQuery);
  1036. if (!pQuery)
  1037. {
  1038. DebugMsg((DM_WARNING, TEXT("CScriptsComponentData::InitializeRSOPScriptsData: Failed to allocate memory for query")));
  1039. hr = HRESULT_FROM_WIN32(ERROR_OUTOFMEMORY);
  1040. goto Exit;
  1041. }
  1042. //
  1043. // Execute the query
  1044. //
  1045. hr = pIWbemServices->ExecQuery (pLanguage, pQuery,
  1046. WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
  1047. NULL, &pEnum);
  1048. if (FAILED(hr))
  1049. {
  1050. DebugMsg((DM_WARNING, TEXT("CScriptsComponentData::InitializeRSOPScriptsData: Failed to query for %s with 0x%x"),
  1051. pQuery, hr));
  1052. goto Exit;
  1053. }
  1054. //
  1055. // Get the first (and only) item
  1056. //
  1057. hr = pEnum->Next(WBEM_INFINITE, 1, pObjects, &ulRet);
  1058. //
  1059. // Check for the "data not available case"
  1060. //
  1061. if ((hr != S_OK) || (ulRet == 0))
  1062. {
  1063. pEnum->Release();
  1064. pEnum = NULL;
  1065. goto LoopAgain;
  1066. }
  1067. //
  1068. // Get the scriptList
  1069. //
  1070. hr = pObjects[0]->Get (pScriptList, 0, &varScriptList, NULL, NULL);
  1071. if (SUCCEEDED(hr))
  1072. {
  1073. //
  1074. // Get the GPO ID
  1075. //
  1076. hr = pObjects[0]->Get (pGPOid, 0, &varGPOid, NULL, NULL);
  1077. if (SUCCEEDED(hr))
  1078. {
  1079. //
  1080. // Get the GPO friendly name from the GPOID
  1081. //
  1082. hr = GetGPOFriendlyName (pIWbemServices, varGPOid.bstrVal,
  1083. pLanguage, &lpGPOName);
  1084. if (SUCCEEDED(hr))
  1085. {
  1086. //
  1087. // Loop through the script entries
  1088. //
  1089. pSafeArray = varScriptList.parray;
  1090. for (lIndex=0; lIndex < (LONG)pSafeArray->rgsabound[0].cElements; lIndex++)
  1091. {
  1092. SafeArrayGetElement (pSafeArray, &lIndex, &pItem);
  1093. hr = pItem->QueryInterface (IID_IWbemClassObject, (LPVOID *)&pObject);
  1094. if (SUCCEEDED(hr))
  1095. {
  1096. //
  1097. // Get the script command line
  1098. //
  1099. hr = pObject->Get (pScript, 0, &varScript, NULL, NULL);
  1100. if (SUCCEEDED(hr))
  1101. {
  1102. //
  1103. // Get the arguments
  1104. //
  1105. hr = pObject->Get (pArgs, 0, &varArgs, NULL, NULL);
  1106. if (SUCCEEDED(hr))
  1107. {
  1108. TCHAR szDate[20];
  1109. TCHAR szTime[20];
  1110. TCHAR szBuffer[45] = {0};
  1111. XBStr xbstrWbemTime;
  1112. SYSTEMTIME SysTime;
  1113. FILETIME FileTime, LocalFileTime;
  1114. //
  1115. // Get the execution time
  1116. //
  1117. hr = pObject->Get (pExecutionTime, 0, &varExecutionTime, NULL, NULL);
  1118. if (SUCCEEDED(hr))
  1119. {
  1120. xbstrWbemTime = varExecutionTime.bstrVal;
  1121. hr = WbemTimeToSystemTime(xbstrWbemTime, SysTime);
  1122. if (SUCCEEDED(hr))
  1123. {
  1124. if (SysTime.wMonth != 0)
  1125. {
  1126. SystemTimeToFileTime (&SysTime, &FileTime);
  1127. FileTimeToLocalFileTime (&FileTime, &LocalFileTime);
  1128. FileTimeToSystemTime (&LocalFileTime, &SysTime);
  1129. GetDateFormat (LOCALE_USER_DEFAULT, DATE_SHORTDATE,
  1130. &SysTime, NULL, szDate, 20);
  1131. GetTimeFormat (LOCALE_USER_DEFAULT, TIME_NOSECONDS,
  1132. &SysTime, NULL, szTime, 20);
  1133. wsprintf (szBuffer, TEXT("%s %s"), szDate, szTime);
  1134. }
  1135. else
  1136. {
  1137. lstrcpy (szBuffer, TEXT(" "));
  1138. }
  1139. }
  1140. VariantClear (&varExecutionTime);
  1141. }
  1142. AddRSOPScriptDataNode(varScript.bstrVal,
  1143. (varArgs.vt == VT_NULL) ? NULL : varArgs.bstrVal,
  1144. lpGPOName, szBuffer, uiScriptType);
  1145. VariantClear (&varArgs);
  1146. }
  1147. VariantClear (&varScript);
  1148. }
  1149. pObject->Release();
  1150. }
  1151. }
  1152. LocalFree (lpGPOName);
  1153. }
  1154. VariantClear (&varGPOid);
  1155. }
  1156. VariantClear (&varScriptList);
  1157. }
  1158. pEnum->Release();
  1159. pEnum = NULL;
  1160. pObjects[0]->Release();
  1161. SysFreeString (pQuery);
  1162. pQuery = NULL;
  1163. uiOrder++;
  1164. }
  1165. LoopAgain:
  1166. hr = S_OK;
  1167. }
  1168. hr = S_OK;
  1169. Exit:
  1170. if (pEnum)
  1171. {
  1172. pEnum->Release();
  1173. }
  1174. if (pIWbemLocator)
  1175. {
  1176. pIWbemLocator->Release();
  1177. }
  1178. if (pIWbemServices)
  1179. {
  1180. pIWbemServices->Release();
  1181. }
  1182. if (pLanguage)
  1183. {
  1184. SysFreeString (pLanguage);
  1185. }
  1186. if (pQuery)
  1187. {
  1188. SysFreeString (pQuery);
  1189. }
  1190. if (pScriptList)
  1191. {
  1192. SysFreeString (pScriptList);
  1193. }
  1194. if (pScript)
  1195. {
  1196. SysFreeString (pScript);
  1197. }
  1198. if (pArgs)
  1199. {
  1200. SysFreeString (pArgs);
  1201. }
  1202. if (pExecutionTime)
  1203. {
  1204. SysFreeString (pExecutionTime);
  1205. }
  1206. if (pGPOid)
  1207. {
  1208. SysFreeString (pGPOid);
  1209. }
  1210. DebugMsg((DM_VERBOSE, TEXT("CScriptsComponentData::InitializeRSOPScriptsData: Leaving")));
  1211. return hr;
  1212. }
  1213. HRESULT CScriptsComponentData::GetGPOFriendlyName(IWbemServices *pIWbemServices,
  1214. LPTSTR lpGPOID, BSTR pLanguage,
  1215. LPTSTR *pGPOName)
  1216. {
  1217. BSTR pQuery = NULL, pName = NULL;
  1218. LPTSTR lpQuery = NULL;
  1219. IEnumWbemClassObject * pEnum = NULL;
  1220. IWbemClassObject *pObjects[2];
  1221. HRESULT hr;
  1222. ULONG ulRet;
  1223. VARIANT varGPOName;
  1224. //
  1225. // Set the default
  1226. //
  1227. *pGPOName = NULL;
  1228. //
  1229. // Build the query
  1230. //
  1231. lpQuery = (LPTSTR) LocalAlloc (LPTR, ((lstrlen(lpGPOID) + 50) * sizeof(TCHAR)));
  1232. if (!lpQuery)
  1233. {
  1234. DebugMsg((DM_WARNING, TEXT("CScriptsComponentData::GetGPOFriendlyName: Failed to allocate memory for unicode query")));
  1235. hr = HRESULT_FROM_WIN32(ERROR_OUTOFMEMORY);
  1236. goto Exit;
  1237. }
  1238. wsprintf (lpQuery, TEXT("SELECT name, id FROM RSOP_GPO where id=\"%s\""), lpGPOID);
  1239. pQuery = SysAllocString (lpQuery);
  1240. if (!pQuery)
  1241. {
  1242. DebugMsg((DM_WARNING, TEXT("CScriptsComponentData::GetGPOFriendlyName: Failed to allocate memory for query")));
  1243. hr = HRESULT_FROM_WIN32(ERROR_OUTOFMEMORY);
  1244. goto Exit;
  1245. }
  1246. //
  1247. // Allocate BSTRs for the property names we want to retreive
  1248. //
  1249. pName = SysAllocString (TEXT("name"));
  1250. if (!pName)
  1251. {
  1252. DebugMsg((DM_WARNING, TEXT("CScriptsComponentData::GetGPOFriendlyName: Failed to allocate memory for name")));
  1253. hr = HRESULT_FROM_WIN32(ERROR_OUTOFMEMORY);
  1254. goto Exit;
  1255. }
  1256. //
  1257. // Execute the query
  1258. //
  1259. hr = pIWbemServices->ExecQuery (pLanguage, pQuery,
  1260. WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
  1261. NULL, &pEnum);
  1262. if (FAILED(hr))
  1263. {
  1264. DebugMsg((DM_WARNING, TEXT("CScriptsComponentData::GetGPOFriendlyName: Failed to query for %s with 0x%x"),
  1265. pQuery, hr));
  1266. goto Exit;
  1267. }
  1268. //
  1269. // Loop through the results
  1270. //
  1271. hr = pEnum->Next(WBEM_INFINITE, 1, pObjects, &ulRet);
  1272. if (FAILED(hr))
  1273. {
  1274. DebugMsg((DM_WARNING, TEXT("CScriptsComponentData::GetGPOFriendlyName: Failed to get first item in query results for %s with 0x%x"),
  1275. pQuery, hr));
  1276. goto Exit;
  1277. }
  1278. //
  1279. // Check for the "data not available case"
  1280. //
  1281. if (ulRet == 0)
  1282. {
  1283. hr = S_OK;
  1284. goto Exit;
  1285. }
  1286. //
  1287. // Get the name
  1288. //
  1289. hr = pObjects[0]->Get (pName, 0, &varGPOName, NULL, NULL);
  1290. pObjects[0]->Release();
  1291. if (FAILED(hr))
  1292. {
  1293. DebugMsg((DM_WARNING, TEXT("CScriptsComponentData::GetGPOFriendlyName: Failed to get gponame in query results for %s with 0x%x"),
  1294. pQuery, hr));
  1295. goto Exit;
  1296. }
  1297. //
  1298. // Save the name
  1299. //
  1300. *pGPOName = (LPTSTR) LocalAlloc (LPTR, (lstrlen(varGPOName.bstrVal) + 1) * sizeof(TCHAR));
  1301. if (!(*pGPOName))
  1302. {
  1303. DebugMsg((DM_WARNING, TEXT("CScriptsComponentData::GetGPOFriendlyName: Failed to allocate memory for GPO Name")));
  1304. hr = HRESULT_FROM_WIN32(ERROR_OUTOFMEMORY);
  1305. goto Exit;
  1306. }
  1307. lstrcpy (*pGPOName, varGPOName.bstrVal);
  1308. VariantClear (&varGPOName);
  1309. hr = S_OK;
  1310. Exit:
  1311. if (pEnum)
  1312. {
  1313. pEnum->Release();
  1314. }
  1315. if (pQuery)
  1316. {
  1317. SysFreeString (pQuery);
  1318. }
  1319. if (lpQuery)
  1320. {
  1321. LocalFree (lpQuery);
  1322. }
  1323. if (pName)
  1324. {
  1325. SysFreeString (pName);
  1326. }
  1327. return hr;
  1328. }
  1329. VOID CScriptsComponentData::DumpRSOPScriptsData(LPRSOPSCRIPTITEM lpList)
  1330. {
  1331. DebugMsg((DM_VERBOSE, TEXT("CScriptsComponentData::DumpRSOPScriptsData: *** Entering ***")));
  1332. while (lpList)
  1333. {
  1334. if (lpList->lpCommandLine)
  1335. {
  1336. OutputDebugString (TEXT("Script: "));
  1337. OutputDebugString (lpList->lpCommandLine);
  1338. if (lpList->lpArgs)
  1339. {
  1340. OutputDebugString (TEXT(" "));
  1341. OutputDebugString (lpList->lpArgs);
  1342. }
  1343. OutputDebugString (TEXT("\n"));
  1344. }
  1345. else
  1346. OutputDebugString (TEXT("NULL command line\n"));
  1347. OutputDebugString (TEXT("GPO Name: "));
  1348. if (lpList->lpGPOName)
  1349. OutputDebugString (lpList->lpGPOName);
  1350. else
  1351. OutputDebugString (TEXT("NULL GPO Name"));
  1352. OutputDebugString (TEXT("\n"));
  1353. OutputDebugString (TEXT("Execution Date: "));
  1354. if (lpList->lpDate)
  1355. OutputDebugString (lpList->lpDate);
  1356. else
  1357. OutputDebugString (TEXT("NULL Execution Date"));
  1358. OutputDebugString (TEXT("\n\n"));
  1359. lpList = lpList->pNext;
  1360. }
  1361. }
  1362. ///////////////////////////////////////////////////////////////////////////////
  1363. // //
  1364. // Class factory object implementation //
  1365. // //
  1366. ///////////////////////////////////////////////////////////////////////////////
  1367. CScriptsComponentDataCF::CScriptsComponentDataCF(BOOL bUser, BOOL bRSOP)
  1368. {
  1369. m_cRef = 1;
  1370. InterlockedIncrement(&g_cRefThisDll);
  1371. m_bUserScope = bUser;
  1372. m_bRSOP = bRSOP;
  1373. }
  1374. CScriptsComponentDataCF::~CScriptsComponentDataCF()
  1375. {
  1376. InterlockedDecrement(&g_cRefThisDll);
  1377. }
  1378. ///////////////////////////////////////////////////////////////////////////////
  1379. // //
  1380. // Class factory object implementation (IUnknown) //
  1381. // //
  1382. ///////////////////////////////////////////////////////////////////////////////
  1383. STDMETHODIMP_(ULONG)
  1384. CScriptsComponentDataCF::AddRef()
  1385. {
  1386. return ++m_cRef;
  1387. }
  1388. STDMETHODIMP_(ULONG)
  1389. CScriptsComponentDataCF::Release()
  1390. {
  1391. if (--m_cRef == 0)
  1392. {
  1393. delete this;
  1394. return 0;
  1395. }
  1396. return m_cRef;
  1397. }
  1398. STDMETHODIMP
  1399. CScriptsComponentDataCF::QueryInterface(REFIID riid, LPVOID FAR* ppv)
  1400. {
  1401. if (IsEqualIID(riid, IID_IUnknown) || IsEqualIID(riid, IID_IClassFactory))
  1402. {
  1403. *ppv = (LPCLASSFACTORY)this;
  1404. m_cRef++;
  1405. return S_OK;
  1406. }
  1407. else
  1408. {
  1409. *ppv = NULL;
  1410. return E_NOINTERFACE;
  1411. }
  1412. }
  1413. ///////////////////////////////////////////////////////////////////////////////
  1414. // //
  1415. // Class factory object implementation (IClassFactory) //
  1416. // //
  1417. ///////////////////////////////////////////////////////////////////////////////
  1418. STDMETHODIMP
  1419. CScriptsComponentDataCF::CreateInstance(LPUNKNOWN pUnkOuter,
  1420. REFIID riid,
  1421. LPVOID FAR* ppvObj)
  1422. {
  1423. *ppvObj = NULL;
  1424. if (pUnkOuter)
  1425. return CLASS_E_NOAGGREGATION;
  1426. CScriptsComponentData *pComponentData = new CScriptsComponentData(m_bUserScope, m_bRSOP); // ref count == 1
  1427. if (!pComponentData)
  1428. return E_OUTOFMEMORY;
  1429. HRESULT hr = pComponentData->QueryInterface(riid, ppvObj);
  1430. pComponentData->Release(); // release initial ref
  1431. return hr;
  1432. }
  1433. STDMETHODIMP
  1434. CScriptsComponentDataCF::LockServer(BOOL fLock)
  1435. {
  1436. return E_NOTIMPL;
  1437. }
  1438. ///////////////////////////////////////////////////////////////////////////////
  1439. // //
  1440. // Class factory object creation (IClassFactory) //
  1441. // //
  1442. ///////////////////////////////////////////////////////////////////////////////
  1443. HRESULT CreateScriptsComponentDataClassFactory (REFCLSID rclsid, REFIID riid, LPVOID* ppv)
  1444. {
  1445. HRESULT hr;
  1446. if (IsEqualCLSID (rclsid, CLSID_ScriptSnapInMachine)) {
  1447. CScriptsComponentDataCF *pComponentDataCF = new CScriptsComponentDataCF(FALSE, FALSE); // ref == 1
  1448. if (!pComponentDataCF)
  1449. return E_OUTOFMEMORY;
  1450. hr = pComponentDataCF->QueryInterface(riid, ppv);
  1451. pComponentDataCF->Release(); // release initial ref
  1452. return hr;
  1453. }
  1454. if (IsEqualCLSID (rclsid, CLSID_ScriptSnapInUser)) {
  1455. CScriptsComponentDataCF *pComponentDataCF = new CScriptsComponentDataCF(TRUE, FALSE); // ref == 1
  1456. if (!pComponentDataCF)
  1457. return E_OUTOFMEMORY;
  1458. hr = pComponentDataCF->QueryInterface(riid, ppv);
  1459. pComponentDataCF->Release(); // release initial ref
  1460. return hr;
  1461. }
  1462. if (IsEqualCLSID (rclsid, CLSID_RSOPScriptSnapInMachine)) {
  1463. CScriptsComponentDataCF *pComponentDataCF = new CScriptsComponentDataCF(FALSE, TRUE); // ref == 1
  1464. if (!pComponentDataCF)
  1465. return E_OUTOFMEMORY;
  1466. hr = pComponentDataCF->QueryInterface(riid, ppv);
  1467. pComponentDataCF->Release(); // release initial ref
  1468. return hr;
  1469. }
  1470. if (IsEqualCLSID (rclsid, CLSID_RSOPScriptSnapInUser)) {
  1471. CScriptsComponentDataCF *pComponentDataCF = new CScriptsComponentDataCF(TRUE, TRUE); // ref == 1
  1472. if (!pComponentDataCF)
  1473. return E_OUTOFMEMORY;
  1474. hr = pComponentDataCF->QueryInterface(riid, ppv);
  1475. pComponentDataCF->Release(); // release initial ref
  1476. return hr;
  1477. }
  1478. return CLASS_E_CLASSNOTAVAILABLE;
  1479. }
  1480. unsigned int CScriptsSnapIn::m_cfNodeType = RegisterClipboardFormat(CCF_NODETYPE);
  1481. ///////////////////////////////////////////////////////////////////////////////
  1482. // //
  1483. // CScriptsSnapIn object implementation //
  1484. // //
  1485. ///////////////////////////////////////////////////////////////////////////////
  1486. CScriptsSnapIn::CScriptsSnapIn(CScriptsComponentData *pComponent)
  1487. {
  1488. m_cRef = 1;
  1489. InterlockedIncrement(&g_cRefThisDll);
  1490. m_pcd = pComponent;
  1491. m_pConsole = NULL;
  1492. m_pResult = NULL;
  1493. m_pHeader = NULL;
  1494. m_pConsoleVerb = NULL;
  1495. m_pDisplayHelp = NULL;
  1496. m_nColumn1Size = 180;
  1497. m_nColumn2Size = 180;
  1498. m_nColumn3Size = 160;
  1499. m_nColumn4Size = 200;
  1500. m_lViewMode = LVS_REPORT;
  1501. LoadString(g_hInstance, IDS_NAME, m_column1, ARRAYSIZE(m_column1));
  1502. LoadString(g_hInstance, IDS_PARAMETERS, m_column2, ARRAYSIZE(m_column2));
  1503. LoadString(g_hInstance, IDS_LASTEXECUTED, m_column3, ARRAYSIZE(m_column3));
  1504. LoadString(g_hInstance, IDS_GPONAME, m_column4, ARRAYSIZE(m_column4));
  1505. }
  1506. CScriptsSnapIn::~CScriptsSnapIn()
  1507. {
  1508. if (m_pConsole != NULL)
  1509. {
  1510. m_pConsole->SetHeader(NULL);
  1511. m_pConsole->Release();
  1512. m_pConsole = NULL;
  1513. }
  1514. if (m_pHeader != NULL)
  1515. {
  1516. m_pHeader->Release();
  1517. m_pHeader = NULL;
  1518. }
  1519. if (m_pResult != NULL)
  1520. {
  1521. m_pResult->Release();
  1522. m_pResult = NULL;
  1523. }
  1524. if (m_pConsoleVerb != NULL)
  1525. {
  1526. m_pConsoleVerb->Release();
  1527. m_pConsoleVerb = NULL;
  1528. }
  1529. if (m_pDisplayHelp != NULL)
  1530. {
  1531. m_pDisplayHelp->Release();
  1532. m_pDisplayHelp = NULL;
  1533. }
  1534. InterlockedDecrement(&g_cRefThisDll);
  1535. }
  1536. ///////////////////////////////////////////////////////////////////////////////
  1537. // //
  1538. // CScriptsSnapIn object implementation (IUnknown) //
  1539. // //
  1540. ///////////////////////////////////////////////////////////////////////////////
  1541. HRESULT CScriptsSnapIn::QueryInterface (REFIID riid, void **ppv)
  1542. {
  1543. if (IsEqualIID(riid, IID_IComponent) || IsEqualIID(riid, IID_IUnknown))
  1544. {
  1545. *ppv = (LPCOMPONENT)this;
  1546. m_cRef++;
  1547. return S_OK;
  1548. }
  1549. else if (IsEqualIID(riid, IID_IExtendPropertySheet))
  1550. {
  1551. *ppv = (LPEXTENDPROPERTYSHEET)this;
  1552. m_cRef++;
  1553. return S_OK;
  1554. }
  1555. else
  1556. {
  1557. *ppv = NULL;
  1558. return E_NOINTERFACE;
  1559. }
  1560. }
  1561. ULONG CScriptsSnapIn::AddRef (void)
  1562. {
  1563. return ++m_cRef;
  1564. }
  1565. ULONG CScriptsSnapIn::Release (void)
  1566. {
  1567. if (--m_cRef == 0) {
  1568. delete this;
  1569. return 0;
  1570. }
  1571. return m_cRef;
  1572. }
  1573. ///////////////////////////////////////////////////////////////////////////////
  1574. // //
  1575. // CScriptsSnapIn object implementation (IComponent) //
  1576. // //
  1577. ///////////////////////////////////////////////////////////////////////////////
  1578. STDMETHODIMP CScriptsSnapIn::Initialize(LPCONSOLE lpConsole)
  1579. {
  1580. HRESULT hr;
  1581. // Save the IConsole pointer
  1582. m_pConsole = lpConsole;
  1583. m_pConsole->AddRef();
  1584. hr = m_pConsole->QueryInterface(IID_IHeaderCtrl,
  1585. reinterpret_cast<void**>(&m_pHeader));
  1586. // Give the console the header control interface pointer
  1587. if (SUCCEEDED(hr))
  1588. m_pConsole->SetHeader(m_pHeader);
  1589. m_pConsole->QueryInterface(IID_IResultData,
  1590. reinterpret_cast<void**>(&m_pResult));
  1591. hr = m_pConsole->QueryConsoleVerb(&m_pConsoleVerb);
  1592. hr = m_pConsole->QueryInterface(IID_IDisplayHelp,
  1593. reinterpret_cast<void**>(&m_pDisplayHelp));
  1594. return S_OK;
  1595. }
  1596. STDMETHODIMP CScriptsSnapIn::Destroy(MMC_COOKIE cookie)
  1597. {
  1598. if (m_pConsole != NULL)
  1599. {
  1600. m_pConsole->SetHeader(NULL);
  1601. m_pConsole->Release();
  1602. m_pConsole = NULL;
  1603. }
  1604. if (m_pHeader != NULL)
  1605. {
  1606. m_pHeader->Release();
  1607. m_pHeader = NULL;
  1608. }
  1609. if (m_pResult != NULL)
  1610. {
  1611. m_pResult->Release();
  1612. m_pResult = NULL;
  1613. }
  1614. if (m_pConsoleVerb != NULL)
  1615. {
  1616. m_pConsoleVerb->Release();
  1617. m_pConsoleVerb = NULL;
  1618. }
  1619. if (m_pDisplayHelp != NULL)
  1620. {
  1621. m_pDisplayHelp->Release();
  1622. m_pDisplayHelp = NULL;
  1623. }
  1624. return S_OK;
  1625. }
  1626. STDMETHODIMP CScriptsSnapIn::Notify(LPDATAOBJECT lpDataObject, MMC_NOTIFY_TYPE event, LPARAM arg, LPARAM param)
  1627. {
  1628. HRESULT hr = S_OK;
  1629. switch(event)
  1630. {
  1631. case MMCN_COLUMNS_CHANGED:
  1632. hr = S_OK;
  1633. break;
  1634. case MMCN_DBLCLICK:
  1635. hr = S_FALSE;
  1636. break;
  1637. case MMCN_ADD_IMAGES:
  1638. HBITMAP hbmp16x16;
  1639. HBITMAP hbmp32x32;
  1640. hbmp16x16 = LoadBitmap(g_hInstance, MAKEINTRESOURCE(IDB_16x16));
  1641. if (hbmp16x16)
  1642. {
  1643. hbmp32x32 = LoadBitmap(g_hInstance, MAKEINTRESOURCE(IDB_32x32));
  1644. if (hbmp32x32)
  1645. {
  1646. LPIMAGELIST pImageList = (LPIMAGELIST) arg;
  1647. // Set the images
  1648. pImageList->ImageListSetStrip(reinterpret_cast<LONG_PTR *>(hbmp16x16),
  1649. reinterpret_cast<LONG_PTR *>(hbmp32x32),
  1650. 0, RGB(255, 0, 255));
  1651. DeleteObject(hbmp32x32);
  1652. }
  1653. DeleteObject(hbmp16x16);
  1654. }
  1655. break;
  1656. case MMCN_SHOW:
  1657. if (arg == TRUE)
  1658. {
  1659. RESULTDATAITEM resultItem;
  1660. LPSCRIPTDATAOBJECT pScriptDataObject;
  1661. MMC_COOKIE cookie;
  1662. INT i, iDescStringID;
  1663. ULONG ulCount = 0;
  1664. LPRSOPSCRIPTITEM lpTemp;
  1665. LPSCRIPTRESULTITEM lpScriptItem;
  1666. TCHAR szDesc[100], szFullDesc[120];
  1667. //
  1668. // Get the cookie of the scope pane item
  1669. //
  1670. hr = lpDataObject->QueryInterface(IID_IScriptDataObject, (LPVOID *)&pScriptDataObject);
  1671. if (FAILED(hr))
  1672. return S_OK;
  1673. hr = pScriptDataObject->GetCookie(&cookie);
  1674. pScriptDataObject->Release(); // release initial ref
  1675. if (FAILED(hr))
  1676. return S_OK;
  1677. //
  1678. // Prepare the view
  1679. //
  1680. m_pHeader->InsertColumn(0, m_column1, LVCFMT_LEFT, m_nColumn1Size);
  1681. if (cookie > 2)
  1682. {
  1683. m_pHeader->InsertColumn(1, m_column2, LVCFMT_LEFT, m_nColumn2Size);
  1684. m_pHeader->InsertColumn(2, m_column3, LVCFMT_LEFT, m_nColumn3Size);
  1685. m_pHeader->InsertColumn(3, m_column4, LVCFMT_LEFT, m_nColumn4Size);
  1686. }
  1687. m_pResult->SetViewMode(m_lViewMode);
  1688. //
  1689. // Add result pane items for this node
  1690. //
  1691. for (i = 0; i < m_pcd->m_pNameSpaceItems[cookie].cResultItems; i++)
  1692. {
  1693. lpScriptItem = (LPSCRIPTRESULTITEM) LocalAlloc (LPTR, sizeof(SCRIPTRESULTITEM));
  1694. if (lpScriptItem)
  1695. {
  1696. lpScriptItem->lpResultItem = &m_pcd->m_pNameSpaceItems[cookie].pResultItems[i];
  1697. lpScriptItem->iDescStringID = m_pcd->m_pNameSpaceItems[cookie].pResultItems[i].iDescStringID;
  1698. lpScriptItem->pNodeID = m_pcd->m_pNameSpaceItems[cookie].pNodeID;
  1699. resultItem.mask = RDI_STR | RDI_IMAGE | RDI_PARAM;
  1700. resultItem.str = MMC_CALLBACK;
  1701. resultItem.nImage = m_pcd->m_pNameSpaceItems[cookie].pResultItems[i].iImage;
  1702. resultItem.lParam = (LPARAM) lpScriptItem;
  1703. m_pResult->InsertItem(&resultItem);
  1704. }
  1705. }
  1706. if (cookie > 2)
  1707. {
  1708. if (cookie == 3)
  1709. {
  1710. lpTemp = m_pcd->m_pRSOPLogon;
  1711. iDescStringID = IDS_LOGON_DESC;
  1712. }
  1713. else if (cookie == 4)
  1714. {
  1715. lpTemp = m_pcd->m_pRSOPLogoff;
  1716. iDescStringID = IDS_LOGOFF_DESC;
  1717. }
  1718. else if (cookie == 5)
  1719. {
  1720. lpTemp = m_pcd->m_pRSOPStartup;
  1721. iDescStringID = IDS_STARTUP_DESC;
  1722. }
  1723. else
  1724. {
  1725. lpTemp = m_pcd->m_pRSOPShutdown;
  1726. iDescStringID = IDS_SHUTDOWN_DESC;
  1727. }
  1728. while (lpTemp)
  1729. {
  1730. lpScriptItem = (LPSCRIPTRESULTITEM) LocalAlloc (LPTR, sizeof(SCRIPTRESULTITEM));
  1731. if (lpScriptItem)
  1732. {
  1733. lpScriptItem->lpRSOPScriptItem = lpTemp;
  1734. lpScriptItem->iDescStringID = iDescStringID;
  1735. lpScriptItem->pNodeID = m_pcd->m_pNameSpaceItems[cookie].pNodeID;
  1736. resultItem.mask = RDI_STR | RDI_IMAGE | RDI_PARAM;
  1737. resultItem.str = MMC_CALLBACK;
  1738. resultItem.nImage = 3;
  1739. resultItem.lParam = (LPARAM) lpScriptItem;
  1740. if (SUCCEEDED(m_pResult->InsertItem(&resultItem)))
  1741. {
  1742. if (lpTemp->lpArgs)
  1743. {
  1744. resultItem.mask = RDI_STR | RDI_PARAM;
  1745. resultItem.str = MMC_CALLBACK;
  1746. resultItem.bScopeItem = FALSE;
  1747. resultItem.nCol = 1;
  1748. resultItem.lParam = (LPARAM) lpScriptItem;
  1749. m_pResult->SetItem(&resultItem);
  1750. }
  1751. if (lpTemp->lpDate)
  1752. {
  1753. resultItem.mask = RDI_STR | RDI_PARAM;
  1754. resultItem.str = MMC_CALLBACK;
  1755. resultItem.bScopeItem = FALSE;
  1756. resultItem.nCol = 2;
  1757. resultItem.lParam = (LPARAM) lpScriptItem;
  1758. m_pResult->SetItem(&resultItem);
  1759. }
  1760. if (lpTemp->lpGPOName)
  1761. {
  1762. resultItem.mask = RDI_STR | RDI_PARAM;
  1763. resultItem.str = MMC_CALLBACK;
  1764. resultItem.bScopeItem = FALSE;
  1765. resultItem.nCol = 3;
  1766. resultItem.lParam = (LPARAM) lpScriptItem;
  1767. m_pResult->SetItem(&resultItem);
  1768. }
  1769. }
  1770. }
  1771. lpTemp = lpTemp->pNext;
  1772. ulCount++;
  1773. }
  1774. LoadString(g_hInstance, IDS_DESCTEXT, szDesc, ARRAYSIZE(szDesc));
  1775. wsprintf (szFullDesc, szDesc, ulCount);
  1776. m_pResult->SetDescBarText(szFullDesc);
  1777. }
  1778. }
  1779. else
  1780. {
  1781. INT i = 0;
  1782. RESULTDATAITEM resultItem;
  1783. while (TRUE)
  1784. {
  1785. ZeroMemory (&resultItem, sizeof(resultItem));
  1786. resultItem.mask = RDI_PARAM;
  1787. resultItem.nIndex = i;
  1788. hr = m_pResult->GetItem(&resultItem);
  1789. if (hr != S_OK)
  1790. {
  1791. break;
  1792. }
  1793. if (!resultItem.bScopeItem)
  1794. {
  1795. LocalFree ((LPSCRIPTRESULTITEM)resultItem.lParam);
  1796. }
  1797. i++;
  1798. }
  1799. m_pResult->DeleteAllRsltItems();
  1800. m_pHeader->GetColumnWidth(0, &m_nColumn1Size);
  1801. m_pHeader->GetColumnWidth(0, &m_nColumn2Size);
  1802. m_pHeader->GetColumnWidth(0, &m_nColumn3Size);
  1803. m_pHeader->GetColumnWidth(0, &m_nColumn4Size);
  1804. m_pResult->GetViewMode(&m_lViewMode);
  1805. m_pResult->SetDescBarText(L"");
  1806. }
  1807. break;
  1808. case MMCN_SELECT:
  1809. if (m_pConsoleVerb)
  1810. {
  1811. LPSCRIPTDATAOBJECT pScriptDataObject;
  1812. DATA_OBJECT_TYPES type;
  1813. MMC_COOKIE cookie;
  1814. //
  1815. // Set the default verb to open
  1816. //
  1817. m_pConsoleVerb->SetDefaultVerb(MMC_VERB_OPEN);
  1818. //
  1819. // See if this is one of our items.
  1820. //
  1821. hr = lpDataObject->QueryInterface(IID_IScriptDataObject, (LPVOID *)&pScriptDataObject);
  1822. if (FAILED(hr))
  1823. break;
  1824. pScriptDataObject->GetType(&type);
  1825. pScriptDataObject->GetCookie(&cookie);
  1826. pScriptDataObject->Release();
  1827. //
  1828. // If this is a GPE result pane item or the root of the namespace
  1829. // nodes, enable the Properties menu item
  1830. //
  1831. if (type == CCT_SCOPE)
  1832. {
  1833. if (cookie == 0)
  1834. {
  1835. m_pConsoleVerb->SetVerbState(MMC_VERB_PROPERTIES, ENABLED, TRUE);
  1836. }
  1837. }
  1838. else
  1839. {
  1840. if (!m_pcd->m_bRSOP)
  1841. {
  1842. if (HIWORD(arg))
  1843. {
  1844. m_pConsoleVerb->SetVerbState(MMC_VERB_PROPERTIES, ENABLED, TRUE);
  1845. m_pConsoleVerb->SetDefaultVerb(MMC_VERB_PROPERTIES);
  1846. }
  1847. }
  1848. }
  1849. }
  1850. break;
  1851. case MMCN_CONTEXTHELP:
  1852. {
  1853. if (m_pDisplayHelp)
  1854. {
  1855. LPSCRIPTDATAOBJECT pScriptDataObject;
  1856. DATA_OBJECT_TYPES type;
  1857. MMC_COOKIE cookie;
  1858. LPOLESTR pszHelpTopic;
  1859. //
  1860. // See if this is one of our items.
  1861. //
  1862. hr = lpDataObject->QueryInterface(IID_IScriptDataObject, (LPVOID *)&pScriptDataObject);
  1863. if (FAILED(hr))
  1864. break;
  1865. pScriptDataObject->Release();
  1866. //
  1867. // Display the scripts help page
  1868. //
  1869. pszHelpTopic = (LPOLESTR) CoTaskMemAlloc (50 * sizeof(WCHAR));
  1870. if (pszHelpTopic)
  1871. {
  1872. lstrcpy (pszHelpTopic, TEXT("gpedit.chm::/scripts.htm"));
  1873. m_pDisplayHelp->ShowTopic (pszHelpTopic);
  1874. }
  1875. }
  1876. }
  1877. break;
  1878. default:
  1879. hr = E_UNEXPECTED;
  1880. break;
  1881. }
  1882. return hr;
  1883. }
  1884. STDMETHODIMP CScriptsSnapIn::GetDisplayInfo(LPRESULTDATAITEM pResult)
  1885. {
  1886. if (pResult)
  1887. {
  1888. if (pResult->bScopeItem == TRUE)
  1889. {
  1890. if (pResult->mask & RDI_STR)
  1891. {
  1892. if (pResult->nCol == 0)
  1893. pResult->str = m_pcd->m_pNameSpaceItems[pResult->lParam].szDisplayName;
  1894. else
  1895. pResult->str = L"";
  1896. }
  1897. if (pResult->mask & RDI_IMAGE)
  1898. {
  1899. pResult->nImage = (pResult->lParam > 2) ? 0 : 3;
  1900. }
  1901. }
  1902. else
  1903. {
  1904. if (pResult->mask & RDI_STR)
  1905. {
  1906. if (pResult->nCol == 0)
  1907. {
  1908. if (m_pcd->m_bRSOP)
  1909. {
  1910. LPSCRIPTRESULTITEM lpItem = (LPSCRIPTRESULTITEM) pResult->lParam;
  1911. pResult->str = lpItem->lpRSOPScriptItem->lpCommandLine;
  1912. }
  1913. else
  1914. {
  1915. LPSCRIPTRESULTITEM lpItem = (LPSCRIPTRESULTITEM) pResult->lParam;
  1916. if (lpItem->lpResultItem->szDisplayName[0] == TEXT('\0'))
  1917. {
  1918. LoadString (g_hInstance, lpItem->lpResultItem->iStringID,
  1919. lpItem->lpResultItem->szDisplayName,
  1920. MAX_DISPLAYNAME_SIZE);
  1921. }
  1922. pResult->str = lpItem->lpResultItem->szDisplayName;
  1923. }
  1924. }
  1925. if (pResult->nCol == 1)
  1926. {
  1927. LPSCRIPTRESULTITEM lpItem = (LPSCRIPTRESULTITEM) pResult->lParam;
  1928. pResult->str = lpItem->lpRSOPScriptItem->lpArgs;
  1929. }
  1930. if (pResult->nCol == 2)
  1931. {
  1932. LPSCRIPTRESULTITEM lpItem = (LPSCRIPTRESULTITEM) pResult->lParam;
  1933. pResult->str = lpItem->lpRSOPScriptItem->lpDate;
  1934. }
  1935. if (pResult->nCol == 3)
  1936. {
  1937. LPSCRIPTRESULTITEM lpItem = (LPSCRIPTRESULTITEM) pResult->lParam;
  1938. pResult->str = lpItem->lpRSOPScriptItem->lpGPOName;
  1939. }
  1940. if (pResult->str == NULL)
  1941. pResult->str = (LPOLESTR)L"";
  1942. }
  1943. }
  1944. }
  1945. return S_OK;
  1946. }
  1947. STDMETHODIMP CScriptsSnapIn::QueryDataObject(MMC_COOKIE cookie, DATA_OBJECT_TYPES type, LPDATAOBJECT *ppDataObject)
  1948. {
  1949. return m_pcd->QueryDataObject(cookie, type, ppDataObject);
  1950. }
  1951. STDMETHODIMP CScriptsSnapIn::GetResultViewType(MMC_COOKIE cookie, LPOLESTR *ppViewType,
  1952. long *pViewOptions)
  1953. {
  1954. return S_FALSE;
  1955. }
  1956. STDMETHODIMP CScriptsSnapIn::CompareObjects(LPDATAOBJECT lpDataObjectA, LPDATAOBJECT lpDataObjectB)
  1957. {
  1958. HRESULT hr = S_FALSE;
  1959. LPSCRIPTDATAOBJECT pScriptDataObjectA, pScriptDataObjectB;
  1960. MMC_COOKIE cookie1, cookie2;
  1961. if (lpDataObjectA == NULL || lpDataObjectB == NULL)
  1962. return E_POINTER;
  1963. //
  1964. // QI for the private GPTDataObject interface
  1965. //
  1966. if (FAILED(lpDataObjectA->QueryInterface(IID_IScriptDataObject,
  1967. (LPVOID *)&pScriptDataObjectA)))
  1968. {
  1969. return S_FALSE;
  1970. }
  1971. if (FAILED(lpDataObjectB->QueryInterface(IID_IScriptDataObject,
  1972. (LPVOID *)&pScriptDataObjectB)))
  1973. {
  1974. pScriptDataObjectA->Release();
  1975. return S_FALSE;
  1976. }
  1977. pScriptDataObjectA->GetCookie(&cookie1);
  1978. pScriptDataObjectB->GetCookie(&cookie2);
  1979. if (cookie1 == cookie2)
  1980. {
  1981. hr = S_OK;
  1982. }
  1983. pScriptDataObjectA->Release();
  1984. pScriptDataObjectB->Release();
  1985. return hr;
  1986. }
  1987. ///////////////////////////////////////////////////////////////////////////////
  1988. // //
  1989. // CScriptsSnapIn object implementation (IExtendPropertySheet) //
  1990. // //
  1991. ///////////////////////////////////////////////////////////////////////////////
  1992. STDMETHODIMP CScriptsSnapIn::CreatePropertyPages(LPPROPERTYSHEETCALLBACK lpProvider,
  1993. LONG_PTR handle, LPDATAOBJECT lpDataObject)
  1994. {
  1995. HRESULT hr;
  1996. PROPSHEETPAGE psp;
  1997. HPROPSHEETPAGE hPage[2];
  1998. LPSCRIPTDATAOBJECT pScriptDataObject;
  1999. LPSCRIPTINFO lpScriptInfo;
  2000. LPSCRIPTRESULTITEM pItem;
  2001. MMC_COOKIE cookie;
  2002. //
  2003. // Make sure this is one of our objects
  2004. //
  2005. if (FAILED(lpDataObject->QueryInterface(IID_IScriptDataObject,
  2006. (LPVOID *)&pScriptDataObject)))
  2007. {
  2008. return S_OK;
  2009. }
  2010. //
  2011. // Get the cookie
  2012. //
  2013. pScriptDataObject->GetCookie(&cookie);
  2014. pScriptDataObject->Release();
  2015. pItem = (LPSCRIPTRESULTITEM)cookie;
  2016. //
  2017. // Allocate a script info struct to pass to the dialog
  2018. //
  2019. lpScriptInfo = (LPSCRIPTINFO) LocalAlloc (LPTR, sizeof(SCRIPTINFO));
  2020. if (!lpScriptInfo)
  2021. {
  2022. return S_OK;
  2023. }
  2024. lpScriptInfo->pCS = this;
  2025. //
  2026. // Initialize the common fields in the property sheet structure
  2027. //
  2028. psp.dwSize = sizeof(PROPSHEETPAGE);
  2029. psp.dwFlags = 0;
  2030. psp.hInstance = g_hInstance;
  2031. psp.lParam = (LPARAM) lpScriptInfo;
  2032. //
  2033. // Do the page specific stuff
  2034. //
  2035. switch (pItem->lpResultItem->dwID)
  2036. {
  2037. case 2:
  2038. psp.pszTemplate = MAKEINTRESOURCE(IDD_SCRIPT);
  2039. psp.pfnDlgProc = ScriptDlgProc;
  2040. lpScriptInfo->ScriptType = ScriptType_Logon;
  2041. hPage[0] = CreatePropertySheetPage(&psp);
  2042. if (hPage[0])
  2043. {
  2044. hr = lpProvider->AddPage(hPage[0]);
  2045. }
  2046. else
  2047. {
  2048. DebugMsg((DM_WARNING, TEXT("CScriptsSnapIn::CreatePropertyPages: Failed to create property sheet page with %d."),
  2049. GetLastError()));
  2050. hr = E_FAIL;
  2051. }
  2052. break;
  2053. case 3:
  2054. psp.pszTemplate = MAKEINTRESOURCE(IDD_SCRIPT);
  2055. psp.pfnDlgProc = ScriptDlgProc;
  2056. lpScriptInfo->ScriptType = ScriptType_Logoff;
  2057. hPage[0] = CreatePropertySheetPage(&psp);
  2058. if (hPage[0])
  2059. {
  2060. hr = lpProvider->AddPage(hPage[0]);
  2061. }
  2062. else
  2063. {
  2064. DebugMsg((DM_WARNING, TEXT("CScriptsSnapIn::CreatePropertyPages: Failed to create property sheet page with %d."),
  2065. GetLastError()));
  2066. hr = E_FAIL;
  2067. }
  2068. break;
  2069. case 4:
  2070. psp.pszTemplate = MAKEINTRESOURCE(IDD_SCRIPT);
  2071. psp.pfnDlgProc = ScriptDlgProc;
  2072. lpScriptInfo->ScriptType = ScriptType_Startup;
  2073. hPage[0] = CreatePropertySheetPage(&psp);
  2074. if (hPage[0])
  2075. {
  2076. hr = lpProvider->AddPage(hPage[0]);
  2077. }
  2078. else
  2079. {
  2080. DebugMsg((DM_WARNING, TEXT("CScriptsSnapIn::CreatePropertyPages: Failed to create property sheet page with %d."),
  2081. GetLastError()));
  2082. hr = E_FAIL;
  2083. }
  2084. break;
  2085. case 5:
  2086. psp.pszTemplate = MAKEINTRESOURCE(IDD_SCRIPT);
  2087. psp.pfnDlgProc = ScriptDlgProc;
  2088. lpScriptInfo->ScriptType = ScriptType_Shutdown;
  2089. hPage[0] = CreatePropertySheetPage(&psp);
  2090. if (hPage[0])
  2091. {
  2092. hr = lpProvider->AddPage(hPage[0]);
  2093. }
  2094. else
  2095. {
  2096. DebugMsg((DM_WARNING, TEXT("CScriptsSnapIn::CreatePropertyPages: Failed to create property sheet page with %d."),
  2097. GetLastError()));
  2098. hr = E_FAIL;
  2099. }
  2100. break;
  2101. }
  2102. return (hr);
  2103. }
  2104. STDMETHODIMP CScriptsSnapIn::QueryPagesFor(LPDATAOBJECT lpDataObject)
  2105. {
  2106. LPSCRIPTDATAOBJECT pScriptDataObject;
  2107. DATA_OBJECT_TYPES type;
  2108. if (SUCCEEDED(lpDataObject->QueryInterface(IID_IScriptDataObject,
  2109. (LPVOID *)&pScriptDataObject)))
  2110. {
  2111. pScriptDataObject->GetType(&type);
  2112. pScriptDataObject->Release();
  2113. if ((type == CCT_RESULT) && (!m_pcd->m_bRSOP))
  2114. return S_OK;
  2115. }
  2116. return S_FALSE;
  2117. }
  2118. ///////////////////////////////////////////////////////////////////////////////
  2119. // //
  2120. // CScriptsSnapIn object implementation (Internal functions) //
  2121. // //
  2122. ///////////////////////////////////////////////////////////////////////////////
  2123. INT_PTR CALLBACK CScriptsSnapIn::ScriptDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  2124. {
  2125. LPSCRIPTINFO lpScriptInfo;
  2126. HRESULT hr;
  2127. switch (message)
  2128. {
  2129. case WM_INITDIALOG:
  2130. {
  2131. TCHAR szType[30];
  2132. TCHAR szSection[30];
  2133. TCHAR szKeyName[30];
  2134. TCHAR szGPOName[256];
  2135. TCHAR szBuffer1[MAX_PATH + 50];
  2136. TCHAR szBuffer2[2 * MAX_PATH];
  2137. TCHAR szBuffer3[MAX_PATH];
  2138. LPTSTR lpEnd;
  2139. LVCOLUMN lvc;
  2140. LV_ITEM item;
  2141. HWND hLV;
  2142. RECT rc;
  2143. INT iIndex;
  2144. //
  2145. // Save the scriptinfo pointer for future use
  2146. //
  2147. lpScriptInfo = (LPSCRIPTINFO) (((LPPROPSHEETPAGE)lParam)->lParam);
  2148. SetWindowLongPtr (hDlg, DWLP_USER, (LONG_PTR) lpScriptInfo);
  2149. //
  2150. // Query for the GPO display name
  2151. //
  2152. hr = lpScriptInfo->pCS->m_pcd->m_pGPTInformation->GetDisplayName(szGPOName, ARRAYSIZE(szGPOName));
  2153. if (FAILED(hr))
  2154. break;
  2155. //
  2156. // Load the type description
  2157. //
  2158. switch (lpScriptInfo->ScriptType)
  2159. {
  2160. case ScriptType_Logon:
  2161. LoadString (g_hInstance, IDS_LOGON, szType, ARRAYSIZE(szType));
  2162. lstrcpy (szSection, TEXT("Logon"));
  2163. break;
  2164. case ScriptType_Logoff:
  2165. LoadString (g_hInstance, IDS_LOGOFF, szType, ARRAYSIZE(szType));
  2166. lstrcpy (szSection, TEXT("Logoff"));
  2167. break;
  2168. case ScriptType_Startup:
  2169. LoadString (g_hInstance, IDS_STARTUP, szType, ARRAYSIZE(szType));
  2170. lstrcpy (szSection, TEXT("Startup"));
  2171. break;
  2172. case ScriptType_Shutdown:
  2173. LoadString (g_hInstance, IDS_SHUTDOWN, szType, ARRAYSIZE(szType));
  2174. lstrcpy (szSection, TEXT("Shutdown"));
  2175. break;
  2176. default:
  2177. DebugMsg((DM_WARNING, TEXT("CScriptsSnapIn::ScriptDlgProc: Unknown script type.")));
  2178. hr = E_FAIL;
  2179. break;
  2180. }
  2181. if (FAILED(hr))
  2182. break;
  2183. //
  2184. // Initialize the title and header
  2185. //
  2186. GetDlgItemText (hDlg, IDC_SCRIPT_TITLE, szBuffer1, ARRAYSIZE(szBuffer1));
  2187. wsprintf (szBuffer2, szBuffer1, szType, szGPOName);
  2188. SetDlgItemText (hDlg, IDC_SCRIPT_TITLE, szBuffer2);
  2189. GetDlgItemText (hDlg, IDC_SCRIPT_HEADING, szBuffer1, ARRAYSIZE(szBuffer1));
  2190. wsprintf (szBuffer2, szBuffer1, szType, szGPOName);
  2191. SetDlgItemText (hDlg, IDC_SCRIPT_HEADING, szBuffer2);
  2192. //
  2193. // Set initial state of buttons
  2194. //
  2195. EnableWindow (GetDlgItem (hDlg, IDC_SCRIPT_UP), FALSE);
  2196. EnableWindow (GetDlgItem (hDlg, IDC_SCRIPT_DOWN), FALSE);
  2197. EnableWindow (GetDlgItem (hDlg, IDC_SCRIPT_EDIT), FALSE);
  2198. EnableWindow (GetDlgItem (hDlg, IDC_SCRIPT_REMOVE), FALSE);
  2199. //
  2200. // Set extended LV styles
  2201. //
  2202. hLV = GetDlgItem (hDlg, IDC_SCRIPT_LIST);
  2203. SendMessage(hLV, LVM_SETEXTENDEDLISTVIEWSTYLE, 0,
  2204. LVS_EX_FULLROWSELECT | LVS_EX_LABELTIP);
  2205. //
  2206. // Insert the columns into the listview
  2207. //
  2208. GetClientRect (hLV, &rc);
  2209. LoadString (g_hInstance, IDS_NAME, szBuffer1, ARRAYSIZE(szBuffer1));
  2210. lvc.mask = LVCF_FMT | LVCF_SUBITEM | LVCF_TEXT | LVCF_WIDTH;
  2211. lvc.fmt = LVCFMT_LEFT;
  2212. lvc.cx = (int)(rc.right * .50);
  2213. lvc.pszText = szBuffer1;
  2214. lvc.cchTextMax = ARRAYSIZE(szBuffer1);
  2215. lvc.iSubItem = 0;
  2216. SendMessage (hLV, LVM_INSERTCOLUMN, 0, (LPARAM) &lvc);
  2217. LoadString (g_hInstance, IDS_PARAMETERS, szBuffer1, ARRAYSIZE(szBuffer1));
  2218. lvc.mask = LVCF_FMT | LVCF_SUBITEM | LVCF_TEXT | LVCF_WIDTH;
  2219. lvc.fmt = LVCFMT_LEFT;
  2220. lvc.cx = rc.right - lvc.cx;
  2221. lvc.pszText = szBuffer1;
  2222. lvc.cchTextMax = ARRAYSIZE(szBuffer1);
  2223. lvc.iSubItem = 0;
  2224. SendMessage (hLV, LVM_INSERTCOLUMN, 1, (LPARAM) &lvc);
  2225. //
  2226. // Insert existing scripts
  2227. //
  2228. lstrcpy (szBuffer1, lpScriptInfo->pCS->m_pcd->m_pScriptsDir);
  2229. lstrcat (szBuffer1, TEXT("\\"));
  2230. lstrcat (szBuffer1, SCRIPTS_FILE_NAME);
  2231. iIndex = 0;
  2232. while (TRUE)
  2233. {
  2234. //
  2235. // Get the command line
  2236. //
  2237. szBuffer3[0] = TEXT('\0');
  2238. _itot (iIndex, szKeyName, 10);
  2239. lpEnd = szKeyName + lstrlen (szKeyName);
  2240. lstrcpy (lpEnd, TEXT("CmdLine"));
  2241. GetPrivateProfileString (szSection, szKeyName, TEXT(""),
  2242. szBuffer3, ARRAYSIZE(szBuffer3),
  2243. szBuffer1);
  2244. if (szBuffer3[0] == TEXT('\0'))
  2245. break;
  2246. //
  2247. // Get the parameters
  2248. //
  2249. szBuffer2[0] = TEXT('\0');
  2250. lstrcpy (lpEnd, TEXT("Parameters"));
  2251. GetPrivateProfileString (szSection, szKeyName, TEXT(""),
  2252. szBuffer2, ARRAYSIZE(szBuffer2),
  2253. szBuffer1);
  2254. //
  2255. // Add script to the list
  2256. //
  2257. lpScriptInfo->pCS->AddScriptToList (hLV, szBuffer3, szBuffer2);
  2258. //
  2259. // Loop again
  2260. //
  2261. iIndex++;
  2262. }
  2263. //
  2264. // Select the first item
  2265. //
  2266. item.mask = LVIF_STATE;
  2267. item.iItem = 0;
  2268. item.iSubItem = 0;
  2269. item.state = LVIS_SELECTED | LVIS_FOCUSED;
  2270. item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
  2271. SendMessage (hLV, LVM_SETITEMSTATE, 0, (LPARAM) &item);
  2272. break;
  2273. }
  2274. case WM_COMMAND:
  2275. if (LOWORD(wParam) == IDC_SCRIPT_UP)
  2276. {
  2277. INT iSrc, iDest;
  2278. LPSCRIPTITEM lpSrc, lpDest;
  2279. HWND hLV = GetDlgItem(hDlg, IDC_SCRIPT_LIST);
  2280. LVITEM item;
  2281. iSrc = ListView_GetNextItem (hLV, -1,
  2282. LVNI_ALL | LVNI_SELECTED);
  2283. if (iSrc != -1)
  2284. {
  2285. iDest = iSrc - 1;
  2286. //
  2287. // Get the current lpScriptItem pointers
  2288. //
  2289. item.mask = LVIF_PARAM;
  2290. item.iItem = iSrc;
  2291. item.iSubItem = 0;
  2292. if (!ListView_GetItem (hLV, &item))
  2293. {
  2294. break;
  2295. }
  2296. lpSrc = (LPSCRIPTITEM) item.lParam;
  2297. item.mask = LVIF_PARAM;
  2298. item.iItem = iDest;
  2299. item.iSubItem = 0;
  2300. if (!ListView_GetItem (hLV, &item))
  2301. {
  2302. break;
  2303. }
  2304. lpDest = (LPSCRIPTITEM) item.lParam;
  2305. //
  2306. // Swap them
  2307. //
  2308. item.mask = LVIF_PARAM;
  2309. item.iItem = iSrc;
  2310. item.iSubItem = 0;
  2311. item.lParam = (LPARAM)lpDest;
  2312. if (!ListView_SetItem (hLV, &item))
  2313. {
  2314. break;
  2315. }
  2316. item.mask = LVIF_PARAM;
  2317. item.iItem = iDest;
  2318. item.iSubItem = 0;
  2319. item.lParam = (LPARAM)lpSrc;
  2320. if (!ListView_SetItem (hLV, &item))
  2321. {
  2322. break;
  2323. }
  2324. //
  2325. // Select the item
  2326. //
  2327. item.mask = LVIF_STATE;
  2328. item.iItem = iSrc;
  2329. item.iSubItem = 0;
  2330. item.state = 0;
  2331. item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
  2332. SendMessage (hLV, LVM_SETITEMSTATE, iSrc, (LPARAM) &item);
  2333. item.mask = LVIF_STATE;
  2334. item.iItem = iDest;
  2335. item.iSubItem = 0;
  2336. item.state = LVIS_SELECTED | LVIS_FOCUSED;
  2337. item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
  2338. SendMessage (hLV, LVM_SETITEMSTATE, iDest, (LPARAM) &item);
  2339. //
  2340. // Update the listview
  2341. //
  2342. ListView_RedrawItems (hLV, iDest, iSrc);
  2343. SendMessage (GetParent(hDlg), PSM_CHANGED, (WPARAM) hDlg, 0);
  2344. SetFocus (hLV);
  2345. }
  2346. }
  2347. else if (LOWORD(wParam) == IDC_SCRIPT_DOWN)
  2348. {
  2349. INT iSrc, iDest;
  2350. LPSCRIPTITEM lpSrc, lpDest;
  2351. HWND hLV = GetDlgItem(hDlg, IDC_SCRIPT_LIST);
  2352. LVITEM item;
  2353. iSrc = ListView_GetNextItem (hLV, -1,
  2354. LVNI_ALL | LVNI_SELECTED);
  2355. if (iSrc != -1)
  2356. {
  2357. iDest = iSrc + 1;
  2358. //
  2359. // Get the current lpScriptItem pointers
  2360. //
  2361. item.mask = LVIF_PARAM;
  2362. item.iItem = iSrc;
  2363. item.iSubItem = 0;
  2364. if (!ListView_GetItem (hLV, &item))
  2365. {
  2366. break;
  2367. }
  2368. lpSrc = (LPSCRIPTITEM) item.lParam;
  2369. item.mask = LVIF_PARAM;
  2370. item.iItem = iDest;
  2371. item.iSubItem = 0;
  2372. if (!ListView_GetItem (hLV, &item))
  2373. {
  2374. break;
  2375. }
  2376. lpDest = (LPSCRIPTITEM) item.lParam;
  2377. //
  2378. // Swap them
  2379. //
  2380. item.mask = LVIF_PARAM;
  2381. item.iItem = iSrc;
  2382. item.iSubItem = 0;
  2383. item.lParam = (LPARAM)lpDest;
  2384. if (!ListView_SetItem (hLV, &item))
  2385. {
  2386. break;
  2387. }
  2388. item.mask = LVIF_PARAM;
  2389. item.iItem = iDest;
  2390. item.iSubItem = 0;
  2391. item.lParam = (LPARAM)lpSrc;
  2392. if (!ListView_SetItem (hLV, &item))
  2393. {
  2394. break;
  2395. }
  2396. //
  2397. // Select the item
  2398. //
  2399. item.mask = LVIF_STATE;
  2400. item.iItem = iSrc;
  2401. item.iSubItem = 0;
  2402. item.state = 0;
  2403. item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
  2404. SendMessage (hLV, LVM_SETITEMSTATE, iSrc, (LPARAM) &item);
  2405. item.mask = LVIF_STATE;
  2406. item.iItem = iDest;
  2407. item.iSubItem = 0;
  2408. item.state = LVIS_SELECTED | LVIS_FOCUSED;
  2409. item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
  2410. SendMessage (hLV, LVM_SETITEMSTATE, iDest, (LPARAM) &item);
  2411. //
  2412. // Update the listview
  2413. //
  2414. ListView_RedrawItems (hLV, iSrc, iDest);
  2415. SendMessage (GetParent(hDlg), PSM_CHANGED, (WPARAM) hDlg, 0);
  2416. SetFocus (hLV);
  2417. }
  2418. }
  2419. else if (LOWORD(wParam) == IDC_SCRIPT_ADD)
  2420. {
  2421. SCRIPTEDITINFO info;
  2422. TCHAR szName[MAX_PATH];
  2423. TCHAR szArgs[2 * MAX_PATH];
  2424. lpScriptInfo = (LPSCRIPTINFO) GetWindowLongPtr (hDlg, DWLP_USER);
  2425. if (!lpScriptInfo) {
  2426. break;
  2427. }
  2428. szName[0] = TEXT('\0');
  2429. szArgs[0] = TEXT('\0');
  2430. info.lpScriptInfo = lpScriptInfo;
  2431. info.bEdit = FALSE;
  2432. info.lpName = szName;
  2433. info.lpArgs = szArgs;
  2434. if (DialogBoxParam (g_hInstance, MAKEINTRESOURCE(IDD_SCRIPT_EDIT),
  2435. hDlg, ScriptEditDlgProc, (LPARAM) &info))
  2436. {
  2437. if (lpScriptInfo->pCS->AddScriptToList (GetDlgItem(hDlg, IDC_SCRIPT_LIST),
  2438. szName, szArgs))
  2439. {
  2440. SendMessage (GetParent(hDlg), PSM_CHANGED, (WPARAM) hDlg, 0);
  2441. SetFocus (GetDlgItem(hDlg, IDC_SCRIPT_LIST));
  2442. }
  2443. }
  2444. }
  2445. else if (LOWORD(wParam) == IDC_SCRIPT_EDIT)
  2446. {
  2447. SCRIPTEDITINFO info;
  2448. TCHAR szName[MAX_PATH];
  2449. TCHAR szArgs[2 * MAX_PATH];
  2450. HWND hLV = GetDlgItem(hDlg, IDC_SCRIPT_LIST);
  2451. INT iIndex;
  2452. LPSCRIPTITEM lpItem;
  2453. LVITEM item;
  2454. DWORD dwSize;
  2455. lpScriptInfo = (LPSCRIPTINFO) GetWindowLongPtr (hDlg, DWLP_USER);
  2456. if (!lpScriptInfo) {
  2457. break;
  2458. }
  2459. //
  2460. // Get the selected item
  2461. //
  2462. iIndex = ListView_GetNextItem (hLV, -1, LVNI_ALL | LVNI_SELECTED);
  2463. if (iIndex != -1)
  2464. {
  2465. //
  2466. // Get the script item pointer
  2467. //
  2468. item.mask = LVIF_PARAM;
  2469. item.iItem = iIndex;
  2470. item.iSubItem = 0;
  2471. if (!ListView_GetItem (hLV, &item))
  2472. {
  2473. break;
  2474. }
  2475. lpItem = (LPSCRIPTITEM) item.lParam;
  2476. //
  2477. // Put up the edit script dialog
  2478. //
  2479. lstrcpy (szName, lpItem->lpName);
  2480. lstrcpy (szArgs, lpItem->lpArgs);
  2481. info.lpScriptInfo = lpScriptInfo;
  2482. info.bEdit = TRUE;
  2483. info.lpName = szName;
  2484. info.lpArgs = szArgs;
  2485. if (DialogBoxParam (g_hInstance, MAKEINTRESOURCE(IDD_SCRIPT_EDIT),
  2486. hDlg, ScriptEditDlgProc, (LPARAM) &info))
  2487. {
  2488. //
  2489. // Free old pointer
  2490. //
  2491. LocalFree (lpItem);
  2492. //
  2493. // Setup new pointer
  2494. //
  2495. dwSize = sizeof(SCRIPTITEM);
  2496. dwSize += ((lstrlen(szName) + 1) * sizeof(TCHAR));
  2497. dwSize += ((lstrlen(szArgs) + 1) * sizeof(TCHAR));
  2498. lpItem = (LPSCRIPTITEM) LocalAlloc (LPTR, dwSize);
  2499. if (!lpItem)
  2500. break;
  2501. lpItem->lpName = (LPTSTR) (((LPBYTE)lpItem) + sizeof(SCRIPTITEM));
  2502. lstrcpy (lpItem->lpName, szName);
  2503. lpItem->lpArgs = lpItem->lpName + lstrlen (lpItem->lpName) + 1;
  2504. lstrcpy (lpItem->lpArgs, szArgs);
  2505. //
  2506. // Set the new script item pointer
  2507. //
  2508. item.mask = LVIF_PARAM;
  2509. item.iItem = iIndex;
  2510. item.iSubItem = 0;
  2511. item.lParam = (LPARAM) lpItem;
  2512. if (!ListView_SetItem (hLV, &item))
  2513. {
  2514. break;
  2515. }
  2516. //
  2517. // Update the display
  2518. //
  2519. ListView_Update (hLV, iIndex);
  2520. SendMessage (GetParent(hDlg), PSM_CHANGED, (WPARAM) hDlg, 0);
  2521. SetFocus (GetDlgItem(hDlg, IDC_SCRIPT_LIST));
  2522. }
  2523. }
  2524. }
  2525. else if (LOWORD(wParam) == IDC_SCRIPT_REMOVE)
  2526. {
  2527. INT iIndex, iNext;
  2528. HWND hLV = GetDlgItem(hDlg, IDC_SCRIPT_LIST);
  2529. LPSCRIPTITEM lpItem;
  2530. LVITEM item;
  2531. //
  2532. // Get the selected item
  2533. //
  2534. iIndex = ListView_GetNextItem (hLV, -1, LVNI_ALL | LVNI_SELECTED);
  2535. if (iIndex != -1)
  2536. {
  2537. //
  2538. // Get the script item pointer
  2539. //
  2540. item.mask = LVIF_PARAM;
  2541. item.iItem = iIndex;
  2542. item.iSubItem = 0;
  2543. if (!ListView_GetItem (hLV, &item))
  2544. {
  2545. break;
  2546. }
  2547. lpItem = (LPSCRIPTITEM) item.lParam;
  2548. //
  2549. // Select the next item
  2550. //
  2551. iNext = ListView_GetNextItem (hLV, iIndex, LVNI_ALL);
  2552. item.mask = LVIF_STATE;
  2553. item.iItem = iNext;
  2554. item.iSubItem = 0;
  2555. item.state = LVIS_SELECTED | LVIS_FOCUSED;
  2556. item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
  2557. SendMessage (hLV, LVM_SETITEMSTATE, iNext, (LPARAM) &item);
  2558. ListView_DeleteItem (hLV, iIndex);
  2559. PostMessage (hDlg, WM_REFRESHDISPLAY, 0, 0);
  2560. SendMessage (GetParent(hDlg), PSM_CHANGED, (WPARAM) hDlg, 0);
  2561. SetFocus (hLV);
  2562. }
  2563. }
  2564. else if (LOWORD(wParam) == IDC_SCRIPT_SHOW)
  2565. {
  2566. TCHAR szPath[MAX_PATH];
  2567. LPTSTR lpEnd;
  2568. lpScriptInfo = (LPSCRIPTINFO) GetWindowLongPtr (hDlg, DWLP_USER);
  2569. if (!lpScriptInfo) {
  2570. break;
  2571. }
  2572. lstrcpy (szPath, lpScriptInfo->pCS->m_pcd->m_pScriptsDir);
  2573. lpEnd = CheckSlash (szPath);
  2574. switch (lpScriptInfo->ScriptType)
  2575. {
  2576. case ScriptType_Logon:
  2577. lstrcpy (lpEnd, TEXT("Logon"));
  2578. break;
  2579. case ScriptType_Logoff:
  2580. lstrcpy (lpEnd, TEXT("Logoff"));
  2581. break;
  2582. case ScriptType_Startup:
  2583. lstrcpy (lpEnd, TEXT("Startup"));
  2584. break;
  2585. case ScriptType_Shutdown:
  2586. lstrcpy (lpEnd, TEXT("Shutdown"));
  2587. break;
  2588. }
  2589. SetCursor (LoadCursor(NULL, IDC_WAIT));
  2590. ShellExecute (hDlg, TEXT("open"), szPath,
  2591. NULL, NULL, SW_SHOWNORMAL);
  2592. SetCursor (LoadCursor(NULL, IDC_ARROW));
  2593. }
  2594. break;
  2595. case WM_NOTIFY:
  2596. lpScriptInfo = (LPSCRIPTINFO) GetWindowLongPtr (hDlg, DWLP_USER);
  2597. if (!lpScriptInfo) {
  2598. break;
  2599. }
  2600. switch (((NMHDR FAR*)lParam)->code)
  2601. {
  2602. case LVN_GETDISPINFO:
  2603. {
  2604. NMLVDISPINFO * lpDispInfo = (NMLVDISPINFO *) lParam;
  2605. LPSCRIPTITEM lpItem = (LPSCRIPTITEM)lpDispInfo->item.lParam;
  2606. if (lpDispInfo->item.iSubItem == 0)
  2607. {
  2608. lpDispInfo->item.pszText = lpItem->lpName;
  2609. }
  2610. else
  2611. {
  2612. lpDispInfo->item.pszText = lpItem->lpArgs;
  2613. }
  2614. }
  2615. break;
  2616. case LVN_DELETEITEM:
  2617. {
  2618. NMLISTVIEW * pLVInfo = (NMLISTVIEW *) lParam;
  2619. if (pLVInfo->lParam)
  2620. {
  2621. LocalFree ((LPTSTR)pLVInfo->lParam);
  2622. }
  2623. }
  2624. break;
  2625. case LVN_ITEMCHANGED:
  2626. PostMessage (hDlg, WM_REFRESHDISPLAY, 0, 0);
  2627. break;
  2628. case PSN_APPLY:
  2629. lpScriptInfo->pCS->OnApplyNotify (hDlg);
  2630. // fall through...
  2631. case PSN_RESET:
  2632. SetWindowLongPtr (hDlg, DWLP_MSGRESULT, PSNRET_NOERROR);
  2633. return TRUE;
  2634. }
  2635. break;
  2636. case WM_REFRESHDISPLAY:
  2637. {
  2638. INT iIndex, iCount;
  2639. HWND hLV = GetDlgItem(hDlg, IDC_SCRIPT_LIST);
  2640. lpScriptInfo = (LPSCRIPTINFO) GetWindowLongPtr (hDlg, DWLP_USER);
  2641. if (!lpScriptInfo) {
  2642. break;
  2643. }
  2644. iIndex = ListView_GetNextItem (hLV, -1,
  2645. LVNI_ALL | LVNI_SELECTED);
  2646. if (iIndex != -1)
  2647. {
  2648. EnableWindow (GetDlgItem (hDlg, IDC_SCRIPT_REMOVE), TRUE);
  2649. EnableWindow (GetDlgItem (hDlg, IDC_SCRIPT_EDIT), TRUE);
  2650. iCount = ListView_GetItemCount(hLV);
  2651. if (iIndex > 0)
  2652. EnableWindow (GetDlgItem (hDlg, IDC_SCRIPT_UP), TRUE);
  2653. else
  2654. EnableWindow (GetDlgItem (hDlg, IDC_SCRIPT_UP), FALSE);
  2655. if (iIndex < (iCount - 1))
  2656. EnableWindow (GetDlgItem (hDlg, IDC_SCRIPT_DOWN), TRUE);
  2657. else
  2658. EnableWindow (GetDlgItem (hDlg, IDC_SCRIPT_DOWN), FALSE);
  2659. }
  2660. else
  2661. {
  2662. EnableWindow (GetDlgItem (hDlg, IDC_SCRIPT_REMOVE), FALSE);
  2663. EnableWindow (GetDlgItem (hDlg, IDC_SCRIPT_EDIT), FALSE);
  2664. EnableWindow (GetDlgItem (hDlg, IDC_SCRIPT_UP), FALSE);
  2665. EnableWindow (GetDlgItem (hDlg, IDC_SCRIPT_DOWN), FALSE);
  2666. }
  2667. }
  2668. break;
  2669. case WM_HELP: // F1
  2670. WinHelp((HWND)((LPHELPINFO) lParam)->hItemHandle, HELP_FILE, HELP_WM_HELP,
  2671. (DWORD_PTR) (LPSTR) aScriptsHelpIds);
  2672. break;
  2673. case WM_CONTEXTMENU: // right mouse click
  2674. WinHelp((HWND) wParam, HELP_FILE, HELP_CONTEXTMENU,
  2675. (DWORD_PTR) (LPSTR) aScriptsHelpIds);
  2676. return (TRUE);
  2677. }
  2678. return FALSE;
  2679. }
  2680. BOOL CScriptsSnapIn::AddScriptToList (HWND hLV, LPTSTR lpName, LPTSTR lpArgs)
  2681. {
  2682. LPSCRIPTITEM lpItem;
  2683. LV_ITEM item;
  2684. INT iItem;
  2685. DWORD dwSize;
  2686. dwSize = sizeof(SCRIPTITEM);
  2687. dwSize += ((lstrlen(lpName) + 1) * sizeof(TCHAR));
  2688. dwSize += ((lstrlen(lpArgs) + 1) * sizeof(TCHAR));
  2689. lpItem = (LPSCRIPTITEM) LocalAlloc (LPTR, dwSize);
  2690. if (!lpItem)
  2691. return FALSE;
  2692. lpItem->lpName = (LPTSTR) (((LPBYTE)lpItem) + sizeof(SCRIPTITEM));
  2693. lstrcpy (lpItem->lpName, lpName);
  2694. lpItem->lpArgs = lpItem->lpName + lstrlen (lpItem->lpName) + 1;
  2695. lstrcpy (lpItem->lpArgs, lpArgs);
  2696. //
  2697. // Add the item
  2698. //
  2699. iItem = ListView_GetItemCount(hLV);
  2700. item.mask = LVIF_TEXT | LVIF_PARAM | LVIF_STATE;
  2701. item.iItem = iItem;
  2702. item.iSubItem = 0;
  2703. item.state = LVIS_SELECTED | LVIS_FOCUSED;
  2704. item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
  2705. item.pszText = LPSTR_TEXTCALLBACK;
  2706. item.lParam = (LPARAM) lpItem;
  2707. ListView_InsertItem (hLV, &item);
  2708. return TRUE;
  2709. }
  2710. LPTSTR CScriptsSnapIn::GetSectionNames (LPTSTR lpFileName)
  2711. {
  2712. DWORD dwSize, dwRead;
  2713. LPTSTR lpNames;
  2714. //
  2715. // Read in the section names
  2716. //
  2717. dwSize = 256;
  2718. lpNames = (LPTSTR) LocalAlloc (LPTR, dwSize * sizeof(TCHAR));
  2719. if (!lpNames)
  2720. {
  2721. return NULL;
  2722. }
  2723. do {
  2724. dwRead = GetPrivateProfileSectionNames (lpNames, dwSize, lpFileName);
  2725. if (dwRead != (dwSize - 2))
  2726. {
  2727. break;
  2728. }
  2729. LocalFree (lpNames);
  2730. dwSize *= 2;
  2731. lpNames = (LPTSTR) LocalAlloc (LPTR, dwSize * sizeof(TCHAR));
  2732. if (!lpNames)
  2733. {
  2734. return FALSE;
  2735. }
  2736. } while (TRUE);
  2737. if (dwRead == 0)
  2738. {
  2739. LocalFree (lpNames);
  2740. lpNames = NULL;
  2741. }
  2742. return lpNames;
  2743. }
  2744. BOOL CScriptsSnapIn::OnApplyNotify (HWND hDlg)
  2745. {
  2746. HWND hLV = GetDlgItem (hDlg, IDC_SCRIPT_LIST);
  2747. WIN32_FILE_ATTRIBUTE_DATA fad;
  2748. LVITEM item;
  2749. LPSCRIPTITEM lpItem;
  2750. LPSCRIPTINFO lpScriptInfo;
  2751. INT iIndex = -1;
  2752. TCHAR szSection[30];
  2753. TCHAR szKeyName[30];
  2754. TCHAR szBuffer1[MAX_PATH];
  2755. LPTSTR lpEnd, lpNames;
  2756. BOOL bAdd = TRUE;
  2757. INT i = 0;
  2758. HANDLE hFile;
  2759. DWORD dwWritten;
  2760. GUID guidScriptsExt = { 0x42B5FAAE, 0x6536, 0x11d2, {0xAE, 0x5A, 0x00, 0x00, 0xF8, 0x75, 0x71, 0xE3}};
  2761. GUID guidSnapinMach = CLSID_ScriptSnapInMachine;
  2762. GUID guidSnapinUser = CLSID_ScriptSnapInUser;
  2763. lpScriptInfo = (LPSCRIPTINFO) GetWindowLongPtr (hDlg, DWLP_USER);
  2764. if (!lpScriptInfo) {
  2765. return FALSE;
  2766. }
  2767. //
  2768. // Get the section name
  2769. //
  2770. switch (lpScriptInfo->ScriptType)
  2771. {
  2772. case ScriptType_Logon:
  2773. lstrcpy (szSection, TEXT("Logon"));
  2774. break;
  2775. case ScriptType_Logoff:
  2776. lstrcpy (szSection, TEXT("Logoff"));
  2777. break;
  2778. case ScriptType_Startup:
  2779. lstrcpy (szSection, TEXT("Startup"));
  2780. break;
  2781. case ScriptType_Shutdown:
  2782. lstrcpy (szSection, TEXT("Shutdown"));
  2783. break;
  2784. default:
  2785. return FALSE;
  2786. }
  2787. //
  2788. // Build pathname to scripts ini file
  2789. //
  2790. lstrcpy (szBuffer1, lpScriptInfo->pCS->m_pcd->m_pScriptsDir);
  2791. lstrcat (szBuffer1, TEXT("\\"));
  2792. lstrcat (szBuffer1, SCRIPTS_FILE_NAME);
  2793. //
  2794. // If the scripts.ini file does not exist, then precreate the file
  2795. // using Unicode text so that the WritePrivateProfile* functions
  2796. // preserve the Unicodeness of the file
  2797. //
  2798. if (!GetFileAttributesEx (szBuffer1, GetFileExInfoStandard, &fad))
  2799. {
  2800. if (GetLastError() == ERROR_FILE_NOT_FOUND)
  2801. {
  2802. hFile = CreateFile(szBuffer1, GENERIC_WRITE, 0, NULL,
  2803. CREATE_NEW, FILE_ATTRIBUTE_HIDDEN, NULL);
  2804. if (hFile != INVALID_HANDLE_VALUE)
  2805. {
  2806. WriteFile(hFile, L"\xfeff\r\n", 3 * sizeof(WCHAR), &dwWritten, NULL);
  2807. CloseHandle(hFile);
  2808. }
  2809. }
  2810. }
  2811. //
  2812. // Delete the old information in the section
  2813. //
  2814. if (!WritePrivateProfileSection(szSection, NULL, szBuffer1))
  2815. {
  2816. TCHAR szTitle[50];
  2817. TCHAR szBuffer1[200],szBuffer2[220];
  2818. DebugMsg((DM_WARNING, TEXT("CScriptsSnapIn::OnApplyNotify: Failed to delete previous %s section in ini file with %d"),
  2819. szSection, GetLastError()));
  2820. LoadString (g_hInstance, IDS_SCRIPTS_NAME, szTitle, ARRAYSIZE(szTitle));
  2821. LoadString (g_hInstance, IDS_SAVEFAILED, szBuffer1, ARRAYSIZE(szBuffer1));
  2822. wsprintf (szBuffer2, szBuffer1, GetLastError());
  2823. MessageBox (hDlg, szBuffer2, szTitle, MB_OK | MB_ICONERROR);
  2824. return FALSE;
  2825. }
  2826. //
  2827. // Enumerate through the items
  2828. //
  2829. while ((iIndex = ListView_GetNextItem (hLV, iIndex, LVNI_ALL)) != -1)
  2830. {
  2831. item.mask = LVIF_PARAM;
  2832. item.iItem = iIndex;
  2833. item.iSubItem = 0;
  2834. if (!ListView_GetItem (hLV, &item))
  2835. {
  2836. continue;
  2837. }
  2838. lpItem = (LPSCRIPTITEM) item.lParam;
  2839. _itot (i, szKeyName, 10);
  2840. lpEnd = szKeyName + lstrlen (szKeyName);
  2841. lstrcpy (lpEnd, TEXT("CmdLine"));
  2842. if (!WritePrivateProfileString (szSection, szKeyName, lpItem->lpName, szBuffer1))
  2843. {
  2844. DebugMsg((DM_WARNING, TEXT("CScriptsSnapIn::OnApplyNotify: Failed to save command line in ini file with %d"),
  2845. GetLastError()));
  2846. }
  2847. lstrcpy (lpEnd, TEXT("Parameters"));
  2848. if (!WritePrivateProfileString (szSection, szKeyName, lpItem->lpArgs, szBuffer1))
  2849. {
  2850. if (lpItem->lpArgs && (*lpItem->lpArgs))
  2851. {
  2852. DebugMsg((DM_WARNING, TEXT("CScriptsSnapIn::OnApplyNotify: Failed to save parameters in ini file with %d"),
  2853. GetLastError()));
  2854. }
  2855. }
  2856. i++;
  2857. }
  2858. //
  2859. // If we didn't write any command lines to scripts.ini,
  2860. // then check if our counterpart is also empty. If so,
  2861. // we can remove the scripts extension from the GPO
  2862. //
  2863. if (i == 0)
  2864. {
  2865. BOOL bFound = FALSE;
  2866. lpNames = GetSectionNames (szBuffer1);
  2867. if (lpNames)
  2868. {
  2869. //
  2870. // Reverse the section name we are looking for
  2871. //
  2872. switch (lpScriptInfo->ScriptType)
  2873. {
  2874. case ScriptType_Logon:
  2875. lstrcpy (szSection, TEXT("Logoff"));
  2876. break;
  2877. case ScriptType_Logoff:
  2878. lstrcpy (szSection, TEXT("Logon"));
  2879. break;
  2880. case ScriptType_Startup:
  2881. lstrcpy (szSection, TEXT("Shutdown"));
  2882. break;
  2883. case ScriptType_Shutdown:
  2884. lstrcpy (szSection, TEXT("Startup"));
  2885. break;
  2886. default:
  2887. return FALSE;
  2888. }
  2889. //
  2890. // See if the opposite name is in the list of names returned
  2891. //
  2892. lpEnd = lpNames;
  2893. while (*lpEnd)
  2894. {
  2895. if (!lstrcmpi (lpEnd, szSection))
  2896. {
  2897. bFound = TRUE;
  2898. break;
  2899. }
  2900. lpEnd = lpEnd + lstrlen (lpEnd);
  2901. }
  2902. if (!bFound)
  2903. {
  2904. bAdd = FALSE;
  2905. }
  2906. LocalFree (lpNames);
  2907. }
  2908. else
  2909. {
  2910. bAdd = FALSE;
  2911. }
  2912. }
  2913. SetFileAttributes (szBuffer1, FILE_ATTRIBUTE_HIDDEN);
  2914. m_pcd->m_pGPTInformation->PolicyChanged( !m_pcd->m_bUserScope,
  2915. bAdd,
  2916. &guidScriptsExt,
  2917. m_pcd->m_bUserScope ? &guidSnapinUser
  2918. : &guidSnapinMach);
  2919. return TRUE;
  2920. }
  2921. INT_PTR CALLBACK CScriptsSnapIn::ScriptEditDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  2922. {
  2923. LPSCRIPTEDITINFO lpInfo;
  2924. HRESULT hr;
  2925. switch (message)
  2926. {
  2927. case WM_INITDIALOG:
  2928. {
  2929. //
  2930. // Save the ScriptEditInfo pointer for future use
  2931. //
  2932. lpInfo = (LPSCRIPTEDITINFO) lParam;
  2933. SetWindowLongPtr (hDlg, DWLP_USER, (LONG_PTR) lpInfo);
  2934. EnableWindow (GetDlgItem(hDlg, IDOK), FALSE);
  2935. if (lpInfo->bEdit)
  2936. {
  2937. TCHAR szTitle[100];
  2938. LoadString (g_hInstance, IDS_SCRIPT_EDIT, szTitle, ARRAYSIZE(szTitle));
  2939. SetWindowText (hDlg, szTitle);
  2940. SetDlgItemText (hDlg, IDC_SCRIPT_NAME, lpInfo->lpName);
  2941. SetDlgItemText (hDlg, IDC_SCRIPT_ARGS, lpInfo->lpArgs);
  2942. }
  2943. break;
  2944. }
  2945. case WM_COMMAND:
  2946. if (LOWORD(wParam) == IDOK)
  2947. {
  2948. lpInfo = (LPSCRIPTEDITINFO) GetWindowLongPtr (hDlg, DWLP_USER);
  2949. if (!lpInfo) {
  2950. break;
  2951. }
  2952. lpInfo->lpName[0] = TEXT('\0');
  2953. GetDlgItemText (hDlg, IDC_SCRIPT_NAME, lpInfo->lpName, MAX_PATH);
  2954. lpInfo->lpArgs[0] = TEXT('\0');
  2955. GetDlgItemText (hDlg, IDC_SCRIPT_ARGS, lpInfo->lpArgs, 2 * MAX_PATH);
  2956. EndDialog (hDlg, TRUE);
  2957. return TRUE;
  2958. }
  2959. else if (LOWORD(wParam) == IDCANCEL)
  2960. {
  2961. EndDialog (hDlg, FALSE);
  2962. return TRUE;
  2963. }
  2964. else if (LOWORD(wParam) == IDC_SCRIPT_BROWSE)
  2965. {
  2966. OPENFILENAME ofn;
  2967. TCHAR szFilter[100];
  2968. TCHAR szTitle[100];
  2969. TCHAR szFile[MAX_PATH];
  2970. TCHAR szPath[MAX_PATH];
  2971. LPTSTR lpTemp, lpEnd;
  2972. DWORD dwStrLen;
  2973. lpInfo = (LPSCRIPTEDITINFO) GetWindowLongPtr (hDlg, DWLP_USER);
  2974. if (!lpInfo) {
  2975. break;
  2976. }
  2977. lstrcpy (szPath, lpInfo->lpScriptInfo->pCS->m_pcd->m_pScriptsDir);
  2978. lpEnd = CheckSlash (szPath);
  2979. switch (lpInfo->lpScriptInfo->ScriptType)
  2980. {
  2981. case ScriptType_Logon:
  2982. lstrcpy (lpEnd, TEXT("Logon"));
  2983. break;
  2984. case ScriptType_Logoff:
  2985. lstrcpy (lpEnd, TEXT("Logoff"));
  2986. break;
  2987. case ScriptType_Startup:
  2988. lstrcpy (lpEnd, TEXT("Startup"));
  2989. break;
  2990. case ScriptType_Shutdown:
  2991. lstrcpy (lpEnd, TEXT("Shutdown"));
  2992. break;
  2993. }
  2994. //
  2995. // Prompt for the script file
  2996. //
  2997. LoadString (g_hInstance, IDS_SCRIPT_FILTER, szFilter, ARRAYSIZE(szFilter));
  2998. LoadString (g_hInstance, IDS_BROWSE, szTitle, ARRAYSIZE(szTitle));
  2999. lpTemp = szFilter;
  3000. while (*lpTemp)
  3001. {
  3002. if (*lpTemp == TEXT('#'))
  3003. *lpTemp = TEXT('\0');
  3004. lpTemp++;
  3005. }
  3006. ZeroMemory (&ofn, sizeof(ofn));
  3007. szFile[0] = TEXT('\0');
  3008. ofn.lStructSize = sizeof(ofn);
  3009. ofn.hwndOwner = hDlg;
  3010. ofn.hInstance = g_hInstance;
  3011. ofn.lpstrFilter = szFilter;
  3012. ofn.nFilterIndex = 2;
  3013. ofn.lpstrFile = szFile;
  3014. ofn.nMaxFile = ARRAYSIZE(szFile);
  3015. ofn.lpstrInitialDir = szPath;
  3016. ofn.lpstrTitle = szTitle;
  3017. ofn.Flags = OFN_NOCHANGEDIR | OFN_HIDEREADONLY | OFN_FILEMUSTEXIST | OFN_EXPLORER;
  3018. if (!GetOpenFileName (&ofn))
  3019. {
  3020. return FALSE;
  3021. }
  3022. dwStrLen = lstrlen (szPath);
  3023. if (CompareString (LOCALE_USER_DEFAULT, NORM_IGNORECASE | NORM_STOP_ON_NULL,
  3024. szPath, dwStrLen, szFile, dwStrLen) == 2)
  3025. SetDlgItemText (hDlg, IDC_SCRIPT_NAME, (szFile + dwStrLen + 1));
  3026. else
  3027. SetDlgItemText (hDlg, IDC_SCRIPT_NAME, szFile);
  3028. }
  3029. else if (LOWORD(wParam) == IDC_SCRIPT_NAME)
  3030. {
  3031. if (HIWORD(wParam) == EN_UPDATE)
  3032. {
  3033. if (GetWindowTextLength (GetDlgItem(hDlg, IDC_SCRIPT_NAME)))
  3034. EnableWindow (GetDlgItem(hDlg, IDOK), TRUE);
  3035. else
  3036. EnableWindow (GetDlgItem(hDlg, IDOK), FALSE);
  3037. }
  3038. }
  3039. break;
  3040. case WM_HELP: // F1
  3041. WinHelp((HWND)((LPHELPINFO) lParam)->hItemHandle, HELP_FILE, HELP_WM_HELP,
  3042. (DWORD_PTR) (LPSTR) aScriptsEditHelpIds);
  3043. break;
  3044. case WM_CONTEXTMENU: // right mouse click
  3045. WinHelp((HWND) wParam, HELP_FILE, HELP_CONTEXTMENU,
  3046. (DWORD_PTR) (LPSTR) aScriptsEditHelpIds);
  3047. return (TRUE);
  3048. }
  3049. return FALSE;
  3050. }
  3051. unsigned int CScriptsDataObject::m_cfNodeType = RegisterClipboardFormat(CCF_NODETYPE);
  3052. unsigned int CScriptsDataObject::m_cfNodeTypeString = RegisterClipboardFormat(CCF_SZNODETYPE);
  3053. unsigned int CScriptsDataObject::m_cfDisplayName = RegisterClipboardFormat(CCF_DISPLAY_NAME);
  3054. unsigned int CScriptsDataObject::m_cfCoClass = RegisterClipboardFormat(CCF_SNAPIN_CLASSID);
  3055. unsigned int CScriptsDataObject::m_cfDescription = RegisterClipboardFormat(L"CCF_DESCRIPTION");
  3056. unsigned int CScriptsDataObject::m_cfHTMLDetails = RegisterClipboardFormat(L"CCF_HTML_DETAILS");
  3057. ///////////////////////////////////////////////////////////////////////////////
  3058. // //
  3059. // CScriptsDataObject implementation //
  3060. // //
  3061. ///////////////////////////////////////////////////////////////////////////////
  3062. CScriptsDataObject::CScriptsDataObject(CScriptsComponentData *pComponent)
  3063. {
  3064. m_cRef = 1;
  3065. InterlockedIncrement(&g_cRefThisDll);
  3066. m_pcd = pComponent;
  3067. m_pcd->AddRef();
  3068. m_type = CCT_UNINITIALIZED;
  3069. m_cookie = -1;
  3070. }
  3071. CScriptsDataObject::~CScriptsDataObject()
  3072. {
  3073. m_pcd->Release();
  3074. InterlockedDecrement(&g_cRefThisDll);
  3075. }
  3076. ///////////////////////////////////////////////////////////////////////////////
  3077. // //
  3078. // CScriptsDataObject object implementation (IUnknown) //
  3079. // //
  3080. ///////////////////////////////////////////////////////////////////////////////
  3081. HRESULT CScriptsDataObject::QueryInterface (REFIID riid, void **ppv)
  3082. {
  3083. if (IsEqualIID(riid, IID_IScriptDataObject))
  3084. {
  3085. *ppv = (LPSCRIPTDATAOBJECT)this;
  3086. m_cRef++;
  3087. return S_OK;
  3088. }
  3089. else if (IsEqualIID(riid, IID_IDataObject) ||
  3090. IsEqualIID(riid, IID_IUnknown))
  3091. {
  3092. *ppv = (LPDATAOBJECT)this;
  3093. m_cRef++;
  3094. return S_OK;
  3095. }
  3096. else
  3097. {
  3098. *ppv = NULL;
  3099. return E_NOINTERFACE;
  3100. }
  3101. }
  3102. ULONG CScriptsDataObject::AddRef (void)
  3103. {
  3104. return ++m_cRef;
  3105. }
  3106. ULONG CScriptsDataObject::Release (void)
  3107. {
  3108. if (--m_cRef == 0) {
  3109. delete this;
  3110. return 0;
  3111. }
  3112. return m_cRef;
  3113. }
  3114. ///////////////////////////////////////////////////////////////////////////////
  3115. // //
  3116. // CScriptsDataObject object implementation (IDataObject) //
  3117. // //
  3118. ///////////////////////////////////////////////////////////////////////////////
  3119. STDMETHODIMP CScriptsDataObject::GetDataHere(LPFORMATETC lpFormatetc, LPSTGMEDIUM lpMedium)
  3120. {
  3121. HRESULT hr = DV_E_CLIPFORMAT;
  3122. // Based on the CLIPFORMAT write data to the stream
  3123. const CLIPFORMAT cf = lpFormatetc->cfFormat;
  3124. if(cf == m_cfNodeType)
  3125. {
  3126. hr = CreateNodeTypeData(lpMedium);
  3127. }
  3128. else if(cf == m_cfNodeTypeString)
  3129. {
  3130. hr = CreateNodeTypeStringData(lpMedium);
  3131. }
  3132. else if (cf == m_cfDisplayName)
  3133. {
  3134. hr = CreateDisplayName(lpMedium);
  3135. }
  3136. else if (cf == m_cfCoClass)
  3137. {
  3138. hr = CreateCoClassID(lpMedium);
  3139. }
  3140. else if (cf == m_cfDescription)
  3141. {
  3142. hr = DV_E_TYMED;
  3143. if (lpMedium->tymed == TYMED_ISTREAM)
  3144. {
  3145. ULONG ulWritten;
  3146. TCHAR szDesc[200];
  3147. if (m_type == CCT_SCOPE)
  3148. {
  3149. LoadString (g_hInstance, m_pcd->m_pNameSpaceItems[m_cookie].iDescStringID, szDesc, ARRAYSIZE(szDesc));
  3150. }
  3151. else
  3152. {
  3153. LPSCRIPTRESULTITEM lpScriptItem = (LPSCRIPTRESULTITEM) m_cookie;
  3154. LoadString (g_hInstance, lpScriptItem->iDescStringID, szDesc, ARRAYSIZE(szDesc));
  3155. }
  3156. IStream *lpStream = lpMedium->pstm;
  3157. if(lpStream)
  3158. {
  3159. hr = lpStream->Write(szDesc, lstrlen(szDesc) * sizeof(TCHAR), &ulWritten);
  3160. }
  3161. }
  3162. }
  3163. else if (cf == m_cfHTMLDetails)
  3164. {
  3165. hr = DV_E_TYMED;
  3166. if ((m_type == CCT_RESULT) && !m_pcd->m_bRSOP)
  3167. {
  3168. if (lpMedium->tymed == TYMED_ISTREAM)
  3169. {
  3170. ULONG ulWritten;
  3171. IStream *lpStream = lpMedium->pstm;
  3172. if(lpStream)
  3173. {
  3174. hr = lpStream->Write(g_szDisplayProperties, lstrlen(g_szDisplayProperties) * sizeof(TCHAR), &ulWritten);
  3175. }
  3176. }
  3177. }
  3178. }
  3179. return hr;
  3180. return hr;
  3181. }
  3182. ///////////////////////////////////////////////////////////////////////////////
  3183. // //
  3184. // CScriptsDataObject object implementation (Internal functions) //
  3185. // //
  3186. ///////////////////////////////////////////////////////////////////////////////
  3187. HRESULT CScriptsDataObject::Create(LPVOID pBuffer, INT len, LPSTGMEDIUM lpMedium)
  3188. {
  3189. HRESULT hr = DV_E_TYMED;
  3190. // Do some simple validation
  3191. if (pBuffer == NULL || lpMedium == NULL)
  3192. return E_POINTER;
  3193. // Make sure the type medium is HGLOBAL
  3194. if (lpMedium->tymed == TYMED_HGLOBAL)
  3195. {
  3196. // Create the stream on the hGlobal passed in
  3197. LPSTREAM lpStream;
  3198. hr = CreateStreamOnHGlobal(lpMedium->hGlobal, FALSE, &lpStream);
  3199. if (SUCCEEDED(hr))
  3200. {
  3201. // Write to the stream the number of bytes
  3202. unsigned long written;
  3203. hr = lpStream->Write(pBuffer, len, &written);
  3204. // Because we told CreateStreamOnHGlobal with 'FALSE',
  3205. // only the stream is released here.
  3206. // Note - the caller (i.e. snap-in, object) will free the HGLOBAL
  3207. // at the correct time. This is according to the IDataObject specification.
  3208. lpStream->Release();
  3209. }
  3210. }
  3211. return hr;
  3212. }
  3213. HRESULT CScriptsDataObject::CreateNodeTypeData(LPSTGMEDIUM lpMedium)
  3214. {
  3215. const GUID * pGUID;
  3216. LPSCRIPTRESULTITEM lpItem = (LPSCRIPTRESULTITEM) m_cookie;
  3217. if (m_cookie == -1)
  3218. return E_UNEXPECTED;
  3219. if (m_type == CCT_RESULT)
  3220. pGUID = lpItem->pNodeID;
  3221. else
  3222. pGUID = m_pcd->m_pNameSpaceItems[m_cookie].pNodeID;
  3223. // Create the node type object in GUID format
  3224. return Create((LPVOID)pGUID, sizeof(GUID), lpMedium);
  3225. }
  3226. HRESULT CScriptsDataObject::CreateNodeTypeStringData(LPSTGMEDIUM lpMedium)
  3227. {
  3228. const GUID * pGUID;
  3229. LPSCRIPTRESULTITEM lpItem = (LPSCRIPTRESULTITEM) m_cookie;
  3230. TCHAR szNodeType[50];
  3231. if (m_cookie == -1)
  3232. return E_UNEXPECTED;
  3233. if (m_type == CCT_RESULT)
  3234. pGUID = lpItem->pNodeID;
  3235. else
  3236. pGUID = m_pcd->m_pNameSpaceItems[m_cookie].pNodeID;
  3237. szNodeType[0] = TEXT('\0');
  3238. StringFromGUID2 (*pGUID, szNodeType, 50);
  3239. // Create the node type object in GUID string format
  3240. return Create((LPVOID)szNodeType, ((lstrlenW(szNodeType)+1) * sizeof(WCHAR)), lpMedium);
  3241. }
  3242. HRESULT CScriptsDataObject::CreateDisplayName(LPSTGMEDIUM lpMedium)
  3243. {
  3244. WCHAR szDisplayName[100] = {0};
  3245. if (m_pcd->m_bUserScope)
  3246. LoadStringW (g_hInstance, IDS_SCRIPTS_NAME_USER, szDisplayName, 100);
  3247. else
  3248. LoadStringW (g_hInstance, IDS_SCRIPTS_NAME_MACHINE, szDisplayName, 100);
  3249. return Create((LPVOID)szDisplayName, (lstrlenW(szDisplayName) + 1) * sizeof(WCHAR), lpMedium);
  3250. }
  3251. HRESULT CScriptsDataObject::CreateCoClassID(LPSTGMEDIUM lpMedium)
  3252. {
  3253. // Create the CoClass information
  3254. if (m_pcd->m_bUserScope)
  3255. return Create((LPVOID)&CLSID_ScriptSnapInUser, sizeof(CLSID), lpMedium);
  3256. else
  3257. return Create((LPVOID)&CLSID_ScriptSnapInMachine, sizeof(CLSID), lpMedium);
  3258. }
  3259. BOOL InitScriptsNameSpace()
  3260. {
  3261. DWORD dwIndex;
  3262. for (dwIndex = 1; dwIndex < ARRAYSIZE(g_GPEScriptsNameSpace); dwIndex++)
  3263. {
  3264. LoadString (g_hInstance, g_GPEScriptsNameSpace[dwIndex].iStringID,
  3265. g_GPEScriptsNameSpace[dwIndex].szDisplayName,
  3266. MAX_DISPLAYNAME_SIZE);
  3267. }
  3268. for (dwIndex = 1; dwIndex < ARRAYSIZE(g_RSOPScriptsNameSpace); dwIndex++)
  3269. {
  3270. LoadString (g_hInstance, g_RSOPScriptsNameSpace[dwIndex].iStringID,
  3271. g_RSOPScriptsNameSpace[dwIndex].szDisplayName,
  3272. MAX_DISPLAYNAME_SIZE);
  3273. }
  3274. return TRUE;
  3275. }
  3276. const TCHAR szThreadingModel[] = TEXT("Apartment");
  3277. HRESULT RegisterScriptExtension (REFGUID clsid, UINT uiStringId, REFGUID rootID, LPTSTR lpSnapInNameIndirect)
  3278. {
  3279. TCHAR szSnapInKey[50];
  3280. TCHAR szSubKey[200];
  3281. TCHAR szSnapInName[100];
  3282. TCHAR szGUID[50];
  3283. DWORD dwDisp, dwValue;
  3284. LONG lResult;
  3285. HKEY hKey;
  3286. //
  3287. // First register the extension
  3288. //
  3289. StringFromGUID2 (clsid, szSnapInKey, 50);
  3290. //
  3291. // Register SnapIn in HKEY_CLASSES_ROOT
  3292. //
  3293. LoadString (g_hInstance, uiStringId, szSnapInName, 100);
  3294. wsprintf (szSubKey, TEXT("CLSID\\%s"), szSnapInKey);
  3295. lResult = RegCreateKeyEx (HKEY_CLASSES_ROOT, szSubKey, 0, NULL,
  3296. REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL,
  3297. &hKey, &dwDisp);
  3298. if (lResult != ERROR_SUCCESS) {
  3299. return SELFREG_E_CLASS;
  3300. }
  3301. RegSetValueEx (hKey, NULL, 0, REG_SZ, (LPBYTE)szSnapInName,
  3302. (lstrlen(szSnapInName) + 1) * sizeof(TCHAR));
  3303. RegCloseKey (hKey);
  3304. wsprintf (szSubKey, TEXT("CLSID\\%s\\InProcServer32"), szSnapInKey);
  3305. lResult = RegCreateKeyEx (HKEY_CLASSES_ROOT, szSubKey, 0, NULL,
  3306. REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL,
  3307. &hKey, &dwDisp);
  3308. if (lResult != ERROR_SUCCESS) {
  3309. return SELFREG_E_CLASS;
  3310. }
  3311. RegSetValueEx (hKey, NULL, 0, REG_EXPAND_SZ, (LPBYTE)g_szSnapInLocation,
  3312. (lstrlen(g_szSnapInLocation) + 1) * sizeof(TCHAR));
  3313. RegSetValueEx (hKey, TEXT("ThreadingModel"), 0, REG_SZ, (LPBYTE)szThreadingModel,
  3314. (lstrlen(szThreadingModel) + 1) * sizeof(TCHAR));
  3315. RegCloseKey (hKey);
  3316. //
  3317. // Register SnapIn with MMC
  3318. //
  3319. wsprintf (szSubKey, TEXT("Software\\Microsoft\\MMC\\SnapIns\\%s"), szSnapInKey);
  3320. lResult = RegCreateKeyEx (HKEY_LOCAL_MACHINE, szSubKey, 0, NULL,
  3321. REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL,
  3322. &hKey, &dwDisp);
  3323. if (lResult != ERROR_SUCCESS) {
  3324. return SELFREG_E_CLASS;
  3325. }
  3326. RegSetValueEx (hKey, TEXT("NameString"), 0, REG_SZ, (LPBYTE)szSnapInName,
  3327. (lstrlen(szSnapInName) + 1) * sizeof(TCHAR));
  3328. RegSetValueEx (hKey, TEXT("NameStringIndirect"), 0, REG_SZ, (LPBYTE)lpSnapInNameIndirect,
  3329. (lstrlen(lpSnapInNameIndirect) + 1) * sizeof(TCHAR));
  3330. RegCloseKey (hKey);
  3331. //
  3332. // Register as an extension for various nodes
  3333. //
  3334. StringFromGUID2 (rootID, szGUID, 50);
  3335. wsprintf (szSubKey, TEXT("Software\\Microsoft\\MMC\\NodeTypes\\%s\\Extensions\\NameSpace"), szGUID);
  3336. lResult = RegCreateKeyEx (HKEY_LOCAL_MACHINE, szSubKey, 0, NULL,
  3337. REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL,
  3338. &hKey, &dwDisp);
  3339. if (lResult != ERROR_SUCCESS) {
  3340. return SELFREG_E_CLASS;
  3341. }
  3342. RegSetValueEx (hKey, szSnapInKey, 0, REG_SZ, (LPBYTE)szSnapInName,
  3343. (lstrlen(szSnapInName) + 1) * sizeof(TCHAR));
  3344. RegCloseKey (hKey);
  3345. return S_OK;
  3346. }
  3347. const TCHAR szViewDescript [] = TEXT("MMCViewExt 1.0 Object");
  3348. const TCHAR szViewGUID [] = TEXT("{B708457E-DB61-4C55-A92F-0D4B5E9B1224}");
  3349. HRESULT RegisterNodeID (REFGUID clsid, REFGUID nodeid)
  3350. {
  3351. TCHAR szSnapInKey[50];
  3352. TCHAR szGUID[50];
  3353. TCHAR szSubKey[200];
  3354. DWORD dwDisp;
  3355. LONG lResult;
  3356. HKEY hKey;
  3357. StringFromGUID2 (clsid, szSnapInKey, 50);
  3358. StringFromGUID2 (nodeid, szGUID, 50);
  3359. //
  3360. // Register the node id
  3361. //
  3362. wsprintf (szSubKey, TEXT("Software\\Microsoft\\MMC\\SnapIns\\%s\\NodeTypes\\%s"),
  3363. szSnapInKey, szGUID);
  3364. lResult = RegCreateKeyEx (HKEY_LOCAL_MACHINE, szSubKey, 0, NULL,
  3365. REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL,
  3366. &hKey, &dwDisp);
  3367. if (lResult != ERROR_SUCCESS) {
  3368. return SELFREG_E_CLASS;
  3369. }
  3370. RegCloseKey (hKey);
  3371. //
  3372. // Register in the NodeTypes key
  3373. //
  3374. StringFromGUID2 (nodeid, szGUID, 50);
  3375. wsprintf (szSubKey, TEXT("Software\\Microsoft\\MMC\\NodeTypes\\%s"), szGUID);
  3376. lResult = RegCreateKeyEx (HKEY_LOCAL_MACHINE, szSubKey, 0, NULL,
  3377. REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL,
  3378. &hKey, &dwDisp);
  3379. if (lResult != ERROR_SUCCESS) {
  3380. return SELFREG_E_CLASS;
  3381. }
  3382. RegCloseKey (hKey);
  3383. //
  3384. // Register for the view extension
  3385. //
  3386. lstrcat (szSubKey, TEXT("\\Extensions\\View"));
  3387. lResult = RegCreateKeyEx (HKEY_LOCAL_MACHINE, szSubKey, 0, NULL,
  3388. REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL,
  3389. &hKey, &dwDisp);
  3390. if (lResult != ERROR_SUCCESS) {
  3391. return SELFREG_E_CLASS;
  3392. }
  3393. RegSetValueEx (hKey, szViewGUID, 0, REG_SZ, (LPBYTE)szViewDescript,
  3394. (lstrlen(szViewDescript) + 1) * sizeof(TCHAR));
  3395. RegCloseKey (hKey);
  3396. return S_OK;
  3397. }
  3398. HRESULT RegisterScripts(void)
  3399. {
  3400. DWORD dwDisp, dwValue;
  3401. LONG lResult;
  3402. HKEY hKey;
  3403. HRESULT hr;
  3404. TCHAR szSnapInName[100];
  3405. //
  3406. // Register the GPE machine extension and it's root node
  3407. //
  3408. hr = RegisterScriptExtension (CLSID_ScriptSnapInMachine, IDS_SCRIPTS_NAME_MACHINE,
  3409. NODEID_Machine, TEXT("@gptext.dll,-2"));
  3410. if (hr != S_OK)
  3411. {
  3412. return hr;
  3413. }
  3414. hr = RegisterNodeID (CLSID_ScriptSnapInMachine, NODEID_ScriptRootMachine);
  3415. if (hr != S_OK)
  3416. {
  3417. return hr;
  3418. }
  3419. //
  3420. // Register the GPE user extension and it's root node
  3421. //
  3422. hr = RegisterScriptExtension (CLSID_ScriptSnapInUser, IDS_SCRIPTS_NAME_USER,
  3423. NODEID_User, TEXT("@gptext.dll,-3"));
  3424. if (hr != S_OK)
  3425. {
  3426. return hr;
  3427. }
  3428. hr = RegisterNodeID (CLSID_ScriptSnapInUser, NODEID_ScriptRootUser);
  3429. if (hr != S_OK)
  3430. {
  3431. return hr;
  3432. }
  3433. //
  3434. // Register the RSOP machine extension and it's nodes
  3435. //
  3436. hr = RegisterScriptExtension (CLSID_RSOPScriptSnapInMachine, IDS_SCRIPTS_NAME_MACHINE,
  3437. NODEID_RSOPMachine, TEXT("@gptext.dll,-2"));
  3438. if (hr != S_OK)
  3439. {
  3440. return hr;
  3441. }
  3442. hr = RegisterNodeID (CLSID_RSOPScriptSnapInMachine, NODEID_RSOPScriptRootMachine);
  3443. if (hr != S_OK)
  3444. {
  3445. return hr;
  3446. }
  3447. hr = RegisterNodeID (CLSID_RSOPScriptSnapInMachine, NODEID_RSOPStartup);
  3448. if (hr != S_OK)
  3449. {
  3450. return hr;
  3451. }
  3452. hr = RegisterNodeID (CLSID_RSOPScriptSnapInMachine, NODEID_RSOPShutdown);
  3453. if (hr != S_OK)
  3454. {
  3455. return hr;
  3456. }
  3457. //
  3458. // Register the RSOP user extension and it's nodes
  3459. //
  3460. hr = RegisterScriptExtension (CLSID_RSOPScriptSnapInUser, IDS_SCRIPTS_NAME_USER,
  3461. NODEID_RSOPUser, TEXT("@gptext.dll,-3"));
  3462. if (hr != S_OK)
  3463. {
  3464. return hr;
  3465. }
  3466. hr = RegisterNodeID (CLSID_RSOPScriptSnapInUser, NODEID_RSOPScriptRootUser);
  3467. if (hr != S_OK)
  3468. {
  3469. return hr;
  3470. }
  3471. hr = RegisterNodeID (CLSID_RSOPScriptSnapInUser, NODEID_RSOPLogon);
  3472. if (hr != S_OK)
  3473. {
  3474. return hr;
  3475. }
  3476. hr = RegisterNodeID (CLSID_RSOPScriptSnapInUser, NODEID_RSOPLogoff);
  3477. if (hr != S_OK)
  3478. {
  3479. return hr;
  3480. }
  3481. //
  3482. // Register the client side extension
  3483. //
  3484. lResult = RegCreateKeyEx (HKEY_LOCAL_MACHINE,
  3485. TEXT("Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon\\GPExtensions\\{42B5FAAE-6536-11d2-AE5A-0000F87571E3}"),
  3486. 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE,
  3487. NULL, &hKey, &dwDisp);
  3488. if (lResult != ERROR_SUCCESS)
  3489. {
  3490. return SELFREG_E_CLASS;
  3491. }
  3492. LoadString (g_hInstance, IDS_SCRIPTS_NAME, szSnapInName, 100);
  3493. RegSetValueEx (hKey, NULL, 0, REG_SZ, (LPBYTE)szSnapInName,
  3494. (lstrlen(szSnapInName) + 1) * sizeof(TCHAR));
  3495. RegSetValueEx (hKey, TEXT("ProcessGroupPolicy"), 0, REG_SZ, (LPBYTE)TEXT("ProcessScriptsGroupPolicy"),
  3496. (lstrlen(TEXT("ProcessScriptsGroupPolicy")) + 1) * sizeof(TCHAR));
  3497. RegSetValueEx (hKey, TEXT("ProcessGroupPolicyEx"), 0, REG_SZ, (LPBYTE)TEXT("ProcessScriptsGroupPolicyEx"),
  3498. (lstrlen(TEXT("ProcessScriptsGroupPolicyEx")) + 1) * sizeof(TCHAR));
  3499. RegSetValueEx (hKey, TEXT("GenerateGroupPolicy"), 0, REG_SZ, (LPBYTE)TEXT("GenerateScriptsGroupPolicy"),
  3500. (lstrlen(TEXT("GenerateScriptsGroupPolicy")) + 1) * sizeof(TCHAR));
  3501. RegSetValueEx (hKey, TEXT("DllName"), 0, REG_EXPAND_SZ, (LPBYTE)TEXT("gptext.dll"),
  3502. (lstrlen(TEXT("gptext.dll")) + 1) * sizeof(TCHAR));
  3503. dwValue = 1;
  3504. RegSetValueEx (hKey, TEXT("NoSlowLink"), 0, REG_DWORD, (LPBYTE)&dwValue,
  3505. sizeof(dwValue));
  3506. RegSetValueEx (hKey, TEXT("NoGPOListChanges"), 0, REG_DWORD, (LPBYTE)&dwValue,
  3507. sizeof(dwValue));
  3508. RegSetValueEx (hKey, TEXT("NotifyLinkTransition"), 0, REG_DWORD, (LPBYTE)&dwValue,
  3509. sizeof(dwValue));
  3510. RegCloseKey (hKey);
  3511. return S_OK;
  3512. }
  3513. HRESULT UnregisterScriptExtension (REFGUID clsid, REFGUID RootNodeID)
  3514. {
  3515. TCHAR szSnapInKey[50];
  3516. TCHAR szSubKey[200];
  3517. TCHAR szGUID[50];
  3518. LONG lResult;
  3519. HKEY hKey;
  3520. DWORD dwDisp;
  3521. StringFromGUID2 (clsid, szSnapInKey, 50);
  3522. wsprintf (szSubKey, TEXT("CLSID\\%s"), szSnapInKey);
  3523. RegDelnode (HKEY_CLASSES_ROOT, szSubKey);
  3524. wsprintf (szSubKey, TEXT("Software\\Microsoft\\MMC\\SnapIns\\%s"), szSnapInKey);
  3525. RegDelnode (HKEY_LOCAL_MACHINE, szSubKey);
  3526. StringFromGUID2 (RootNodeID, szGUID, 50);
  3527. wsprintf (szSubKey, TEXT("Software\\Microsoft\\MMC\\NodeTypes\\%s\\Extensions\\NameSpace"), szGUID);
  3528. lResult = RegOpenKeyEx (HKEY_LOCAL_MACHINE, szSubKey, 0,
  3529. KEY_WRITE, &hKey);
  3530. if (lResult == ERROR_SUCCESS) {
  3531. RegDeleteValue (hKey, szSnapInKey);
  3532. RegCloseKey (hKey);
  3533. }
  3534. return S_OK;
  3535. }
  3536. HRESULT UnregisterScripts(void)
  3537. {
  3538. TCHAR szSnapInKey[50];
  3539. TCHAR szSubKey[200];
  3540. TCHAR szGUID[50];
  3541. LONG lResult;
  3542. HKEY hKey;
  3543. DWORD dwDisp;
  3544. //
  3545. // Unregister the GPE machine extension
  3546. //
  3547. UnregisterScriptExtension (CLSID_ScriptSnapInMachine, NODEID_Machine);
  3548. StringFromGUID2 (NODEID_ScriptRootMachine, szGUID, 50);
  3549. wsprintf (szSubKey, TEXT("Software\\Microsoft\\MMC\\NodeTypes\\%s"), szGUID);
  3550. RegDelnode (HKEY_LOCAL_MACHINE, szSubKey);
  3551. //
  3552. // Unregister the GPE user extension
  3553. //
  3554. UnregisterScriptExtension (CLSID_ScriptSnapInUser, NODEID_User);
  3555. StringFromGUID2 (NODEID_ScriptRootUser, szGUID, 50);
  3556. wsprintf (szSubKey, TEXT("Software\\Microsoft\\MMC\\NodeTypes\\%s"), szGUID);
  3557. RegDelnode (HKEY_LOCAL_MACHINE, szSubKey);
  3558. //
  3559. // Unregister the RSOP machine extension
  3560. //
  3561. UnregisterScriptExtension (CLSID_RSOPScriptSnapInMachine, NODEID_RSOPMachine);
  3562. StringFromGUID2 (NODEID_RSOPScriptRootMachine, szGUID, 50);
  3563. wsprintf (szSubKey, TEXT("Software\\Microsoft\\MMC\\NodeTypes\\%s"), szGUID);
  3564. RegDelnode (HKEY_LOCAL_MACHINE, szSubKey);
  3565. StringFromGUID2 (NODEID_RSOPStartup, szGUID, 50);
  3566. wsprintf (szSubKey, TEXT("Software\\Microsoft\\MMC\\NodeTypes\\%s"), szGUID);
  3567. RegDelnode (HKEY_LOCAL_MACHINE, szSubKey);
  3568. StringFromGUID2 (NODEID_RSOPShutdown, szGUID, 50);
  3569. wsprintf (szSubKey, TEXT("Software\\Microsoft\\MMC\\NodeTypes\\%s"), szGUID);
  3570. RegDelnode (HKEY_LOCAL_MACHINE, szSubKey);
  3571. //
  3572. // Unregister the RSOP user extension
  3573. //
  3574. UnregisterScriptExtension (CLSID_RSOPScriptSnapInUser, NODEID_RSOPUser);
  3575. StringFromGUID2 (NODEID_RSOPScriptRootUser, szGUID, 50);
  3576. wsprintf (szSubKey, TEXT("Software\\Microsoft\\MMC\\NodeTypes\\%s"), szGUID);
  3577. RegDelnode (HKEY_LOCAL_MACHINE, szSubKey);
  3578. StringFromGUID2 (NODEID_RSOPLogon, szGUID, 50);
  3579. wsprintf (szSubKey, TEXT("Software\\Microsoft\\MMC\\NodeTypes\\%s"), szGUID);
  3580. RegDelnode (HKEY_LOCAL_MACHINE, szSubKey);
  3581. StringFromGUID2 (NODEID_RSOPLogoff, szGUID, 50);
  3582. wsprintf (szSubKey, TEXT("Software\\Microsoft\\MMC\\NodeTypes\\%s"), szGUID);
  3583. RegDelnode (HKEY_LOCAL_MACHINE, szSubKey);
  3584. //
  3585. // Unregister the client side extension
  3586. //
  3587. RegDeleteKey (HKEY_LOCAL_MACHINE, TEXT("Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon\\GPExtensions\\{42B5FAAE-6536-11d2-AE5A-0000F87571E3}"));
  3588. return S_OK;
  3589. }
  3590. //=============================================================================
  3591. //
  3592. // This is the client side extension for scripts which gathers the
  3593. // working directories and stores them in the registry.
  3594. //
  3595. DWORD AddPathToList(LPTSTR *lpDirs, LPTSTR lpPath)
  3596. {
  3597. LPTSTR lpTemp, lpTemp2;
  3598. DWORD dwSize, dwResult = ERROR_SUCCESS;
  3599. DebugMsg((DM_VERBOSE, TEXT("AddPathToList: Adding <%s> to list."), lpPath));
  3600. lpTemp = *lpDirs;
  3601. if (lpTemp)
  3602. {
  3603. dwSize = lstrlen (lpTemp); // size of original paths
  3604. dwSize++; // space for a semicolon
  3605. dwSize += lstrlen (lpPath); // size of new path
  3606. dwSize++; // space for a null terminator
  3607. lpTemp2 = (LPTSTR) LocalReAlloc (lpTemp, (dwSize * sizeof(TCHAR)), LMEM_MOVEABLE | LMEM_ZEROINIT);
  3608. if (lpTemp2)
  3609. {
  3610. lstrcat (lpTemp2, TEXT(";"));
  3611. lstrcat (lpTemp2, lpPath);
  3612. *lpDirs = lpTemp2;
  3613. }
  3614. else
  3615. {
  3616. dwResult = GetLastError();
  3617. DebugMsg((DM_WARNING, TEXT("AddPathToList: Failed to allocate memory with %d."), dwResult));
  3618. }
  3619. }
  3620. else
  3621. {
  3622. lpTemp = (LPTSTR)LocalAlloc (LPTR, (lstrlen(lpPath) + 1) * sizeof(TCHAR));
  3623. if (lpTemp)
  3624. {
  3625. lstrcpy (lpTemp, lpPath);
  3626. }
  3627. else
  3628. {
  3629. dwResult = GetLastError();
  3630. DebugMsg((DM_WARNING, TEXT("AddPathToList: Failed to allocate memory with %d."), dwResult));
  3631. }
  3632. *lpDirs = lpTemp;
  3633. }
  3634. return dwResult;
  3635. }
  3636. DWORD
  3637. ProcessScripts( DWORD dwFlags,
  3638. HANDLE hToken,
  3639. HKEY hKeyRoot,
  3640. PGROUP_POLICY_OBJECT pDeletedGPOList,
  3641. PGROUP_POLICY_OBJECT pChangedGPOList,
  3642. BOOL *pbAbort,
  3643. BOOL bRSoPPlanningMode,
  3644. IWbemServices* pWbemServices,
  3645. HRESULT* phrRsopStatus );
  3646. DWORD ProcessScriptsGroupPolicy (DWORD dwFlags, HANDLE hToken, HKEY hKeyRoot,
  3647. PGROUP_POLICY_OBJECT pDeletedGPOList,
  3648. PGROUP_POLICY_OBJECT pChangedGPOList,
  3649. ASYNCCOMPLETIONHANDLE pHandle, BOOL *pbAbort,
  3650. PFNSTATUSMESSAGECALLBACK pStatusCallback)
  3651. {
  3652. HRESULT hrRSoPStatus = 0;
  3653. return ProcessScripts( dwFlags,
  3654. hToken,
  3655. hKeyRoot,
  3656. pDeletedGPOList,
  3657. pChangedGPOList,
  3658. pbAbort,
  3659. FALSE,
  3660. 0,
  3661. &hrRSoPStatus );
  3662. }
  3663. DWORD ProcessScriptsGroupPolicyEx( DWORD dwFlags,
  3664. HANDLE hToken,
  3665. HKEY hKeyRoot,
  3666. PGROUP_POLICY_OBJECT pDeletedGPOList,
  3667. PGROUP_POLICY_OBJECT pChangedGPOList,
  3668. ASYNCCOMPLETIONHANDLE pHandle,
  3669. BOOL* pbAbort,
  3670. PFNSTATUSMESSAGECALLBACK pStatusCallback,
  3671. IWbemServices* pWbemServices,
  3672. HRESULT* phrRsopStatus )
  3673. {
  3674. *phrRsopStatus = S_OK;
  3675. return ProcessScripts( dwFlags,
  3676. hToken,
  3677. hKeyRoot,
  3678. pDeletedGPOList,
  3679. pChangedGPOList,
  3680. pbAbort,
  3681. FALSE,
  3682. pWbemServices,
  3683. phrRsopStatus );
  3684. }
  3685. DWORD GenerateScriptsGroupPolicy( DWORD dwFlags,
  3686. BOOL *pbAbort,
  3687. WCHAR *pwszSite,
  3688. PRSOP_TARGET pMachTarget,
  3689. PRSOP_TARGET pUserTarget )
  3690. {
  3691. DWORD dwResult = ERROR_SUCCESS;
  3692. HRESULT hrRSoPStatus = 0;
  3693. if ( pMachTarget )
  3694. {
  3695. //
  3696. // log machine scripts
  3697. //
  3698. dwResult = ProcessScripts( dwFlags | GPO_INFO_FLAG_MACHINE,
  3699. (HANDLE)pMachTarget->pRsopToken,
  3700. 0,
  3701. 0,
  3702. pMachTarget->pGPOList,
  3703. pbAbort,
  3704. TRUE,
  3705. pMachTarget->pWbemServices,
  3706. &hrRSoPStatus );
  3707. if ( dwResult != ERROR_SUCCESS )
  3708. {
  3709. DebugMsg((DM_VERBOSE, L"GenerateScriptPolicy: could not log machine scripts, error %d", dwResult));
  3710. }
  3711. }
  3712. if ( pUserTarget )
  3713. {
  3714. //
  3715. // log user scripts
  3716. //
  3717. dwResult = ProcessScripts( dwFlags & ~GPO_INFO_FLAG_MACHINE,
  3718. (HANDLE)pUserTarget->pRsopToken,
  3719. 0,
  3720. 0,
  3721. pUserTarget->pGPOList,
  3722. pbAbort,
  3723. TRUE,
  3724. pUserTarget->pWbemServices,
  3725. &hrRSoPStatus );
  3726. if ( dwResult != ERROR_SUCCESS )
  3727. {
  3728. DebugMsg((DM_VERBOSE, L"GenerateScriptPolicy: could not log user scripts, error %d", dwResult ));
  3729. }
  3730. }
  3731. return dwResult;
  3732. }