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

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