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.

477 lines
11 KiB

  1. /*
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. setting.cpp
  5. Abstract:
  6. CSetting object. Used to support Remote Assistance Channel settings.
  7. Revision History:
  8. created steveshi 08/23/00
  9. */
  10. #include "stdafx.h"
  11. #include "SAFRCFileDlg.h"
  12. #include "setting.h"
  13. #include "userenv.h"
  14. #include "stdio.h"
  15. const TCHAR cstrRCBDYINI[] = _T("RcBuddy.ini");
  16. const TCHAR cstrRCBDYAPP[] = _T("RcBuddyChannel");
  17. const TCHAR cstrSubDir[] = _T("\\Local Settings\\Application Data\\RcIncidents");
  18. extern HINSTANCE g_hInstance;
  19. BOOL CreateRAIncidentDirectory(LPTSTR path, LPCTSTR subPath);
  20. /*********************************************************
  21. Func:
  22. GetProfileString
  23. Abstract:
  24. Get profile string inside the channel's setting file.
  25. Params:
  26. bstrSec: Section key.
  27. pVal: Output string (default is "0", if not found.)
  28. *********************************************************/
  29. HRESULT CSetting::GetProfileString(BSTR bstrSec, BSTR* pVal)
  30. {
  31. HRESULT hr = S_FALSE;
  32. TCHAR sBuf[512];
  33. DWORD dwSize;
  34. USES_CONVERSION;
  35. if (FAILED(InitProfile()))
  36. goto done;
  37. dwSize = GetPrivateProfileString(cstrRCBDYAPP,
  38. W2T(bstrSec),
  39. TEXT("0"), &sBuf[0], 512, m_pIniFile);
  40. *pVal = CComBSTR(sBuf).Copy();
  41. hr = S_OK;
  42. done:
  43. return hr;
  44. }
  45. /*********************************************************
  46. Func:
  47. SetProfileString
  48. Abstract:
  49. Set profile string inside the channel's setting file.
  50. Params:
  51. bstrSec: Section key.
  52. bstrVal: New value
  53. *********************************************************/
  54. HRESULT CSetting::SetProfileString(BSTR bstrSec, BSTR bstrVal)
  55. {
  56. HRESULT hr = S_FALSE;
  57. USES_CONVERSION;
  58. if (FAILED(InitProfile()))
  59. goto done;
  60. //MessageBox(NULL,m_pIniFile,OLE2T(bstrSec),MB_OK);
  61. if (!WritePrivateProfileString(cstrRCBDYAPP, W2T(bstrSec), W2T(bstrVal), m_pIniFile))
  62. goto done;
  63. hr = S_OK;
  64. done:
  65. return hr;
  66. }
  67. /*********************************************************
  68. Func:
  69. get_GetUserProfileDirectory
  70. Abstract:
  71. Return user's profile directory
  72. *********************************************************/
  73. HRESULT CSetting::get_GetUserProfileDirectory(/*[out, retval]*/ BSTR *pVal)
  74. {
  75. HRESULT hr = S_FALSE;
  76. if (FAILED(hr = InitProfile()))
  77. goto done;
  78. *pVal = CComBSTR(m_pProfileDir).Detach();
  79. done:
  80. return hr;
  81. }
  82. /*********************************************************
  83. Func:
  84. get_GetUserTempFileName
  85. Abstract:
  86. Return a temp file name under user's profile directory
  87. *********************************************************/
  88. HRESULT CSetting::get_GetUserTempFileName(/*[out, retval]*/ BSTR *pVal)
  89. {
  90. HRESULT hr = S_FALSE;
  91. TCHAR sFile[MAX_PATH + 256];
  92. if(FAILED(InitProfile()))
  93. goto done;
  94. // Get Temp file name
  95. if (!GetTempFileName(m_pProfileDir, _T("RC"), 0, &sFile[0]))
  96. goto done;
  97. *pVal = CComBSTR(sFile).Copy();
  98. hr = S_OK;
  99. done:
  100. return hr;
  101. }
  102. //////////////////////////////////////////////////////////////////////////////////////
  103. // Helper functions used to support the above methods or properties
  104. /////////////////////////////
  105. /*********************************************************
  106. Func:
  107. InitProfile
  108. Abstract:
  109. Create the setting file.
  110. A RCIncidents subdir will be created under user's profile dir.
  111. A RcBuddy.ini file be created as the user's RA channel setting file.
  112. *********************************************************/
  113. HRESULT CSetting::InitProfile()
  114. {
  115. HRESULT hr = E_FAIL;
  116. if (m_pProfileDir && m_pIniFile) // No need to process
  117. return S_OK;
  118. if (m_pProfileDir || m_pIniFile) // Only one has value: Error. No need to process either.
  119. return E_FAIL;
  120. // Get User profile directory
  121. HANDLE hProcess = GetCurrentProcess();
  122. TCHAR* pPath = NULL;
  123. TCHAR sPath[MAX_PATH];
  124. ULONG ulSize = sizeof(sPath) - sizeof(cstrSubDir) -1; // preserve space for subdir.
  125. TCHAR sFile[MAX_PATH + 256];
  126. HANDLE hToken = NULL;
  127. int iRet = 0;
  128. BOOL bNeedFree = FALSE;
  129. DWORD dwSizePtr=0;
  130. if (!OpenProcessToken(hProcess, TOKEN_QUERY | TOKEN_WRITE, &hToken))
  131. goto done;
  132. if (!GetUserProfileDirectory(hToken, &sPath[0], &ulSize)) // Buffer not big enough
  133. {
  134. if (ulSize == sizeof(sPath)-1) // Not because of insufficent space.
  135. goto done;
  136. //SWI
  137. dwSizePtr=ulSize+1+sizeof(cstrSubDir);
  138. pPath = (TCHAR*)malloc((dwSizePtr)*sizeof(TCHAR));
  139. if (!pPath)
  140. {
  141. hr = E_OUTOFMEMORY;
  142. goto done;
  143. }
  144. bNeedFree = TRUE;
  145. if (!GetUserProfileDirectory(hToken, pPath, &ulSize))
  146. {
  147. hr = HRESULT_FROM_WIN32(GetLastError());
  148. goto done;
  149. }
  150. }
  151. if (!pPath)
  152. {
  153. dwSizePtr=MAX_PATH;
  154. pPath = sPath;
  155. }
  156. // Create RCIncidents sub dir
  157. // _tcscat(pPath, sSubDir);
  158. // iRet = SHCreateDirectoryEx(NULL, pPath, NULL);
  159. BOOL retVal= CreateRAIncidentDirectory(pPath, cstrSubDir);
  160. if (retVal == FALSE)
  161. goto done;
  162. //SWI
  163. //_tcscat(pPath, cstrSubDir);
  164. hr=StringCchCat(pPath,dwSizePtr,cstrSubDir);
  165. if(FAILED(hr))
  166. {
  167. goto done;
  168. }
  169. // if (iRet != ERROR_SUCCESS && iRet != ERROR_ALREADY_EXISTS)
  170. // goto done;
  171. // Set variables
  172. iRet = (_tcslen(pPath) + 1) * sizeof(TCHAR);
  173. m_pProfileDir = (TCHAR*)malloc(iRet);
  174. if (!m_pProfileDir)
  175. {
  176. hr = E_OUTOFMEMORY;
  177. goto done;
  178. }
  179. memcpy(m_pProfileDir, pPath, iRet);
  180. dwSizePtr= iRet + (1+sizeof(cstrRCBDYINI))*sizeof(TCHAR);
  181. m_pIniFile = (TCHAR*)malloc(dwSizePtr);
  182. if (!m_pIniFile)
  183. {
  184. hr = E_OUTOFMEMORY;
  185. goto done;
  186. }
  187. //SWI
  188. //_stprintf(m_pIniFile, _T("%s\\%s"), m_pProfileDir, cstrRCBDYINI);
  189. hr=StringCbPrintf(m_pIniFile,dwSizePtr,_T("%s\\%s"), m_pProfileDir, cstrRCBDYINI);
  190. done:
  191. if (hToken)
  192. CloseHandle(hToken);
  193. if (bNeedFree)
  194. free(pPath);
  195. return hr;
  196. }
  197. /*********************************************************
  198. Func:
  199. get_GetPropertyInBlob
  200. Abstract:
  201. Get the specified property value in Blob
  202. Params:
  203. bstrBlob: Blob for searching. (ex: 8;PASS=ABC )
  204. bstrName: property name. (ex: "PASS", without '=' char)
  205. *********************************************************/
  206. HRESULT CSetting::get_GetPropertyInBlob(/*[in]*/ BSTR bstrBlob, /*[in]*/ BSTR bstrName, /*[out, retval]*/ BSTR *pVal)
  207. {
  208. HRESULT hRet = S_FALSE;
  209. WCHAR *p1, *p2, *pEnd;
  210. LONG lTotal =0;
  211. size_t lProp = 0;
  212. size_t iNameLen;
  213. if (!bstrBlob || *bstrBlob==L'\0' || !bstrName || *bstrName ==L'\0'|| !pVal)
  214. return FALSE;
  215. iNameLen = wcslen(bstrName);
  216. pEnd = bstrBlob + wcslen(bstrBlob);
  217. p1 = p2 = bstrBlob;
  218. while (1)
  219. {
  220. // get porperty length
  221. while (*p2 != L';' && *p2 != L'\0' && iswdigit(*p2) ) p2++;
  222. if (*p2 != L';')
  223. goto done;
  224. *p2 = L'\0'; // set it to get length
  225. lProp = _wtol(p1);
  226. *p2 = L';'; // revert it back.
  227. // get property string
  228. p1 = ++p2;
  229. while (*p2 != L'=' && *p2 != L'\0' && p2 < p1+lProp) p2++;
  230. if (*p2 != L'=')
  231. goto done;
  232. if ((p2-p1==iNameLen) && (wcsncmp(p1, bstrName, iNameLen)==0) )
  233. {
  234. if (lProp == iNameLen+1) // A=B= case (no value)
  235. goto done;
  236. WCHAR C = *(p2 + lProp-iNameLen);
  237. *(p2 + lProp-iNameLen) = L'\0';
  238. *pVal = SysAllocString(p2+1);
  239. *(p2 + lProp-iNameLen) = C;
  240. hRet = S_OK;
  241. break;
  242. }
  243. // check next property
  244. p2 = p1 = p1 + lProp;
  245. if (p2 > pEnd)
  246. break;
  247. }
  248. done:
  249. return hRet;
  250. }
  251. STDMETHODIMP CSetting::AddPropertyToBlob(BSTR pName, BSTR pValue, BSTR poldBlob, BSTR *pnewBlob)
  252. {
  253. WCHAR *pszBuf = NULL;
  254. LONG len, lOldBlob = 0;
  255. BOOL bHasValue = FALSE;
  256. DWORD dwSizePtr=0;
  257. if(!pName || *pName==L'\0' || !pnewBlob)
  258. {
  259. goto done;
  260. }
  261. if(poldBlob && *poldBlob != L'\0')
  262. lOldBlob = wcslen(poldBlob);
  263. len = wcslen(pName) + 1; // ;pName=pValue 1 is for the '='
  264. if (pValue && *pValue != L'\0')
  265. {
  266. len += wcslen(pValue);
  267. bHasValue = TRUE;
  268. }
  269. //SWI
  270. dwSizePtr=len + lOldBlob + 1;
  271. pszBuf = new WCHAR[dwSizePtr];
  272. if (lOldBlob > 0)
  273. {
  274. if (bHasValue)
  275. {
  276. //SWI
  277. //swprintf(pszBuf, L"%s%d;%s=%s", poldBlob, len, pName, pValue);
  278. StringCchPrintfW(pszBuf,dwSizePtr,L"%s%d;%s=%s", poldBlob, len, pName, pValue);
  279. }
  280. else
  281. {
  282. //SWI
  283. //swprintf(pszBuf, L"%s%d;%s=", poldBlob, len, pName);
  284. StringCchPrintfW(pszBuf,dwSizePtr,L"%s%d;%s=", poldBlob, len, pName);
  285. }
  286. }
  287. else
  288. {
  289. if (bHasValue)
  290. //SWI
  291. //swprintf(pszBuf, L"%d;%s=%s", len, pName, pValue);
  292. StringCchPrintfW(pszBuf,dwSizePtr, L"%d;%s=%s", len, pName, pValue);
  293. else
  294. //SWI
  295. //swprintf(pszBuf, L"%d;%s=", len, pName);
  296. StringCchPrintfW(pszBuf,dwSizePtr, L"%d;%s=", len, pName);
  297. }
  298. done:
  299. *pnewBlob = ::SysAllocString(pszBuf);
  300. if (pszBuf) delete pszBuf;
  301. return S_OK;
  302. }
  303. BOOL CreateRAIncidentDirectory(LPTSTR path, LPCTSTR subPath)
  304. {
  305. BOOL bRetVal = FALSE;
  306. TCHAR seps[] = _T("\\");
  307. LPTSTR ptrDirPath = NULL;
  308. LPTSTR ptrSubDirPath = NULL;
  309. DWORD dwSizePtrDir=0,dwSizePtrSubDir=0;
  310. HRESULT hr=S_OK;
  311. //SWI
  312. dwSizePtrDir=strlen(path) + strlen(subPath) + 1;
  313. ptrDirPath = new TCHAR[dwSizePtrDir];
  314. if (ptrDirPath == NULL)
  315. {
  316. bRetVal = FALSE;
  317. goto done;
  318. }
  319. //SWI
  320. //lstrcpy(ptrDirPath,path);
  321. hr=StringCchCopy(ptrDirPath,dwSizePtrDir,path);
  322. if(FAILED(hr))
  323. {
  324. bRetVal = FALSE;
  325. goto done;
  326. }
  327. dwSizePtrSubDir=strlen(subPath) + 1;
  328. ptrSubDirPath = new TCHAR[dwSizePtrSubDir];
  329. if (ptrSubDirPath == NULL)
  330. {
  331. bRetVal = FALSE;
  332. goto done;
  333. }
  334. //SWI
  335. //lstrcpy(ptrSubDirPath,subPath);
  336. hr=StringCchCopy(ptrSubDirPath,dwSizePtrSubDir,subPath);
  337. if(FAILED(hr))
  338. {
  339. bRetVal = FALSE;
  340. goto done;
  341. }
  342. LPTSTR token;
  343. token= _tcstok(ptrSubDirPath, seps);
  344. while( token != NULL )
  345. {
  346. //lstrcat(ptrDirPath,_T("\\"));
  347. //lstrcat(ptrDirPath,token);
  348. hr=StringCchCat(ptrDirPath,dwSizePtrDir,_T("\\"));
  349. if(FAILED(hr))
  350. {
  351. bRetVal = FALSE;
  352. goto done;
  353. }
  354. hr=StringCchCat(ptrDirPath,dwSizePtrDir,token);
  355. if(FAILED(hr))
  356. {
  357. bRetVal = FALSE;
  358. goto done;
  359. }
  360. //MessageBox(NULL,ptrDirPath,token,MB_OK);
  361. if (CreateDirectory(ptrDirPath,NULL) == 0)
  362. {
  363. DWORD err = GetLastError();
  364. if ((err != ERROR_ALREADY_EXISTS) && (err != ERROR_SUCCESS))
  365. {
  366. bRetVal = FALSE;
  367. goto done;
  368. }
  369. }
  370. /* Get next token: */
  371. token = _tcstok(NULL, seps);
  372. }
  373. bRetVal = TRUE;
  374. done:
  375. if (ptrDirPath)
  376. delete ptrDirPath;
  377. if (ptrSubDirPath)
  378. delete ptrSubDirPath;
  379. return bRetVal;
  380. }