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.

697 lines
20 KiB

  1. #define STRICT
  2. #include <windows.h>
  3. #include <stdlib.h>
  4. #include <stdarg.h>
  5. #include <crtdbg.h>
  6. #include <winbase.h>
  7. #include <ras.h>
  8. #include <time.h>
  9. #include "icwunicd.h"
  10. #include "RegData.h"
  11. //-----------------------------------------------------------------------------
  12. // Defines
  13. //-----------------------------------------------------------------------------
  14. #define MAX_REGSTRING 150
  15. #define DEFAULT_DIALOGTIMEOUT 1800000 // half hour
  16. #define DEFAULT_SLEEPDURATION 30000 // 30 seconds
  17. //-----------------------------------------------------------------------------
  18. // Global Handles and other defines
  19. //-----------------------------------------------------------------------------
  20. time_t g_tStartDate = 0;
  21. int g_nISPTrialDays = 0;
  22. int g_nTotalNotifications = -1;
  23. DWORD g_dwDialogTimeOut = 0;
  24. DWORD g_dwWakeupInterval = 0;
  25. TCHAR g_szISPName[MAX_REGSTRING];
  26. TCHAR g_szISPPhone[MAX_REGSTRING];
  27. TCHAR g_szSignupURL[MAX_REGSTRING];
  28. TCHAR g_szISPMsg[MAX_ISPMSGSTRING];
  29. TCHAR g_szSignupURLTrialOver[MAX_REGSTRING];
  30. TCHAR g_szConnectoidName[MAX_REGSTRING];
  31. //-----------------------------------------------------------------------------
  32. // Registry entry strings.
  33. //-----------------------------------------------------------------------------
  34. static const TCHAR* g_szKeyRunOnce = TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\RunOnce");
  35. static const TCHAR* g_szEntryRunOnce = TEXT("IcwRmind");
  36. // Key for IE run once stuff
  37. static const TCHAR* g_szKeyIERunOnce = TEXT("Software\\Microsoft\\Internet Explorer\\Main");
  38. static const TCHAR* g_szEntryIERunOnce = TEXT("First Home Page");
  39. static const TCHAR* g_szHtmlFile = TEXT("TrialExp.html");
  40. // This is the key where all the application data will be stored.
  41. static const TCHAR* g_szKeyIcwRmind = TEXT("Software\\Microsoft\\Internet Connection Wizard\\IcwRmind");
  42. // These entries will be created by the connection wizard.
  43. static const TCHAR* g_szEntryISPName = TEXT("ISP_Name");
  44. static const TCHAR* g_szEntryISPPhone = TEXT("ISP_Phone");
  45. static const TCHAR* g_szEntryISPMsg = TEXT("ISP_Message");
  46. static const TCHAR* g_szEntryTrialDays = TEXT("Trial_Days");
  47. static const TCHAR* g_szEntrySignupURL = TEXT("Signup_URL");
  48. static const TCHAR* g_szEntrySignupURLTrialOver = TEXT("Expired_URL");
  49. static const TCHAR* g_szEntryConnectoidName = TEXT("Entry_Name");
  50. static const TCHAR* g_szSignupSuccessfuly = TEXT("TrialConverted");
  51. // These entries will be created by this application.
  52. static const TCHAR* g_szEntryTrialStart = TEXT("Trial_Start");
  53. static const TCHAR* g_szEntryTrialStartString = TEXT("Trial_Start_String");
  54. static const TCHAR* g_szEntryAppIsVisible = TEXT("App_IsVisible");
  55. static const TCHAR* g_szEntryWakeupInterval = TEXT("Wakeup_Interval");
  56. static const TCHAR* g_szEntryTotalNotifications = TEXT("Total_Notifications");
  57. static const TCHAR* g_szEntryDialogTimeOut = TEXT("Dialog_TimeOut");
  58. //-----------------------------------------------------------------------------
  59. // GetWakeupInterval
  60. //-----------------------------------------------------------------------------
  61. DWORD GetWakeupInterval()
  62. {
  63. if (g_dwWakeupInterval)
  64. {
  65. return g_dwWakeupInterval;
  66. }
  67. CMcRegistry reg;
  68. if (OpenIcwRmindKey(reg))
  69. {
  70. bool bRetCode = reg.GetValue(g_szEntryWakeupInterval, g_dwWakeupInterval);
  71. // If not in the registry then set the default value.
  72. if (!bRetCode)
  73. {
  74. g_dwWakeupInterval = DEFAULT_SLEEPDURATION;
  75. }
  76. }
  77. return g_dwWakeupInterval;
  78. }
  79. //-----------------------------------------------------------------------------
  80. // GetDialogTimeout
  81. //-----------------------------------------------------------------------------
  82. DWORD GetDialogTimeout()
  83. {
  84. if (g_dwDialogTimeOut)
  85. {
  86. return g_dwDialogTimeOut;
  87. }
  88. CMcRegistry reg;
  89. if (OpenIcwRmindKey(reg))
  90. {
  91. bool bRetCode = reg.GetValue(g_szEntryDialogTimeOut, g_dwDialogTimeOut);
  92. // If not in the registry then set the default value.
  93. if (!bRetCode)
  94. {
  95. g_dwDialogTimeOut = DEFAULT_DIALOGTIMEOUT;
  96. }
  97. }
  98. return g_dwDialogTimeOut;
  99. }
  100. //-----------------------------------------------------------------------------
  101. // IsApplicationVisible
  102. //-----------------------------------------------------------------------------
  103. BOOL IsApplicationVisible()
  104. {
  105. // This data is debug data so it is not cached. Default value is
  106. // FALSE if not found in registry.
  107. BOOL bVisible = FALSE;
  108. CMcRegistry reg;
  109. if (OpenIcwRmindKey(reg))
  110. {
  111. DWORD dwData = 0;
  112. bool bRetCode = reg.GetValue(g_szEntryAppIsVisible, dwData);
  113. if (bRetCode)
  114. {
  115. bVisible = (BOOL) dwData;
  116. }
  117. }
  118. return bVisible;
  119. }
  120. //-----------------------------------------------------------------------------
  121. // GetConnectionName
  122. //-----------------------------------------------------------------------------
  123. const TCHAR* GetISPConnectionName()
  124. {
  125. // If we already retrieved this then simply pass it back.
  126. if (lstrlen(g_szConnectoidName))
  127. {
  128. return g_szConnectoidName;
  129. }
  130. CMcRegistry reg;
  131. if (OpenIcwRmindKey(reg))
  132. {
  133. bool bRetCode = reg.GetValue(g_szEntryConnectoidName, g_szConnectoidName, sizeof(TCHAR)*MAX_REGSTRING);
  134. _ASSERT(bRetCode);
  135. }
  136. return g_szConnectoidName;
  137. }
  138. //-----------------------------------------------------------------------------
  139. // GetISPSignupUrl
  140. //-----------------------------------------------------------------------------
  141. const TCHAR* GetISPSignupUrl()
  142. {
  143. // If we already retrieved this then simply pass it back.
  144. if (lstrlen(g_szSignupURL))
  145. {
  146. return g_szSignupURL;
  147. }
  148. CMcRegistry reg;
  149. if (OpenIcwRmindKey(reg))
  150. {
  151. bool bRetCode = reg.GetValue(g_szEntrySignupURL, g_szSignupURL, sizeof(TCHAR)*MAX_REGSTRING);
  152. _ASSERT(bRetCode);
  153. }
  154. return g_szSignupURL;
  155. }
  156. //-----------------------------------------------------------------------------
  157. // GetISPSignupUrlTrialOver
  158. //-----------------------------------------------------------------------------
  159. const TCHAR* GetISPSignupUrlTrialOver()
  160. {
  161. // If we already retrieved this then simply pass it back.
  162. if (lstrlen(g_szSignupURLTrialOver))
  163. {
  164. return g_szSignupURLTrialOver;
  165. }
  166. CMcRegistry reg;
  167. if (OpenIcwRmindKey(reg))
  168. {
  169. bool bRetCode = reg.GetValue(g_szEntrySignupURLTrialOver, g_szSignupURLTrialOver, sizeof(TCHAR)*MAX_REGSTRING);
  170. _ASSERT(bRetCode);
  171. }
  172. return g_szSignupURLTrialOver;
  173. }
  174. //-----------------------------------------------------------------------------
  175. // SetupRunOnce
  176. //-----------------------------------------------------------------------------
  177. void SetupRunOnce()
  178. {
  179. CMcRegistry reg;
  180. bool bRetCode = reg.OpenKey(HKEY_LOCAL_MACHINE, g_szKeyRunOnce);
  181. _ASSERT(bRetCode);
  182. if (bRetCode)
  183. {
  184. LPTSTR lpszFileName = new TCHAR[_MAX_PATH + 23];
  185. if (GetModuleFileName(GetModuleHandle(NULL), lpszFileName, _MAX_PATH + 20))
  186. {
  187. // Add a command line parameter.
  188. lstrcat(lpszFileName, TEXT(" -R"));
  189. bRetCode = reg.SetValue(g_szEntryRunOnce, lpszFileName);
  190. _ASSERT(bRetCode);
  191. }
  192. delete [] lpszFileName;
  193. }
  194. }
  195. //-----------------------------------------------------------------------------
  196. // RemoveRunOnce
  197. //-----------------------------------------------------------------------------
  198. void RemoveRunOnce()
  199. {
  200. CMcRegistry reg;
  201. bool bRetCode = reg.OpenKey(HKEY_LOCAL_MACHINE, g_szKeyRunOnce);
  202. _ASSERT(bRetCode);
  203. if (bRetCode)
  204. {
  205. bRetCode = reg.SetValue(g_szEntryRunOnce, TEXT(""));
  206. _ASSERT(bRetCode);
  207. }
  208. }
  209. //-----------------------------------------------------------------------------
  210. // GetISPName
  211. //-----------------------------------------------------------------------------
  212. const TCHAR* GetISPName()
  213. {
  214. // If we already retrieved this then simply pass it back.
  215. if (lstrlen(g_szISPName))
  216. {
  217. return g_szISPName;
  218. }
  219. CMcRegistry reg;
  220. if (OpenIcwRmindKey(reg))
  221. {
  222. bool bRetCode = reg.GetValue(g_szEntryISPName, g_szISPName, sizeof(TCHAR)*MAX_REGSTRING);
  223. _ASSERT(bRetCode);
  224. }
  225. return g_szISPName;
  226. }
  227. //-----------------------------------------------------------------------------
  228. // GetISPPhone
  229. //-----------------------------------------------------------------------------
  230. const TCHAR* GetISPPhone()
  231. {
  232. // If we already retrieved this then simply pass it back.
  233. if (lstrlen(g_szISPPhone))
  234. {
  235. return g_szISPPhone;
  236. }
  237. CMcRegistry reg;
  238. if (OpenIcwRmindKey(reg))
  239. {
  240. bool bRetCode = reg.GetValue(g_szEntryISPPhone, g_szISPPhone, sizeof(TCHAR)*MAX_REGSTRING);
  241. _ASSERT(bRetCode);
  242. }
  243. return g_szISPPhone;
  244. }
  245. //-----------------------------------------------------------------------------
  246. // GetISPMessage
  247. //-----------------------------------------------------------------------------
  248. const TCHAR* GetISPMessage()
  249. {
  250. // If we already retrieved this then simply pass it back.
  251. if (lstrlen(g_szISPMsg))
  252. {
  253. return g_szISPMsg;
  254. }
  255. CMcRegistry reg;
  256. if (OpenIcwRmindKey(reg))
  257. {
  258. bool bRetCode = reg.GetValue(g_szEntryISPMsg, g_szISPMsg, sizeof(TCHAR)*MAX_ISPMSGSTRING);
  259. _ASSERT(bRetCode);
  260. }
  261. return g_szISPMsg;
  262. }
  263. //-----------------------------------------------------------------------------
  264. // GetISPTrialDays
  265. //-----------------------------------------------------------------------------
  266. int GetISPTrialDays()
  267. {
  268. // If we already retrieved this then simply pass it back.
  269. if (g_nISPTrialDays)
  270. {
  271. return g_nISPTrialDays;
  272. }
  273. CMcRegistry reg;
  274. if (OpenIcwRmindKey(reg))
  275. {
  276. DWORD dwData = 0;
  277. bool bRetCode = reg.GetValue(g_szEntryTrialDays, dwData);
  278. _ASSERT(bRetCode);
  279. if (bRetCode)
  280. {
  281. g_nISPTrialDays = (int) dwData;
  282. }
  283. }
  284. return g_nISPTrialDays;
  285. }
  286. //-----------------------------------------------------------------------------
  287. // GetTrialStartDate
  288. //-----------------------------------------------------------------------------
  289. time_t GetTrialStartDate()
  290. {
  291. // If we already retrieved this then simply pass it back.
  292. if (g_tStartDate)
  293. {
  294. return g_tStartDate;
  295. }
  296. // If the trial start date entry does not exist in the registry then
  297. // this is the first we have been executed so the trial start date
  298. // is today's date. Put this back in the registry.
  299. CMcRegistry reg;
  300. if (OpenIcwRmindKey(reg))
  301. {
  302. DWORD dwData = 0;
  303. bool bRetCode = reg.GetValue(g_szEntryTrialStart, dwData);
  304. if (bRetCode && 0 != dwData)
  305. {
  306. g_tStartDate = (time_t) dwData;
  307. }
  308. else
  309. {
  310. time_t tTime;
  311. time(&tTime);
  312. if (reg.SetValue(g_szEntryTrialStart, (DWORD) tTime))
  313. {
  314. g_tStartDate = tTime;
  315. SetStartDateString(tTime);
  316. }
  317. }
  318. }
  319. return g_tStartDate;
  320. }
  321. //-----------------------------------------------------------------------------
  322. // OpenIcwRmindKey
  323. //-----------------------------------------------------------------------------
  324. bool OpenIcwRmindKey(CMcRegistry &reg)
  325. {
  326. // This method will open the IcwRmind key in the registry. If the key
  327. // does not exist it will be created here.
  328. bool bRetCode = reg.OpenKey(HKEY_LOCAL_MACHINE, g_szKeyIcwRmind);
  329. if (!bRetCode)
  330. {
  331. bRetCode = reg.CreateKey(HKEY_LOCAL_MACHINE, g_szKeyIcwRmind);
  332. _ASSERT(bRetCode);
  333. }
  334. return bRetCode;
  335. }
  336. //-----------------------------------------------------------------------------
  337. // ClearCachedData
  338. //-----------------------------------------------------------------------------
  339. void ClearCachedData()
  340. {
  341. // Clear all the global data so that it will be reread out of the
  342. // registry.
  343. g_tStartDate = 0;
  344. g_nISPTrialDays = 0;
  345. g_dwDialogTimeOut = 0;
  346. g_dwWakeupInterval = 0;
  347. g_szISPName[0] = 0;
  348. g_szISPMsg[0] = 0;
  349. g_szISPPhone[0] = 0;
  350. g_szSignupURL[0] = 0;
  351. g_szSignupURLTrialOver[0] = 0;
  352. g_szConnectoidName[0] = 0;
  353. g_nTotalNotifications = -1;
  354. }
  355. //-----------------------------------------------------------------------------
  356. // ResetCachedData
  357. //-----------------------------------------------------------------------------
  358. void ResetCachedData()
  359. {
  360. // Clear all the global data so that it will be reread out of the
  361. // registry.
  362. g_tStartDate = 0;
  363. g_nISPTrialDays = 0;
  364. g_dwDialogTimeOut = 0;
  365. g_dwWakeupInterval = 0;
  366. g_szISPName[0] = 0;
  367. g_szISPMsg[0] = 0;
  368. g_szISPPhone[0] = 0;
  369. g_szSignupURL[0] = 0;
  370. g_szSignupURLTrialOver[0] = 0;
  371. g_szConnectoidName[0] = 0;
  372. g_nTotalNotifications = -1;
  373. // We must also clear the start date and total notifications out
  374. // of the registry.
  375. CMcRegistry reg;
  376. if (OpenIcwRmindKey(reg))
  377. {
  378. bool bRetCode = reg.SetValue(g_szEntryTrialStart, (DWORD) 0);
  379. _ASSERT(bRetCode);
  380. bRetCode = reg.SetValue(g_szEntryTotalNotifications, (DWORD) 0);
  381. _ASSERT(bRetCode);
  382. }
  383. }
  384. //-----------------------------------------------------------------------------
  385. // GetTotalNotifications
  386. //-----------------------------------------------------------------------------
  387. int GetTotalNotifications()
  388. {
  389. // This is the number of times we have notified the user and the user
  390. // has responded to us. We will only notify them 3 times.
  391. if (-1 != g_nTotalNotifications)
  392. {
  393. _ASSERT(g_nTotalNotifications <= 3);
  394. return g_nTotalNotifications;
  395. }
  396. CMcRegistry reg;
  397. if (OpenIcwRmindKey(reg))
  398. {
  399. DWORD dwData = 0;
  400. bool bRetCode = reg.GetValue(g_szEntryTotalNotifications, dwData);
  401. if (bRetCode)
  402. {
  403. g_nTotalNotifications = (int) dwData;
  404. }
  405. else
  406. {
  407. g_nTotalNotifications = 0;
  408. }
  409. }
  410. return g_nTotalNotifications;
  411. }
  412. //-----------------------------------------------------------------------------
  413. // IncrementTotalNotifications
  414. //-----------------------------------------------------------------------------
  415. void IncrementTotalNotifications()
  416. {
  417. _ASSERT(g_nTotalNotifications < 3 && -1 != g_nTotalNotifications);
  418. if (g_nTotalNotifications < 3 && -1 != g_nTotalNotifications)
  419. {
  420. ++g_nTotalNotifications;
  421. // Let's put it back into the registry now.
  422. CMcRegistry reg;
  423. if (OpenIcwRmindKey(reg))
  424. {
  425. DWORD dwData = 0;
  426. bool bRetCode = reg.SetValue(g_szEntryTotalNotifications, (DWORD) g_nTotalNotifications);
  427. _ASSERT(bRetCode);
  428. }
  429. }
  430. }
  431. //-----------------------------------------------------------------------------
  432. // ResetTrialStartDate
  433. //-----------------------------------------------------------------------------
  434. void ResetTrialStartDate(time_t timeNewStartDate)
  435. {
  436. CMcRegistry reg;
  437. if (OpenIcwRmindKey(reg))
  438. {
  439. if (reg.SetValue(g_szEntryTrialStart, (DWORD) timeNewStartDate))
  440. {
  441. g_tStartDate = timeNewStartDate;
  442. SetStartDateString(timeNewStartDate);
  443. }
  444. else
  445. {
  446. _ASSERT(false);
  447. }
  448. }
  449. else
  450. {
  451. _ASSERT(false);
  452. }
  453. }
  454. //-----------------------------------------------------------------------------
  455. // DeleteAllRegistryData
  456. //-----------------------------------------------------------------------------
  457. void DeleteAllRegistryData()
  458. {
  459. // Delete the Run Once data. We do this by setting the value
  460. // to nothing.
  461. CMcRegistry reg;
  462. bool bRetCode = reg.OpenKey(HKEY_LOCAL_MACHINE, g_szKeyRunOnce);
  463. _ASSERT(bRetCode);
  464. if (bRetCode)
  465. {
  466. bRetCode = reg.SetValue(g_szEntryRunOnce, TEXT(""));
  467. _ASSERT(bRetCode);
  468. }
  469. // Delete the Remind Key and all it's values.
  470. RegDeleteKey(HKEY_LOCAL_MACHINE, g_szKeyIcwRmind);
  471. }
  472. //-----------------------------------------------------------------------------
  473. // IsSignupSuccessful
  474. //-----------------------------------------------------------------------------
  475. BOOL IsSignupSuccessful()
  476. {
  477. BOOL bSuccess = FALSE;
  478. CMcRegistry reg;
  479. // Do not cache this data. Some other app will write this entry
  480. // once the user has successfully signed up.
  481. if (OpenIcwRmindKey(reg))
  482. {
  483. DWORD dwData = 0;
  484. bool bRetCode = reg.GetValue(g_szSignupSuccessfuly, dwData);
  485. if (bRetCode)
  486. {
  487. bSuccess = (BOOL) dwData;
  488. }
  489. }
  490. return bSuccess;
  491. }
  492. //-----------------------------------------------------------------------------
  493. // RemoveTrialConvertedFlag
  494. //-----------------------------------------------------------------------------
  495. void RemoveTrialConvertedFlag()
  496. {
  497. BOOL bSuccess = FALSE;
  498. CMcRegistry reg;
  499. if (OpenIcwRmindKey(reg))
  500. {
  501. bool bRetCode = reg.SetValue(g_szSignupSuccessfuly, (DWORD) 0);
  502. _ASSERT(bRetCode);
  503. }
  504. }
  505. //-----------------------------------------------------------------------------
  506. // SetStartDateString
  507. //-----------------------------------------------------------------------------
  508. void SetStartDateString(time_t timeStartDate)
  509. {
  510. CMcRegistry reg;
  511. TCHAR buf[255];
  512. wsprintf(buf, TEXT("%s"), ctime(&timeStartDate));
  513. if (OpenIcwRmindKey(reg))
  514. {
  515. reg.SetValue(g_szEntryTrialStartString, buf);
  516. }
  517. }
  518. //-----------------------------------------------------------------------------
  519. // SetIERunOnce
  520. //-----------------------------------------------------------------------------
  521. void SetIERunOnce()
  522. {
  523. CMcRegistry reg;
  524. bool bRetCode = reg.OpenKey(HKEY_CURRENT_USER, g_szKeyIERunOnce);
  525. // The html page for the IE run once is in the same directory as
  526. // the IcwRmind exe. Create the full qualified path.
  527. if (bRetCode)
  528. {
  529. TCHAR* pszBuf = new TCHAR[_MAX_PATH];
  530. if (pszBuf)
  531. {
  532. if (GetModuleFileName(GetModuleHandle(NULL), pszBuf, _MAX_PATH))
  533. {
  534. TCHAR* pszBufPath = new TCHAR[_MAX_PATH];
  535. if (pszBufPath)
  536. {
  537. TCHAR* pszDrive = new TCHAR[_MAX_DRIVE];
  538. if (pszDrive)
  539. {
  540. _tsplitpath(pszBuf, pszDrive, pszBufPath, NULL, NULL);
  541. lstrcpy(pszBuf, pszDrive);
  542. lstrcat(pszBuf, pszBufPath);
  543. lstrcat(pszBuf, g_szHtmlFile);
  544. reg.SetValue(g_szEntryIERunOnce, pszBuf);
  545. delete [] pszDrive;
  546. }
  547. delete [] pszBufPath;
  548. }
  549. }
  550. delete [] pszBuf;
  551. }
  552. }
  553. }
  554. //-----------------------------------------------------------------------------
  555. // RemoveIERunOnce
  556. //-----------------------------------------------------------------------------
  557. void RemoveIERunOnce()
  558. {
  559. HKEY hkey;
  560. long lErr = ::RegOpenKeyEx(HKEY_CURRENT_USER, g_szKeyIERunOnce, 0, KEY_READ | KEY_WRITE, &hkey);
  561. if (ERROR_SUCCESS == lErr)
  562. {
  563. RegDeleteValue(hkey, g_szEntryIERunOnce);
  564. RegCloseKey(hkey);
  565. }
  566. }