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.

400 lines
9.6 KiB

  1. // CRegWizCtrl.cpp : Implementation of CRegWizCtrl
  2. #include "stdafx.h"
  3. #include "RegwizCtrl.h"
  4. #include "CRwCtrl.h"
  5. #include "rw_common.h"
  6. #include "regutil.h"
  7. int TstFn1 ( HANDLE hInstance );
  8. _TCHAR szProductPath[256];
  9. /////////////////////////////////////////////////////////////////////////////
  10. // CRegWizCtrl
  11. STDMETHODIMP CRegWizCtrl::InterfaceSupportsErrorInfo(REFIID riid)
  12. {
  13. static const IID* arr[] =
  14. {
  15. &IID_IRegWizCtrl,
  16. };
  17. for (int i=0;i<sizeof(arr)/sizeof(arr[0]);i++)
  18. {
  19. if (InlineIsEqualGUID(*arr[i],riid))
  20. return S_OK;
  21. }
  22. return S_FALSE;
  23. }
  24. STDMETHODIMP CRegWizCtrl::InvokeRegWizard(BSTR ProductPath)
  25. {
  26. LPCTSTR pPath;
  27. int iRet;
  28. iRet = 0; // No Error
  29. // TODO: Add your implementation code here
  30. //TstFn1 ( _Module.GetModuleInstance(), m_ctlButton.m_hWnd);
  31. #ifndef _UNICODE
  32. pPath = BstrToSz(ProductPath);
  33. #else
  34. pPath = ProductPath;
  35. #endif
  36. if(pPath) {
  37. //MessageBox("TransferRegWizInformation",lpInfo);
  38. iRet = InvokeRegistration ( _Module.GetModuleInstance() ,pPath );
  39. #ifndef _UNICODE
  40. CoTaskMemFree((void *) pPath);
  41. #endif
  42. }
  43. //return (HRESULT) iRet;
  44. //return iRet == NO_ERROR ? S_OK:E_FAIL;
  45. return S_OK;
  46. }
  47. STDMETHODIMP CRegWizCtrl::TransferRegWizInformation(BSTR InformationPath)
  48. {
  49. // TODO: Add your implementation code here
  50. LPCTSTR lpInfo;
  51. //::MessageBox(NULL,"TransferRegWizInformation","SSS ",MB_OK);
  52. #ifndef _UNICODE
  53. lpInfo = BstrToSz(InformationPath);
  54. #else
  55. lpInfo = InformationPath;
  56. #endif
  57. if(lpInfo) {
  58. // MessageBox("TransferRegWizInformation",lpInfo);
  59. TransmitRegWizInfo(_Module.GetModuleInstance(), lpInfo,FALSE);
  60. #ifndef _UNICODE
  61. CoTaskMemFree((void *) lpInfo);
  62. #endif
  63. }
  64. return S_OK;
  65. }
  66. STDMETHODIMP CRegWizCtrl::put_IsRegistered(BSTR InformationPath)
  67. {
  68. LPCTSTR lpInfo;
  69. #ifndef _UNICODE
  70. lpInfo = BstrToSz(InformationPath);
  71. #else
  72. lpInfo = InformationPath;
  73. #endif
  74. _tcscpy(szProductPath,lpInfo);
  75. return S_OK;
  76. }
  77. STDMETHODIMP CRegWizCtrl::get_IsRegistered(VARIANT_BOOL * pbStatus)
  78. {
  79. int iReturn ;
  80. HKEY hKey;
  81. TCHAR szValueName[256] = _T("");
  82. TCHAR szRetVal[48];
  83. DWORD dwSize= 48;
  84. LONG regStatus ;
  85. *pbStatus = FALSE;
  86. HINSTANCE hInstance = _Module.GetModuleInstance();
  87. int resSize = LoadString(hInstance,
  88. IDS_INPUT_ISREGISTERED,szValueName,128);
  89. iReturn = 1;
  90. regStatus = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
  91. szProductPath,0,KEY_READ |KEY_WRITE,&hKey);
  92. if (regStatus != ERROR_SUCCESS)
  93. {
  94. return E_FAIL; // error
  95. }
  96. regStatus = RegQueryValueEx(hKey,
  97. szValueName,
  98. NULL,
  99. 0,
  100. (LPBYTE) szRetVal,
  101. &dwSize);
  102. if (regStatus == ERROR_SUCCESS){
  103. // Verifty the Value
  104. //
  105. if(szRetVal[0] == _T('1')) {
  106. *pbStatus = TRUE; // Product Registred flag is set
  107. }
  108. }
  109. RegCloseKey(hKey);
  110. return S_OK;
  111. }
  112. /* B S T R C R E A T E S Z */
  113. /*-------------------------------------------------------------------------
  114. Owner: SteveBu
  115. Creates a BSTR from a string
  116. -------------------------------------------------------------------------*/
  117. #ifndef _UNICODE
  118. BSTR BstrCreateSz(LPCSTR lpa)
  119. {
  120. BSTR bsz;
  121. UINT cch;
  122. cch = lstrlen(lpa);
  123. bsz = SysAllocStringLen(NULL, cch);
  124. if (bsz == NULL)
  125. return NULL;
  126. if (cch > 0)
  127. MultiByteToWideChar(CP_ACP, 0, lpa, cch, bsz, cch);
  128. bsz[cch] = NULL;
  129. return bsz;
  130. }
  131. #endif
  132. /* G E T _ M S I D */
  133. /*-------------------------------------------------------------------------
  134. Owner: SteveBu
  135. Returns a user's MSID from the Registry. If it does not exist, this
  136. code issues a MSID using GetNewGUID.
  137. -------------------------------------------------------------------------*/
  138. STDMETHODIMP CRegWizCtrl::get_MSID(BSTR *pbstrMSID)
  139. {
  140. HRESULT hr = S_OK;
  141. _TCHAR szValue[256];
  142. char czNewGuid[256];
  143. BOOL fPutMSID = FALSE;
  144. szValue[0] = '\0';
  145. *pbstrMSID = NULL;
  146. if(!GetMSIDfromRegistry(_Module.GetModuleInstance(), szValue))
  147. {
  148. GetNewGUID(czNewGuid);
  149. _tcscpy(szValue,ConvertToUnicode(czNewGuid));
  150. fPutMSID = TRUE;
  151. }
  152. // TODO: Add your implementation code here
  153. #ifndef _UNICODE
  154. if ((*pbstrMSID = BstrCreateSz(szValue)) == NULL)
  155. #else
  156. if ((*pbstrMSID = SysAllocString(szValue)) == NULL)
  157. #endif // _UNICODE
  158. {
  159. hr = E_OUTOFMEMORY;
  160. }
  161. if (SUCCEEDED(hr) && fPutMSID && *pbstrMSID)
  162. return put_MSID(*pbstrMSID);
  163. return hr;
  164. }
  165. /* P U T _ M S I D */
  166. /*-------------------------------------------------------------------------
  167. Owner: SteveBu
  168. Adds a MSID to the registry
  169. -------------------------------------------------------------------------*/
  170. STDMETHODIMP CRegWizCtrl::put_MSID(BSTR bstrMSID)
  171. {
  172. DWORD dwRet;
  173. _TCHAR szKeyName[256];
  174. HKEY hIDKey;
  175. TCHAR szMSID[256];
  176. HINSTANCE hInstance = _Module.GetModuleInstance();
  177. _tcscpy(szKeyName,_T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion"));
  178. #ifndef _UNICODE
  179. WideCharToMultiByte(CP_ACP, 0, bstrMSID, -1, szMSID, 256, NULL, NULL);
  180. #else
  181. _tcscpy(szMSID, bstrMSID);
  182. #endif // _UNICODE
  183. dwRet = RegOpenKeyEx(HKEY_CURRENT_USER,szKeyName,0,KEY_ALL_ACCESS,&hIDKey);
  184. if (dwRet == ERROR_SUCCESS)
  185. {
  186. szMSID[32]=_T('\0');
  187. // Store MSID into
  188. dwRet = RegSetValueEx(hIDKey,_T("MSID"),NULL,REG_SZ,(CONST BYTE *)szMSID,
  189. _tcslen((LPCTSTR)szMSID));
  190. RegCloseKey(hIDKey);
  191. }
  192. _TCHAR szPartialKey[256];
  193. int resSize = LoadString(hInstance,IDS_KEY2,szKeyName,255);
  194. _tcscat(szKeyName,_T("\\"));
  195. resSize = LoadString(hInstance,IDS_KEY3,szPartialKey,255);
  196. _tcscat(szKeyName,szPartialKey);
  197. _tcscat(szKeyName,_T("\\"));
  198. resSize = LoadString(hInstance,IDS_KEY4,szPartialKey,255);
  199. _tcscat(szKeyName,szPartialKey);
  200. dwRet = RegOpenKeyEx(HKEY_CURRENT_USER,szKeyName,0,KEY_ALL_ACCESS,&hIDKey);
  201. if (dwRet == ERROR_SUCCESS)
  202. {
  203. // Store MSID into
  204. dwRet = RegSetValueEx(hIDKey,_T("MSID"),NULL,REG_SZ,(CONST BYTE *)szMSID, _tcslen((LPCTSTR)szMSID));
  205. RegCloseKey(hIDKey);
  206. }
  207. return S_OK;
  208. }
  209. /* G E T _ H W I D */
  210. /*-------------------------------------------------------------------------
  211. Owner: SteveBu
  212. Returns the machine HWID or creates it.
  213. -------------------------------------------------------------------------*/
  214. STDMETHODIMP CRegWizCtrl::get_HWID(BSTR * pbstrHWID)
  215. {
  216. HKEY hKeyHWID;
  217. HRESULT hr = S_OK;
  218. BOOL fPutHWID = FALSE;
  219. _TCHAR szKeyName[256];
  220. _TCHAR szValue[256];
  221. char czNewGuid[256];
  222. *pbstrHWID = NULL;
  223. szValue[0] = '\0';
  224. _tcscpy(szKeyName,_T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion"));
  225. LONG regStatus = RegOpenKeyEx(HKEY_LOCAL_MACHINE, szKeyName, 0, KEY_READ, &hKeyHWID);
  226. if (regStatus == ERROR_SUCCESS)
  227. {
  228. _TCHAR szValueName[64];
  229. unsigned long infoSize = 255;
  230. LoadString(_Module.GetModuleInstance(),IDS_HWID, szValueName,64);
  231. regStatus = RegQueryValueEx(hKeyHWID,szValueName,NULL,0,(LPBYTE) szValue,&infoSize);
  232. if (regStatus != ERROR_SUCCESS)
  233. {
  234. GetNewGUID(czNewGuid);
  235. _tcscpy(szValue,ConvertToUnicode(czNewGuid));
  236. fPutHWID = TRUE;
  237. }
  238. RegCloseKey(hKeyHWID);
  239. }
  240. #ifndef _UNICODE
  241. if ((*pbstrHWID = BstrCreateSz(szValue)) == NULL)
  242. #else
  243. if ((*pbstrHWID = SysAllocString(szValue)) == NULL)
  244. #endif // _UNICODE
  245. {
  246. hr = E_OUTOFMEMORY;
  247. }
  248. if (SUCCEEDED(hr) && fPutHWID && *pbstrHWID)
  249. return put_HWID(*pbstrHWID);
  250. return hr;
  251. }
  252. /* P U T _ H W I D */
  253. /*-------------------------------------------------------------------------
  254. Owner: SteveBu
  255. Adds the passed in HWID to the registry
  256. -------------------------------------------------------------------------*/
  257. STDMETHODIMP CRegWizCtrl::put_HWID(BSTR bstrHWID)
  258. {
  259. DWORD dwRet;
  260. _TCHAR szKeyName[256];
  261. HKEY hIDKey;
  262. _tcscpy(szKeyName,_T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion"));
  263. // Store HWID into Registry
  264. dwRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE,szKeyName,0,KEY_ALL_ACCESS,&hIDKey);
  265. if (dwRet == ERROR_SUCCESS)
  266. {
  267. _TCHAR szHWID[256];
  268. #ifndef _UNICODE
  269. WideCharToMultiByte(CP_ACP, 0, bstrHWID, -1, szHWID, 256, NULL, NULL);
  270. #else
  271. _tcscpy(szHWID, bstrHWID);
  272. #endif // _UNICODE
  273. dwRet = RegSetValueEx(hIDKey,_T("HWID"),NULL, REG_SZ,(CONST BYTE *)szHWID, _tcslen((LPCTSTR)szHWID));
  274. RegCloseKey(hIDKey);
  275. }
  276. return S_OK;
  277. }
  278. /* T R A N S F E R O E M R E G W I Z I N F O R M A T I O N */
  279. /*-------------------------------------------------------------------------
  280. Owner: SteveBu
  281. Transfer product registration and OEM information.
  282. -------------------------------------------------------------------------*/
  283. STDMETHODIMP CRegWizCtrl::TransferOEMRegWizInformation(BSTR InformationPath)
  284. {
  285. LPCTSTR lpInfo;
  286. //::MessageBox(NULL,"TransferOEMRegWizInformation","SSS ",MB_OK);
  287. #ifndef _UNICODE
  288. lpInfo = BstrToSz(InformationPath);
  289. #else
  290. lpInfo = InformationPath;
  291. #endif
  292. if(lpInfo) {
  293. // MessageBox("TransferRegWizInformation",lpInfo);
  294. TransmitRegWizInfo(_Module.GetModuleInstance(), lpInfo, TRUE);
  295. #ifndef _UNICODE
  296. CoTaskMemFree((void *) lpInfo);
  297. #endif
  298. }
  299. return S_OK;
  300. }
  301. /* G E T _ V E R S I O N */
  302. /*-------------------------------------------------------------------------
  303. Owner: SteveBu
  304. Returns the RegWiz Version Number
  305. -------------------------------------------------------------------------*/
  306. STDMETHODIMP CRegWizCtrl::get_Version(BSTR * pbstr)
  307. {
  308. _TCHAR szValue[256];
  309. GetRegWizardVersionString(_Module.GetModuleInstance(),szValue);
  310. #ifndef _UNICODE
  311. if ((*pbstr = BstrCreateSz(szValue)) == NULL)
  312. #else
  313. if ((*pbstr = SysAllocString(szValue)) == NULL)
  314. #endif // _UNICODE
  315. {
  316. return E_OUTOFMEMORY;
  317. }
  318. return S_OK;
  319. }