Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

817 lines
18 KiB

  1. //-------------------------------------------------------------------
  2. //
  3. // FILE: CLiCLicReg.Cpp
  4. //
  5. // Summary;
  6. // Class implementation for handling the licensing api registration
  7. //
  8. // Notes;
  9. // Key = \HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\LicenseInfo
  10. // Value= ErrorControl : REG_DWORD : 0x1
  11. // Value= Start : REG_DWORD : 0x3
  12. // Value= Type : REG_DWORD : 0x4
  13. //
  14. // Subkeys :
  15. // \SNA
  16. // \SQL
  17. // \FilePrint
  18. //
  19. // Value for All Subkeys=
  20. // Mode : REG_DWORD : (0x0 = Per Seat Mode, 0x1 = Concurrent/Per Server Mode)
  21. // ConcurrentLimit : REG_DWORD : (0x<limit>, ie. 0x100 = 256 concurrent user limit)
  22. // FamilyDisplayName: RED_SZ : Name for this service (not version specific)
  23. // DisplayName : REG_SZ : User seen name for this Service entry
  24. // FlipAllow : REG_DWORD : (0x0 = can change license mode, 0x1 license mode can't
  25. // be changed. Server apps are only allowed to switch their license mode
  26. // once, so after the first switch, this value would be set to non-zero,
  27. // then the UI will not allow for further changes to the licence mode.
  28. // Changing is currently allowed but a dialog is raised to warn them of the
  29. // possible violation.
  30. //
  31. // History
  32. // 11/15/94 MikeMi Created
  33. //
  34. //-------------------------------------------------------------------
  35. #include <windows.h>
  36. #include "CLicReg.hpp"
  37. // Strings for keys and values
  38. //
  39. const WCHAR szLicenseKey[] = L"SYSTEM\\CurrentControlSet\\Services\\LicenseInfo";
  40. const WCHAR szErrControlValue[] = L"ErrorControl";
  41. const WCHAR szStartValue[] = L"Start";
  42. const WCHAR szTypeValue[] = L"Type";
  43. const WCHAR szNameValue[] = L"DisplayName";
  44. const WCHAR szFamilyNameValue[] = L"FamilyDisplayName";
  45. const WCHAR szModeValue[] = L"Mode";
  46. const WCHAR szLimitValue[] = L"ConcurrentLimit";
  47. const WCHAR szFlipValue[] = L"FlipAllow";
  48. // set values under License Key
  49. //
  50. const DWORD dwErrControlValue = SERVICE_ERROR_NORMAL; // 1;
  51. const DWORD dwStartValue = SERVICE_DEMAND_START; // 3;
  52. const DWORD dwTypeValue = SERVICE_ADAPTER; // 4;
  53. //-------------------------------------------------------------------
  54. //
  55. // Method: CLicReg::CLicReg
  56. //
  57. // Summary;
  58. // Contructor
  59. //
  60. // History;
  61. // Nov-15-94 MikeMi Created
  62. //
  63. //-------------------------------------------------------------------
  64. CLicReg::CLicReg( )
  65. {
  66. _hkey = NULL;
  67. }
  68. //-------------------------------------------------------------------
  69. //
  70. // Method: CLicReg::~CLicReg
  71. //
  72. // Summary;
  73. // Destructor
  74. //
  75. // History;
  76. // Nov-15-94 MikeMi Created
  77. //
  78. //-------------------------------------------------------------------
  79. CLicReg::~CLicReg( )
  80. {
  81. Close();
  82. }
  83. //-------------------------------------------------------------------
  84. //
  85. // Method: CLicReg::CommitNow
  86. //
  87. // Summary;
  88. // This will flush the changes made imediately
  89. //
  90. // Return:
  91. // ERROR_SUCCESS when this method works.
  92. // See RegFlushKey for return values
  93. //
  94. // Notes:
  95. //
  96. // History;
  97. // Nov-15-94 MikeMi Created
  98. //
  99. //-------------------------------------------------------------------
  100. LONG
  101. CLicReg::CommitNow()
  102. {
  103. return( RegFlushKey( _hkey ) );
  104. }
  105. //-------------------------------------------------------------------
  106. //
  107. // Method: CLicReg::Close
  108. //
  109. // Summary;
  110. // This will close the registry. See Open.
  111. //
  112. // Return:
  113. // ERROR_SUCCESS when this method works.
  114. // See RegCloseKey for return values
  115. //
  116. // Notes:
  117. //
  118. // History;
  119. // Nov-15-94 MikeMi Created
  120. //
  121. //-------------------------------------------------------------------
  122. LONG
  123. CLicReg::Close()
  124. {
  125. LONG lrt = ERROR_SUCCESS;
  126. if ( _hkey )
  127. {
  128. lrt = ::RegCloseKey( _hkey );
  129. _hkey = NULL;
  130. }
  131. return( lrt );
  132. }
  133. //-------------------------------------------------------------------
  134. //
  135. // Method: CLicRegLicense::Open
  136. //
  137. // Summary;
  138. // This will open the registry for License Services Enumeration.
  139. //
  140. // Arguments;
  141. // fNew [out] - Was the opened reg key new.
  142. // pszComputer [in] - the computer name to open the registry on
  143. // this value maybe null (default), this means local machine
  144. // should be of the form \\name
  145. //
  146. // Return:
  147. // ERROR_SUCCESS when this method works.
  148. // See RegCreateKeyEx & RegSetValueEx for error returns.
  149. //
  150. // Notes:
  151. //
  152. // History;
  153. // Nov-15-94 MikeMi Created
  154. //
  155. //-------------------------------------------------------------------
  156. LONG
  157. CLicRegLicense::Open( BOOL& fNew, LPCWSTR pszComputer )
  158. {
  159. DWORD dwDisposition;
  160. LONG lrt;
  161. HKEY hkeyRemote = NULL;
  162. lrt = RegConnectRegistry( (LPTSTR)pszComputer,
  163. HKEY_LOCAL_MACHINE,
  164. &hkeyRemote );
  165. if (ERROR_SUCCESS == lrt)
  166. {
  167. fNew = FALSE;
  168. lrt = ::RegCreateKeyEx( hkeyRemote,
  169. szLicenseKey,
  170. 0,
  171. NULL,
  172. REG_OPTION_NON_VOLATILE,
  173. KEY_ALL_ACCESS,
  174. NULL,
  175. &_hkey,
  176. &dwDisposition );
  177. if ((ERROR_SUCCESS == lrt) &&
  178. (REG_CREATED_NEW_KEY == dwDisposition) )
  179. {
  180. fNew = TRUE;
  181. // Set normal values
  182. //
  183. lrt = ::RegSetValueEx( _hkey,
  184. szErrControlValue,
  185. 0,
  186. REG_DWORD,
  187. (PBYTE)&dwErrControlValue,
  188. sizeof( DWORD ) );
  189. if (ERROR_SUCCESS == lrt)
  190. {
  191. lrt = ::RegSetValueEx( _hkey,
  192. szStartValue,
  193. 0,
  194. REG_DWORD,
  195. (PBYTE)&dwStartValue,
  196. sizeof( DWORD ) );
  197. if (ERROR_SUCCESS == lrt)
  198. {
  199. lrt = ::RegSetValueEx( _hkey,
  200. szTypeValue,
  201. 0,
  202. REG_DWORD,
  203. (PBYTE)&dwTypeValue,
  204. sizeof( DWORD ) );
  205. }
  206. }
  207. }
  208. ::RegCloseKey( hkeyRemote );
  209. }
  210. return( lrt );
  211. }
  212. //-------------------------------------------------------------------
  213. //
  214. // Method: CLicRegLicense::EnumService
  215. //
  216. // Summary;
  217. // This will enumerate services listed in the registry for Licensing
  218. //
  219. // Arguments;
  220. // iService [in] - This should be zero on the first call and incremented
  221. // on subsequent calls.
  222. // pszBuffer [out] - string buffer to place service reg name in
  223. // cchBuffer [in-out] - the length of the pszBuffer, if not large enough,
  224. // this value will change to what is needed.
  225. //
  226. // Return:
  227. // ERROR_SUCCESS when this method works.
  228. // ERROR_NO_MORE_ITEMS when end of enumeration was reached
  229. // See RegEnumKeyEx for error return values
  230. //
  231. // Notes:
  232. //
  233. // History;
  234. // Nov-15-94 MikeMi Created
  235. //
  236. //-------------------------------------------------------------------
  237. LONG
  238. CLicRegLicense::EnumService( DWORD iService, LPWSTR pszBuffer, DWORD& cchBuffer )
  239. {
  240. LONG lrt;
  241. FILETIME ftLastWritten;
  242. lrt = ::RegEnumKeyEx( _hkey,
  243. iService,
  244. pszBuffer,
  245. &cchBuffer,
  246. 0,
  247. NULL,
  248. NULL,
  249. &ftLastWritten );
  250. return( lrt );
  251. }
  252. //-------------------------------------------------------------------
  253. //
  254. // Method: CLicRegLicenseService::CLicRegLicenseService
  255. //
  256. // Summary;
  257. // Contructor
  258. //
  259. // Arguments;
  260. // pszService [in] - Service Reg Key Name
  261. //
  262. // History;
  263. // Nov-15-94 MikeMi Created
  264. //
  265. //-------------------------------------------------------------------
  266. CLicRegLicenseService::CLicRegLicenseService( LPCWSTR pszService )
  267. {
  268. _pszService = (LPWSTR)pszService;
  269. }
  270. //-------------------------------------------------------------------
  271. //
  272. // Method: CLicRegLicenseService::SetServie
  273. //
  274. // Summary;
  275. // Set the Service Reg Key name
  276. //
  277. // Arguments;
  278. // pszService [in] - Service Reg Key Name
  279. //
  280. // History;
  281. // Nov-15-94 MikeMi Created
  282. // Apr-26-95 MikeMi Added Computer name and remoting
  283. //
  284. //-------------------------------------------------------------------
  285. void
  286. CLicRegLicenseService::SetService( LPCWSTR pszService )
  287. {
  288. Close();
  289. _pszService = (LPWSTR)pszService;
  290. }
  291. //-------------------------------------------------------------------
  292. //
  293. // Method: CLicRegLicenseService::Open
  294. //
  295. // Summary;
  296. // Opens/Create registry entry for this service
  297. //
  298. // Arguments;
  299. // pszComputer [in] - the computer name to open the registry on
  300. // this value maybe null (default), this means local machine
  301. // should be of the form \\name
  302. //
  303. // Return:
  304. // ERROR_SUCCESS - Open/Created Correctly
  305. // See RegCreateKeyEx for other errors.
  306. //
  307. // History;
  308. // Nov-15-94 MikeMi Created
  309. //
  310. //-------------------------------------------------------------------
  311. LONG
  312. CLicRegLicenseService::Open( LPCWSTR pszComputer, BOOL fCreate )
  313. {
  314. HKEY hkeyRoot;
  315. DWORD dwDisposition;
  316. LONG lrt;
  317. HKEY hkeyRemote = NULL;
  318. lrt = RegConnectRegistry( (LPTSTR)pszComputer,
  319. HKEY_LOCAL_MACHINE,
  320. &hkeyRemote );
  321. if (ERROR_SUCCESS == lrt)
  322. {
  323. if (fCreate)
  324. {
  325. lrt = ::RegCreateKeyEx( hkeyRemote,
  326. szLicenseKey,
  327. 0,
  328. NULL,
  329. REG_OPTION_NON_VOLATILE,
  330. KEY_ALL_ACCESS,
  331. NULL,
  332. &hkeyRoot,
  333. &dwDisposition );
  334. }
  335. else
  336. {
  337. lrt = ::RegOpenKeyEx( hkeyRemote,
  338. szLicenseKey,
  339. 0,
  340. KEY_ALL_ACCESS,
  341. &hkeyRoot );
  342. }
  343. if (ERROR_SUCCESS == lrt)
  344. {
  345. // open or create our service key
  346. //
  347. if (fCreate)
  348. {
  349. lrt = ::RegCreateKeyEx( hkeyRoot,
  350. _pszService,
  351. 0,
  352. NULL,
  353. REG_OPTION_NON_VOLATILE,
  354. KEY_ALL_ACCESS,
  355. NULL,
  356. &_hkey,
  357. &dwDisposition );
  358. }
  359. else
  360. {
  361. lrt = ::RegOpenKeyEx( hkeyRoot,
  362. _pszService,
  363. 0,
  364. KEY_ALL_ACCESS,
  365. &_hkey );
  366. }
  367. ::RegCloseKey( hkeyRoot );
  368. }
  369. ::RegCloseKey( hkeyRemote );
  370. }
  371. return( lrt );
  372. }
  373. //-------------------------------------------------------------------
  374. //
  375. // Method: CLicRegLicenseService::CanChangeMode
  376. //
  377. // Summary;
  378. // This will check the registry to see if the license mode
  379. // can be changed.
  380. //
  381. // Return: TRUE if the mode can be changed, otherwise FALSE
  382. //
  383. // History;
  384. // Nov-15-94 MikeMi Created
  385. //
  386. //-------------------------------------------------------------------
  387. BOOL
  388. CLicRegLicenseService::CanChangeMode()
  389. {
  390. BOOL frt = TRUE;
  391. LONG lrt;
  392. DWORD dwSize = sizeof( DWORD );
  393. DWORD dwRegType = REG_DWORD;
  394. DWORD fWasChanged;
  395. lrt = ::RegQueryValueEx( _hkey,
  396. (LPWSTR)szFlipValue,
  397. 0,
  398. &dwRegType,
  399. (PBYTE)&fWasChanged,
  400. &dwSize );
  401. if ( (ERROR_SUCCESS == lrt) &&
  402. (dwRegType == REG_DWORD) &&
  403. (dwSize == sizeof( DWORD )) )
  404. {
  405. frt = !fWasChanged;
  406. }
  407. else
  408. {
  409. SetChangeFlag( FALSE );
  410. }
  411. return( frt );
  412. }
  413. //-------------------------------------------------------------------
  414. //
  415. // Method: CLicRegLicenseService::SetChangeFlag
  416. //
  417. // Summary;
  418. // This will set the change flag in the registry
  419. //
  420. // Arguments;
  421. // fHasChanged [in] - Has the license been changed
  422. //
  423. // Return:
  424. // ERROR_SUCCESS - The flag was set
  425. // See RegSetValueEx for error returns
  426. //
  427. // Notes:
  428. //
  429. // History;
  430. // Nov-15-94 MikeMi Created
  431. //
  432. //-------------------------------------------------------------------
  433. LONG
  434. CLicRegLicenseService::SetChangeFlag( BOOL fHasChanged )
  435. {
  436. LONG lrt;
  437. DWORD dwf = (DWORD)fHasChanged;
  438. lrt = ::RegSetValueEx( _hkey,
  439. szFlipValue,
  440. 0,
  441. REG_DWORD,
  442. (PBYTE)&dwf,
  443. sizeof( DWORD ) );
  444. return( lrt );
  445. }
  446. //-------------------------------------------------------------------
  447. //
  448. // Method: CLicRegLicenseService::SetMode
  449. //
  450. // Summary;
  451. // Set this services licensing mode
  452. //
  453. // Arguments;
  454. // lm [in] - the mode to set the registry to
  455. //
  456. // Return:
  457. // ERROR_SUCCESS - The mode was set
  458. // See RegSetValueEx for error returns
  459. //
  460. // Notes:
  461. //
  462. // History;
  463. // Nov-15-94 MikeMi Created
  464. //
  465. //-------------------------------------------------------------------
  466. LONG
  467. CLicRegLicenseService::SetMode( LICENSE_MODE lm )
  468. {
  469. LONG lrt;
  470. DWORD dwlm = (DWORD)lm;
  471. lrt = ::RegSetValueEx( _hkey,
  472. szModeValue,
  473. 0,
  474. REG_DWORD,
  475. (PBYTE)&dwlm,
  476. sizeof( DWORD ) );
  477. return( lrt );
  478. }
  479. //-------------------------------------------------------------------
  480. //
  481. // Method: CLicRegLicenseService::SetUserLimit
  482. //
  483. // Summary;
  484. // Set this serices user limit in the registry
  485. //
  486. // Arguments;
  487. // dwLimit[in] - the limit to set
  488. //
  489. // Return:
  490. // ERROR_SUCCESS - The limit was set
  491. // See RegSetValueEx for error returns
  492. //
  493. // Notes:
  494. //
  495. // History;
  496. // Nov-15-94 MikeMi Created
  497. //
  498. //-------------------------------------------------------------------
  499. LONG
  500. CLicRegLicenseService::SetUserLimit( DWORD dwLimit )
  501. {
  502. LONG lrt;
  503. lrt = ::RegSetValueEx( _hkey,
  504. szLimitValue,
  505. 0,
  506. REG_DWORD,
  507. (PBYTE)&dwLimit,
  508. sizeof( DWORD ) );
  509. return( lrt );
  510. }
  511. //-------------------------------------------------------------------
  512. //
  513. // Method: CLicRegLicenseService::GetMode
  514. //
  515. // Summary;
  516. // Retrieve the services license mode from the registry
  517. //
  518. // Arguments;
  519. // lm [out] - the mode from the registry
  520. //
  521. // Return:
  522. // ERROR_SUCCESS - The mode was retrieved
  523. // See RegQueryValueEx for error returns
  524. //
  525. // Notes:
  526. //
  527. // History;
  528. // Nov-15-94 MikeMi Created
  529. //
  530. //-------------------------------------------------------------------
  531. LONG
  532. CLicRegLicenseService::GetMode( LICENSE_MODE& lm )
  533. {
  534. LONG lrt;
  535. DWORD dwSize = sizeof( LICENSE_MODE );
  536. DWORD dwRegType = REG_DWORD;
  537. DWORD dwlm = LICMODE_UNDEFINED;
  538. lrt = ::RegQueryValueEx( _hkey,
  539. (LPWSTR)szModeValue,
  540. 0,
  541. &dwRegType,
  542. (PBYTE)&dwlm,
  543. &dwSize );
  544. lm = (LICENSE_MODE)dwlm;
  545. if ( (dwRegType != REG_DWORD) ||
  546. (dwSize != sizeof( LICENSE_MODE )) )
  547. {
  548. lrt = ERROR_BADDB;
  549. }
  550. if (ERROR_SUCCESS != lrt)
  551. {
  552. lm = LICMODE_UNDEFINED;
  553. }
  554. return( lrt );
  555. }
  556. //-------------------------------------------------------------------
  557. //
  558. // Method: CLicRegLicenseService::GetUserLimit
  559. //
  560. // Summary;
  561. // retrieve the user limit fro this service from the registry
  562. //
  563. // Arguments;
  564. // dwLimit [out] - The limit retrieved
  565. //
  566. // Return:
  567. //
  568. // Notes:
  569. // ERROR_SUCCESS - The limit was retrieved
  570. // See RegQueryValueEx for error returns
  571. //
  572. // History;
  573. // Nov-15-94 MikeMi Created
  574. //
  575. //-------------------------------------------------------------------
  576. LONG
  577. CLicRegLicenseService::GetUserLimit( DWORD& dwLimit )
  578. {
  579. LONG lrt;
  580. DWORD dwSize = sizeof( DWORD );
  581. DWORD dwRegType = REG_DWORD;
  582. lrt = ::RegQueryValueEx( _hkey,
  583. (LPWSTR)szLimitValue,
  584. 0,
  585. &dwRegType,
  586. (PBYTE)&dwLimit,
  587. &dwSize );
  588. if ( (dwRegType != REG_DWORD) ||
  589. (dwSize != sizeof( DWORD )) )
  590. {
  591. lrt = ERROR_BADDB;
  592. }
  593. if (ERROR_SUCCESS != lrt)
  594. {
  595. dwLimit = 0;
  596. }
  597. return( lrt );
  598. }
  599. //-------------------------------------------------------------------
  600. //
  601. // Method: CLicRegLicenseService::GetDisplayName
  602. //
  603. // Summary;
  604. // Retrieve the display name for this service from the registry
  605. //
  606. // Arguments;
  607. // pszName [in-out] - the buffer to place the retrieved name
  608. // cchName [in-out] - the length of the pszName buffer in chars
  609. //
  610. // Return:
  611. // ERROR_SUCCESS - The mode was retrieved
  612. // See RegQueryValueEx for error returns
  613. //
  614. // Notes:
  615. //
  616. // History;
  617. // Nov-18-94 MikeMi Created
  618. //
  619. //-------------------------------------------------------------------
  620. LONG
  621. CLicRegLicenseService::GetDisplayName( LPWSTR pszName, DWORD& cchName )
  622. {
  623. LONG lrt;
  624. DWORD dwSize = cchName * sizeof(WCHAR);
  625. DWORD dwRegType = REG_SZ;
  626. lrt = ::RegQueryValueEx( _hkey,
  627. (LPWSTR)szNameValue,
  628. 0,
  629. &dwRegType,
  630. (PBYTE)pszName,
  631. &dwSize );
  632. if ((NULL != pszName) && // request for data size
  633. (dwRegType != REG_SZ))
  634. {
  635. lrt = ERROR_BADDB;
  636. }
  637. cchName = dwSize / sizeof( WCHAR );
  638. return( lrt );
  639. }
  640. //-------------------------------------------------------------------
  641. //
  642. // Method: CLicRegLicenseService::SetDisplayName
  643. //
  644. // Summary;
  645. // Set the display name for this service in the regstry
  646. //
  647. // Arguments;
  648. // pszName [in] - the null terminated display name
  649. //
  650. // Return:
  651. // ERROR_SUCCESS - The name eas set
  652. // See RegSetValueEx for error returns
  653. //
  654. // Notes:
  655. //
  656. // History;
  657. // Nov-18-94 MikeMi Created
  658. //
  659. //-------------------------------------------------------------------
  660. LONG
  661. CLicRegLicenseService::SetDisplayName( LPCWSTR pszName )
  662. {
  663. LONG lrt;
  664. lrt = ::RegSetValueEx( _hkey,
  665. szNameValue,
  666. 0,
  667. REG_SZ,
  668. (PBYTE)pszName,
  669. (lstrlen( pszName ) + 1) * sizeof( WCHAR ) );
  670. return( lrt );
  671. }
  672. //-------------------------------------------------------------------
  673. //
  674. // Method: CLicRegLicenseService::GetFamilyDisplayName
  675. //
  676. // Summary;
  677. // Retrieve the family display name for this service from the registry
  678. //
  679. // Arguments;
  680. // pszName [in-out] - the buffer to place the retrieved name
  681. // cchName [in-out] - the length of the pszName buffer in chars
  682. //
  683. // Return:
  684. // ERROR_SUCCESS - The mode was retrieved
  685. // See RegQueryValueEx for error returns
  686. //
  687. // Notes:
  688. //
  689. // History;
  690. // Nov-18-94 MikeMi Created
  691. //
  692. //-------------------------------------------------------------------
  693. LONG
  694. CLicRegLicenseService::GetFamilyDisplayName( LPWSTR pszName, DWORD& cchName )
  695. {
  696. LONG lrt;
  697. DWORD dwSize = cchName * sizeof(WCHAR);
  698. DWORD dwRegType = REG_SZ;
  699. lrt = ::RegQueryValueEx( _hkey,
  700. (LPWSTR)szFamilyNameValue,
  701. 0,
  702. &dwRegType,
  703. (PBYTE)pszName,
  704. &dwSize );
  705. if ((NULL != pszName) && // request for data size
  706. (dwRegType != REG_SZ))
  707. {
  708. lrt = ERROR_BADDB;
  709. }
  710. cchName = dwSize / sizeof( WCHAR );
  711. return( lrt );
  712. }
  713. //-------------------------------------------------------------------
  714. //
  715. // Method: CLicRegLicenseService::SetFamilyDisplayName
  716. //
  717. // Summary;
  718. // Set the Family display name for this service in the regstry
  719. //
  720. // Arguments;
  721. // pszName [in] - the null terminated display name
  722. //
  723. // Return:
  724. // ERROR_SUCCESS - The name eas set
  725. // See RegSetValueEx for error returns
  726. //
  727. // Notes:
  728. //
  729. // History;
  730. // Nov-18-94 MikeMi Created
  731. //
  732. //-------------------------------------------------------------------
  733. LONG
  734. CLicRegLicenseService::SetFamilyDisplayName( LPCWSTR pszName )
  735. {
  736. LONG lrt;
  737. lrt = ::RegSetValueEx( _hkey,
  738. szFamilyNameValue,
  739. 0,
  740. REG_SZ,
  741. (PBYTE)pszName,
  742. (lstrlen( pszName ) + 1) * sizeof( WCHAR ) );
  743. return( lrt );
  744. }