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.

845 lines
26 KiB

  1. ///////////////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Microsoft WMI OLE DB Provider
  4. // (C) Copyright 1999 Microsoft Corporation. All Rights Reserved.
  5. //
  6. // IPersistFile interface implementation for Datasource object
  7. //
  8. ///////////////////////////////////////////////////////////////////////////////////////////////
  9. #include "headers.h"
  10. // Key values of the different properties to be stored in the file
  11. WCHAR wszDataSource[] = L"DataSource"; // DBPROP_INIT_DATASOURCE
  12. WCHAR wszUserId[] = L"UserID"; // DBPROP_AUTH_USERID
  13. WCHAR wszMode[] = L"Mode"; // DBPROP_INIT_MODE
  14. WCHAR wszProtection[] = L"ProtectionLevel"; // DBPROP_INIT_PROTECTION_LEVEL
  15. WCHAR wszImpersonation[] = L"ImpersonationLevel"; // DBPROP_INIT_IMPERSONATION_LEVEL
  16. WCHAR wszWmioledbQualif[] = L"WmiOledbQualifiers"; // DBPROP_WMIOLEDB_QUALIFIERS
  17. WCHAR wszWmioledbAuthority[]= L"Authority"; // DBPROP_WMIOLEDB_AUTHORITY
  18. WCHAR wszSystemProperties[] = L"System Properties"; // DBPROP_WMIOLEDB_SYSTEMPROPERTIES
  19. const ULONG DBCS_MAXWID =sizeof(WCHAR);
  20. ///////////////////////////////////////////////////////////////////////////////////////////////
  21. //
  22. // Get the CLSID of the DSO.
  23. //
  24. // Returns one of the following values
  25. // HRESULT
  26. // S_OK The method succeeded.
  27. // E_FAIL Provider-specific error.
  28. //
  29. ///////////////////////////////////////////////////////////////////////////////////////////////
  30. STDMETHODIMP CImpIPersistFile::GetClassID( CLSID *pClassID )
  31. {
  32. HRESULT hr = S_OK;
  33. CSetStructuredExceptionHandler seh;
  34. TRY_BLOCK;
  35. // Serialize the object
  36. CAutoBlock cab(m_pObj->GetCriticalSection());
  37. // Clear Error information
  38. g_pCError->ClearErrorInfo();
  39. // Check the pointer
  40. if ( pClassID ){
  41. memcpy( pClassID, &CLSID_WMIOLEDB, sizeof(CLSID) );
  42. hr = S_OK;
  43. }
  44. else
  45. {
  46. hr = E_FAIL;
  47. }
  48. hr = hr == S_OK ? hr :g_pCError->PostHResult(hr,&IID_IRowsetLocate);
  49. CATCH_BLOCK_HRESULT(hr,L"IPersistFile::GetClassID");
  50. return hr;
  51. }
  52. ///////////////////////////////////////////////////////////////////////////////////////////////
  53. //
  54. // Checks an object for changes since it was last saved to its current file.
  55. //
  56. // Returns one of the following values
  57. // HRESULT
  58. // S_OK The object has changed since it was last saved
  59. // S_FALSE The object has not changed since the last saved
  60. //
  61. ///////////////////////////////////////////////////////////////////////////////////////////////
  62. STDMETHODIMP CImpIPersistFile::IsDirty(void)
  63. {
  64. HRESULT hr = S_FALSE;
  65. CSetStructuredExceptionHandler seh;
  66. TRY_BLOCK;
  67. // Serialize the object
  68. CAutoBlock cab(m_pObj->GetCriticalSection());
  69. // Clear previous Error Object for this thread
  70. g_pCError->ClearErrorInfo();
  71. hr = m_pObj->m_bIsPersitFileDirty ? S_OK : S_FALSE;
  72. hr = hr == S_OK ? hr :g_pCError->PostHResult(hr,&IID_IRowsetLocate);
  73. CATCH_BLOCK_HRESULT(hr,L"IPersistFile::IsDirty");
  74. return hr;
  75. }
  76. ///////////////////////////////////////////////////////////////////////////////////////////////
  77. //
  78. // Retrieves either the absolute path to the object's current working file
  79. // or, if there is no current working file, the object's default filename prompt.
  80. //
  81. // Returns one of the following values
  82. // HRESULT
  83. // S_OK A valid absolute path was successfully returned
  84. // S_FALSE The default save prompt was returned
  85. // E_OUTOFMEMORY The operation failed due to insufficient memory
  86. // E_FAIL The operation failed due to some reason other than
  87. // insufficient memory
  88. //
  89. ///////////////////////////////////////////////////////////////////////////////////////////////
  90. STDMETHODIMP CImpIPersistFile::GetCurFile( LPOLESTR *ppszFileName)
  91. {
  92. HRESULT hr = S_FALSE;
  93. int nFileNameLen = 0;
  94. CSetStructuredExceptionHandler seh;
  95. TRY_BLOCK;
  96. // Serialize the object
  97. CAutoBlock cab(m_pObj->GetCriticalSection());
  98. // Clear previous Error Object for this thread
  99. g_pCError->ClearErrorInfo();
  100. if (ppszFileName == NULL)
  101. hr = E_FAIL;
  102. else
  103. if(m_pObj->m_strPersistFileName != NULL)
  104. {
  105. nFileNameLen = (SysStringLen(m_pObj->m_strPersistFileName) + 1 )* sizeof(WCHAR);
  106. *ppszFileName = (LPOLESTR )g_pIMalloc->Alloc(nFileNameLen);
  107. if ( ! *ppszFileName )
  108. hr = E_OUTOFMEMORY;
  109. else
  110. {
  111. wcscpy(*ppszFileName,m_pObj->m_strPersistFileName);
  112. hr = S_OK;
  113. }
  114. }
  115. // Else if file name is null then return the defauld save prompt ie "*.dso"
  116. else
  117. if(m_pObj->m_strPersistFileName == NULL)
  118. {
  119. static const WCHAR wszDefaultSavePrompt[] = L"*.dso";
  120. nFileNameLen = (wcslen(wszDefaultSavePrompt) + 1) * sizeof(WCHAR);
  121. try
  122. {
  123. *ppszFileName = (OLECHAR*)(LPOLESTR )g_pIMalloc->Alloc(nFileNameLen);
  124. }
  125. catch(...)
  126. {
  127. if(*ppszFileName)
  128. {
  129. g_pIMalloc->Free(*ppszFileName);
  130. *ppszFileName = NULL;
  131. }
  132. throw;
  133. }
  134. if ( ! *ppszFileName )
  135. {
  136. hr = E_OUTOFMEMORY;
  137. }
  138. else
  139. {
  140. // Copy the string
  141. wcscpy(*ppszFileName, wszDefaultSavePrompt);
  142. hr = S_FALSE;
  143. }
  144. }
  145. hr = hr == S_OK ? hr :g_pCError->PostHResult(hr,&IID_IRowsetLocate);
  146. CATCH_BLOCK_HRESULT(hr,L"IPersistFile::GetCurFile");
  147. return hr;
  148. }
  149. ///////////////////////////////////////////////////////////////////////////////////////////////
  150. //
  151. // Opens the specified file and initializes an object from the file contents.
  152. //
  153. // Returns one of the following values
  154. // HRESULT
  155. // S_OK The object was successfully loaded
  156. // S_FALSE The default save prompt was returned
  157. // E_OUTOFMEMORY The object could not be loaded due to a lack of memory
  158. // DB_E_ALREADYINITIALIZED The object is already initialized and so load failed
  159. // STG_E_INVALIDNAME The name of the file passed is invalid or null
  160. // STG_E_FILENOTFOUND The specified file not found
  161. // E_FAIL The operation failed due to some reason other than
  162. // insufficient memory
  163. ///////////////////////////////////////////////////////////////////////////////////////////////
  164. STDMETHODIMP CImpIPersistFile::Load(LPCOLESTR pszFileName, DWORD dwMode)
  165. {
  166. HRESULT hr = S_OK;
  167. CSetStructuredExceptionHandler seh;
  168. TRY_BLOCK;
  169. CAutoBlock cab(m_pObj->GetCriticalSection());
  170. // Clear previous Error Object for this thread
  171. g_pCError->ClearErrorInfo();
  172. // @devnote We ignore the 'dwMode' parameter.
  173. // Illegal to load if initialized.
  174. if (IsInitialized())
  175. hr = DB_E_ALREADYINITIALIZED;
  176. // Read all the properties from the file.
  177. // But we just store them; client has to call IDBInitialize.
  178. else
  179. if (!pszFileName)
  180. {
  181. hr = STG_E_INVALIDNAME;
  182. }
  183. else
  184. {
  185. // Read the file and initialize the object
  186. hr = ReadFromFile(pszFileName);
  187. // Clear the dirty flag, store name.
  188. if (SUCCEEDED(hr))
  189. {
  190. ClearDirtyFlag();
  191. // Save the filename, if given.
  192. if (pszFileName)
  193. {
  194. SysFreeString(m_pObj->m_strPersistFileName);
  195. m_pObj->m_strPersistFileName = Wmioledb_SysAllocString(pszFileName);
  196. }
  197. }
  198. }
  199. hr = hr == S_OK ? hr :g_pCError->PostHResult(hr,&IID_IRowsetLocate);
  200. CATCH_BLOCK_HRESULT(hr,L"IPersistFile::Load");
  201. return hr;
  202. }
  203. ///////////////////////////////////////////////////////////////////////////////////////////////
  204. //
  205. // Saves a copy of the object into the specified file.ie. saves initialization properties
  206. //
  207. // Returns one of the following values
  208. // HRESULT
  209. // S_OK The object was successfully loaded
  210. // S_FALSE The default save prompt was returned
  211. // E_OUTOFMEMORY The object could not be loaded due to a lack of memory
  212. // STG_E_INVALIDNAME The name of the file passed is invalid or null
  213. // E_FAIL The operation failed due to some reason other than
  214. // insufficient memory
  215. ///////////////////////////////////////////////////////////////////////////////////////////////
  216. STDMETHODIMP CImpIPersistFile::Save(LPCOLESTR pszFileName,BOOL fRemember)
  217. {
  218. HRESULT hr = S_OK;
  219. LPCOLESTR pFileNameTemp;
  220. CSetStructuredExceptionHandler seh;
  221. TRY_BLOCK;
  222. // Serialize the object
  223. CAutoBlock cab(m_pObj->GetCriticalSection());
  224. // Clear previous Error Object for this thread
  225. g_pCError->ClearErrorInfo();
  226. if(m_pObj->m_fDSOInitialized == FALSE)
  227. {
  228. hr = E_UNEXPECTED;
  229. }
  230. else
  231. // if file name stored AND the filename passed is NULL
  232. if(( pszFileName == NULL && m_pObj->m_strPersistFileName == NULL) ||
  233. (pszFileName != NULL && wcslen(pszFileName) == 0))
  234. hr = STG_E_INVALIDNAME;
  235. else
  236. {
  237. pFileNameTemp = pszFileName == NULL ? m_pObj->m_strPersistFileName : pszFileName;
  238. // Call this function to save properties to the file
  239. if(S_OK == (hr = WriteToFile(pFileNameTemp)))
  240. {
  241. ClearDirtyFlag();
  242. }
  243. // if the name of file is not to be remembered then free the string
  244. if(pszFileName != NULL && fRemember == TRUE)
  245. {
  246. SysFreeString(m_pObj->m_strPersistFileName);
  247. m_pObj->m_strPersistFileName = Wmioledb_SysAllocString(pszFileName);
  248. // SysFreeString(m_pObj->m_strPersistFileName);
  249. // m_pObj->m_strPersistFileName = NULL;
  250. // m_pObj->m_bIsPersitFileDirty = TRUE;
  251. }
  252. if(fRemember == FALSE)
  253. {
  254. m_pObj->m_bIsPersitFileDirty = TRUE;
  255. }
  256. }
  257. CATCH_BLOCK_HRESULT(hr,L"IPersistFile::Save");
  258. hr = hr == S_OK ? hr :g_pCError->PostHResult(hr,&IID_IRowsetLocate);
  259. return hr;
  260. }
  261. ///////////////////////////////////////////////////////////////////////////////////////////////
  262. //
  263. // Notifies the object that it can write to its file
  264. //
  265. // Returns one of the following values
  266. // HRESULT
  267. // S_OK Returned in all cases
  268. ///////////////////////////////////////////////////////////////////////////////////////////////
  269. STDMETHODIMP CImpIPersistFile::SaveCompleted(LPCOLESTR pszFileName)
  270. {
  271. HRESULT hr = S_OK;
  272. CSetStructuredExceptionHandler seh;
  273. TRY_BLOCK;
  274. // Clear previous Error Object for this thread
  275. g_pCError->ClearErrorInfo();
  276. CATCH_BLOCK_HRESULT(hr,L"IPersistFile::SaveCompleted");
  277. return hr;
  278. }
  279. ///////////////////////////////////////////////////////////////////////////////////////////////
  280. //
  281. // Function to Write initialization properties to the file
  282. //
  283. // Returns one of the following values
  284. // HRESULT
  285. // S_OK Writing into file is successful
  286. // E_FAIL Writing to file failed
  287. // Return values of GetProperties
  288. //
  289. ///////////////////////////////////////////////////////////////////////////////////////////////
  290. HRESULT CImpIPersistFile::WriteToFile(LPCOLESTR strFileName)
  291. {
  292. DBPROPSET* prgPropertySets;
  293. ULONG ulPropSets = 0;
  294. HRESULT hr = S_OK;
  295. ULONG nPropSetIndex = 0;
  296. ULONG nPropIndex = 0;
  297. WCHAR * wszKey = NULL;
  298. VARIANT varTemp;
  299. BOOL bWriteToFile = TRUE;
  300. VariantInit(&varTemp);
  301. // Get data source object's properties
  302. if(S_OK == (hr = m_pObj->m_pUtilProp->GetProperties(PROPSET_DSO,0,NULL,&ulPropSets,&prgPropertySets)))
  303. {
  304. for(nPropSetIndex = 0 ; nPropSetIndex < ulPropSets ; nPropSetIndex++)
  305. {
  306. // Only Initialization properties are stored in the file
  307. if(prgPropertySets[nPropSetIndex].guidPropertySet == DBPROPSET_DBINIT ||
  308. prgPropertySets[nPropSetIndex].guidPropertySet == DBPROPSET_WMIOLEDB_DBINIT)
  309. {
  310. // For all properties in the property set returned
  311. for(nPropIndex = 0 ; nPropIndex < prgPropertySets[nPropSetIndex].cProperties ; nPropIndex++)
  312. {
  313. bWriteToFile = TRUE;
  314. // Switch on the property ID to get the appropriate key value
  315. switch(prgPropertySets[nPropSetIndex].rgProperties[nPropIndex].dwPropertyID)
  316. {
  317. case DBPROP_INIT_DATASOURCE:
  318. wszKey = &wszDataSource[0];
  319. break;
  320. case DBPROP_AUTH_USERID:
  321. wszKey = &wszUserId[0];
  322. case DBPROP_INIT_MODE:
  323. wszKey = &wszMode[0];
  324. break;
  325. case DBPROP_INIT_PROTECTION_LEVEL:
  326. wszKey = &wszProtection[0];
  327. break;
  328. case DBPROP_INIT_IMPERSONATION_LEVEL:
  329. wszKey = &wszImpersonation[0];
  330. break;
  331. case DBPROP_WMIOLEDB_QUALIFIERS:
  332. wszKey = &wszWmioledbQualif[0];
  333. break;
  334. case DBPROP_WMIOLEDB_SYSTEMPROPERTIES:
  335. wszKey = &wszSystemProperties[0];
  336. break;
  337. case DBPROP_WMIOLEDB_AUTHORITY:
  338. wszKey = &wszWmioledbAuthority[0];
  339. break;
  340. default:
  341. bWriteToFile = FALSE;
  342. }
  343. // If everything is successful then write value to file
  344. if(bWriteToFile == TRUE)
  345. {
  346. // If property is not of type BSTR convert it into BSTR
  347. VariantClear(&varTemp);
  348. if(prgPropertySets[nPropSetIndex].rgProperties[nPropIndex].vValue.vt != VT_BSTR)
  349. {
  350. // Convert the value to string
  351. VariantChangeType(&varTemp,&prgPropertySets[nPropSetIndex].rgProperties[nPropIndex].vValue,
  352. VARIANT_NOVALUEPROP,VT_BSTR);
  353. }
  354. else
  355. {
  356. VariantCopy(&varTemp,&prgPropertySets[nPropSetIndex].rgProperties[nPropIndex].vValue);
  357. }
  358. // Call the member function to persist into a file
  359. if(!WritePrivateProfileString(WMIOLEDB,wszKey,varTemp.bstrVal,strFileName))
  360. {
  361. VariantClear(&varTemp);
  362. return E_FAIL;
  363. }
  364. }
  365. }
  366. }
  367. }
  368. //==========================================================================
  369. // Free memory we allocated to by GetProperties
  370. //==========================================================================
  371. m_pObj->m_pUtilProp->m_PropMemMgr.FreeDBPROPSET(ulPropSets, prgPropertySets);
  372. }
  373. VariantClear(&varTemp);
  374. return hr;
  375. }
  376. ///////////////////////////////////////////////////////////////////////////////////////////////
  377. //
  378. // Read properties from the file and initialize the datasource object by setting the
  379. // initialization properties
  380. //
  381. // Returns one of the following values
  382. // HRESULT
  383. // S_OK Reading from file and datasource initiallization was successful
  384. // STG_E_FILENOTFOUND The specified file not found
  385. // E_FAIL Reading from file faied , due to lack of memory
  386. // Return values of SetProperties
  387. //
  388. ///////////////////////////////////////////////////////////////////////////////////////////////
  389. HRESULT CImpIPersistFile::ReadFromFile(LPCOLESTR pszFileName)
  390. {
  391. HRESULT hr = E_FAIL;
  392. WCHAR wszFileNameFull[MAX_PATH]; // fill in with full path name
  393. WCHAR wszValue[MAX_PATH];
  394. VARIANT varProp;
  395. LONG lValue = 0;
  396. VariantInit(&varProp);
  397. // Check if file exists and also get the full absolute path
  398. if(S_OK == (hr = GetCurrentFile(pszFileName,wszFileNameFull,GENERIC_READ)))
  399. {
  400. memset(wszValue,0,MAX_PATH * sizeof(WCHAR));
  401. // Get the DBPROP_INIT_DATASOURCE from the file
  402. if(GetPrivateProfileStr(WMIOLEDB,wszDataSource,pszFileName,wszValue))
  403. {
  404. varProp.vt = VT_BSTR;
  405. varProp.bstrVal = Wmioledb_SysAllocString(wszValue);
  406. // Set DBPROP_INIT_DATASOURCE property
  407. hr = SetDBInitProp(DBPROP_INIT_DATASOURCE,DBPROPSET_DBINIT,varProp);
  408. }
  409. // Get the DBPROP_AUTH_USERID from the file
  410. if(GetPrivateProfileStr(WMIOLEDB,wszUserId,pszFileName,wszValue))
  411. {
  412. varProp.vt = VT_BSTR;
  413. varProp.bstrVal = Wmioledb_SysAllocString(wszValue);
  414. // Set DBPROP_AUTH_USERID property
  415. hr = SetDBInitProp(DBPROP_AUTH_USERID,DBPROPSET_DBINIT,varProp);
  416. }
  417. // Get the DBPROP_INIT_MODE from the file
  418. if( hr == S_OK && GetPrivateProfileLong(WMIOLEDB,wszMode,pszFileName,lValue))
  419. {
  420. VariantClear(&varProp);
  421. varProp.vt = VT_I4;
  422. varProp.lVal = lValue;
  423. // Set DBPROP_INIT_MODE property
  424. hr = SetDBInitProp(DBPROP_INIT_MODE,DBPROPSET_DBINIT,varProp);
  425. }
  426. // Get the DBPROP_INIT_PROTECTION_LEVEL from the file
  427. if( hr == S_OK && GetPrivateProfileLong(WMIOLEDB,wszProtection,pszFileName,lValue))
  428. {
  429. VariantClear(&varProp);
  430. varProp.vt = VT_I4;
  431. varProp.lVal = lValue;
  432. // Set DBPROP_INIT_PROTECTION_LEVEL property
  433. hr = SetDBInitProp(DBPROP_INIT_PROTECTION_LEVEL,DBPROPSET_DBINIT,varProp);
  434. }
  435. // Get the DBPROP_INIT_IMPERSONATION_LEVEL from the file
  436. if( hr == S_OK && GetPrivateProfileLong(WMIOLEDB,wszImpersonation,pszFileName,lValue))
  437. {
  438. VariantClear(&varProp);
  439. varProp.vt = VT_I4;
  440. varProp.lVal = lValue;
  441. // Set DBPROP_INIT_IMPERSONATION_LEVEL property
  442. hr = SetDBInitProp(DBPROP_INIT_IMPERSONATION_LEVEL,DBPROPSET_DBINIT,varProp);
  443. }
  444. // Get the DBPROP_WMIOLEDB_QUALIFIERS from the file
  445. if( hr == S_OK && GetPrivateProfileLong(WMIOLEDB,wszWmioledbQualif,pszFileName,lValue))
  446. {
  447. VariantClear(&varProp);
  448. varProp.vt = VT_I4;
  449. varProp.lVal = lValue;
  450. // Set DBPROP_WMIOLEDB_QUALIFIERS property
  451. hr = SetDBInitProp(DBPROP_WMIOLEDB_QUALIFIERS,DBPROPSET_WMIOLEDB_DBINIT,varProp);
  452. }
  453. // Get the DBPROP_WMIOLEDB_SYSTEMPROPERTIES from the file
  454. if( hr == S_OK && GetPrivateProfileLong(WMIOLEDB,wszSystemProperties,pszFileName,lValue))
  455. {
  456. VariantClear(&varProp);
  457. varProp.vt = VT_BOOL;
  458. varProp.boolVal = (BOOL)lValue;
  459. // Set DBPROP_WMIOLEDB_QUALIFIERS property
  460. hr = SetDBInitProp(DBPROP_WMIOLEDB_SYSTEMPROPERTIES,DBPROPSET_WMIOLEDB_DBINIT,varProp);
  461. }
  462. // Get the DBPROP_WMIOLEDB_AUTHORITY from the file
  463. if( hr == S_OK && GetPrivateProfileStr(WMIOLEDB,wszWmioledbAuthority,pszFileName,wszValue))
  464. {
  465. VariantClear(&varProp);
  466. varProp.vt = VT_BSTR;
  467. varProp.bstrVal = SysAllocString(wszValue);
  468. // Set DBPROP_WMIOLEDB_AUTHORITY property
  469. hr = SetDBInitProp(DBPROP_WMIOLEDB_AUTHORITY,DBPROPSET_WMIOLEDB_DBINIT,varProp);
  470. }
  471. }
  472. VariantClear(&varProp);
  473. return hr;
  474. }
  475. //////////////////////////////////////////////////////////////////////////////////////////////////////////////
  476. // Clear the dirty flag
  477. //////////////////////////////////////////////////////////////////////////////////////////////////////////////
  478. void CImpIPersistFile::ClearDirtyFlag()
  479. {
  480. m_pObj->m_bIsPersitFileDirty = FALSE;
  481. }
  482. //////////////////////////////////////////////////////////////////////////////////////////////////////////////
  483. // Check if Datasource is Initialized
  484. //////////////////////////////////////////////////////////////////////////////////////////////////////////////
  485. BOOL CImpIPersistFile::IsInitialized()
  486. {
  487. return m_pObj->m_fDSOInitialized;
  488. }
  489. //////////////////////////////////////////////////////////////////////////////////////////////////////////////
  490. // Write a key value to the File in INI file format
  491. //////////////////////////////////////////////////////////////////////////////////////////////////////////////
  492. BOOL CImpIPersistFile::WritePrivateProfileString(LPCOLESTR wszAppName,LPCOLESTR wszKeyName,LPCOLESTR wszValue,LPCOLESTR wszFileName)
  493. {
  494. BOOL fRet = FALSE;
  495. // If operation system supports UNICODE then call UNICODE version
  496. if(g_bIsAnsiOS == FALSE)
  497. {
  498. fRet = ::WritePrivateProfileStringW(wszAppName,
  499. wszKeyName,
  500. wszValue,
  501. wszFileName);
  502. }
  503. // Else convert the strings to ANSI format and call ANSI version of the function
  504. else
  505. {
  506. LPSTR szAppName = NULL;
  507. LPSTR szKeyName = NULL;
  508. LPSTR szValue = NULL;
  509. LPSTR szFileName = NULL;
  510. if( UnicodeToAnsi((WCHAR *)wszAppName,szAppName) &&
  511. UnicodeToAnsi((WCHAR *)wszKeyName,szKeyName) &&
  512. UnicodeToAnsi((WCHAR *)wszValue,szValue) &&
  513. UnicodeToAnsi((WCHAR *)wszFileName,szFileName) )
  514. {
  515. fRet = ::WritePrivateProfileStringA(szAppName,
  516. szKeyName,
  517. szValue,
  518. szFileName);
  519. }
  520. // release the memory
  521. SAFE_DELETE_ARRAY(szAppName);
  522. SAFE_DELETE_ARRAY(szKeyName);
  523. SAFE_DELETE_ARRAY(szValue);
  524. SAFE_DELETE_ARRAY(szFileName);
  525. }
  526. return fRet;
  527. }
  528. //////////////////////////////////////////////////////////////////////////////////////////////////////////////
  529. // Get a String Key value from the file
  530. // Assumption: that the the buffer size of wszValue is MAX_PATH
  531. //////////////////////////////////////////////////////////////////////////////////////////////////////////////
  532. BOOL CImpIPersistFile::GetPrivateProfileStr(LPCOLESTR wszAppName,LPCOLESTR wszKeyName,LPCOLESTR wszFileName,LPOLESTR wszValue)
  533. {
  534. BOOL fRet = FALSE;
  535. // If operation system supports UNICODE then call UNICODE version
  536. if(g_bIsAnsiOS == FALSE)
  537. {
  538. fRet = ::GetPrivateProfileStringW(wszAppName,
  539. wszKeyName,
  540. NULL,
  541. wszValue,
  542. MAX_PATH,
  543. wszFileName);
  544. }
  545. // Else convert the strings to ANSI format and call ANSI version of the function
  546. else
  547. {
  548. LPSTR szAppName = NULL;
  549. LPSTR szKeyName = NULL;
  550. LPSTR szFileName = NULL;
  551. LPSTR pszValue = NULL;
  552. pszValue = new char[MAX_PATH * DBCS_MAXWID];
  553. if( UnicodeToAnsi((WCHAR *)wszAppName,szAppName) &&
  554. UnicodeToAnsi((WCHAR *)wszKeyName,szKeyName) &&
  555. UnicodeToAnsi((WCHAR *)wszFileName,szFileName) )
  556. {
  557. if(0 != (fRet = ::GetPrivateProfileStringA(szAppName,
  558. szKeyName,
  559. NULL,
  560. pszValue,
  561. MAX_PATH * DBCS_MAXWID,
  562. szFileName)))
  563. {
  564. // convert back to UNICODE
  565. AllocateAndConvertAnsiToUnicode(pszValue,wszValue);
  566. }
  567. }
  568. // release the memory
  569. SAFE_DELETE_ARRAY(szAppName);
  570. SAFE_DELETE_ARRAY(szKeyName);
  571. SAFE_DELETE_ARRAY(pszValue);
  572. SAFE_DELETE_ARRAY(szFileName);
  573. }
  574. return fRet;
  575. }
  576. //////////////////////////////////////////////////////////////////////////////////////////////////////////////
  577. // Get a Long Key value from the file
  578. // //NTRaid:111772
  579. // 06/07/00
  580. //////////////////////////////////////////////////////////////////////////////////////////////////////////////
  581. BOOL CImpIPersistFile::GetPrivateProfileLong(LPCOLESTR wszAppName,LPCOLESTR wszKeyName,LPCOLESTR wszFileName, LONG &lValue)
  582. {
  583. BOOL fRet = FALSE;
  584. INT nDefaultVal = -1111;
  585. // If operation system supports UNICODE then call UNICODE version
  586. if(g_bIsAnsiOS == FALSE)
  587. {
  588. if(nDefaultVal != (lValue = ::GetPrivateProfileIntW(wszAppName,
  589. wszKeyName,
  590. nDefaultVal,
  591. wszFileName)))
  592. fRet = TRUE;
  593. }
  594. // Else convert the strings to ANSI format and call ANSI version of the function
  595. else
  596. {
  597. LPSTR szAppName = NULL;
  598. LPSTR szKeyName = NULL;
  599. LPSTR szFileName = NULL;
  600. if( UnicodeToAnsi((WCHAR *)wszAppName,szAppName) &&
  601. UnicodeToAnsi((WCHAR *)wszKeyName,szKeyName) &&
  602. UnicodeToAnsi((WCHAR *)wszFileName,szFileName) )
  603. {
  604. if( nDefaultVal != (lValue = ::GetPrivateProfileIntA(szAppName,
  605. szKeyName,
  606. nDefaultVal,
  607. szFileName)))
  608. fRet = TRUE;
  609. }
  610. // release the memory
  611. SAFE_DELETE_ARRAY(szAppName);
  612. SAFE_DELETE_ARRAY(szKeyName);
  613. SAFE_DELETE_ARRAY(szFileName);
  614. }
  615. return fRet;
  616. }
  617. //////////////////////////////////////////////////////////////////////////////////////////////////////////////
  618. // Function which gets the absolute path of the file, then checks whether the file exists for
  619. // the given access on the file
  620. //////////////////////////////////////////////////////////////////////////////////////////////////////////////
  621. HRESULT CImpIPersistFile::GetCurrentFile(LPCOLESTR pwszFileName,LPOLESTR wszFileNameFull,DWORD dwAccess)
  622. {
  623. //NTRaid:111771
  624. // 06/07/00
  625. HANDLE hFile = INVALID_HANDLE_VALUE;
  626. DWORD dwShareMode;
  627. DWORD dwCreationDistribution;
  628. HRESULT hr = S_OK;
  629. // Call this function to get the absolute path of the file
  630. if(S_OK == ( hr = GetAbsolutePath(pwszFileName,wszFileNameFull)))
  631. {
  632. // Set the access, share mpde and file creation flags depending on the access
  633. // for which the file is required
  634. if(dwAccess != GENERIC_READ)
  635. {
  636. dwAccess = GENERIC_READ | GENERIC_WRITE;
  637. dwShareMode = 0; // disallow other user access, FILE_SHARE_READ, _WRITE
  638. dwCreationDistribution = OPEN_ALWAYS; // will create if it doesn't exist
  639. }
  640. else
  641. {
  642. dwShareMode = FILE_SHARE_READ;
  643. dwCreationDistribution = OPEN_EXISTING;
  644. }
  645. // If operation system supports UNICODE then call UNICODE version
  646. if ( !g_bIsAnsiOS )
  647. {
  648. hFile = ::CreateFileW(wszFileNameFull, dwAccess, dwShareMode, NULL,
  649. dwCreationDistribution, FILE_ATTRIBUTE_NORMAL, NULL );
  650. }
  651. // Else convert the strings to ANSI format and call ANSI version of the function
  652. else
  653. {
  654. LPSTR szFileName;
  655. if(UnicodeToAnsi((WCHAR *)pwszFileName,szFileName))
  656. hFile = ::CreateFileA(szFileName, dwAccess, dwShareMode, NULL,
  657. dwCreationDistribution, FILE_ATTRIBUTE_NORMAL, NULL );
  658. else
  659. hr = E_FAIL;
  660. SAFE_DELETE_ARRAY(szFileName);
  661. }
  662. // If the handle is invalid then return error
  663. if (hFile == INVALID_HANDLE_VALUE)
  664. {
  665. hr = STG_E_FILENOTFOUND;
  666. }
  667. else
  668. CloseHandle(hFile);
  669. } // Get the full path
  670. return hr;
  671. }
  672. //////////////////////////////////////////////////////////////////////////////////////////////////////////////
  673. // Get the absolute path of the file
  674. //////////////////////////////////////////////////////////////////////////////////////////////////////////////
  675. HRESULT CImpIPersistFile::GetAbsolutePath(LPCOLESTR pwszFileName,LPOLESTR wszFileNameFull)
  676. {
  677. HRESULT hr = S_OK;
  678. // If operation system supports UNICODE then call UNICODE version
  679. if ( !g_bIsAnsiOS )
  680. {
  681. if(NULL == _wfullpath( wszFileNameFull, pwszFileName, MAX_PATH ))
  682. hr = E_FAIL;
  683. }
  684. // Else convert the strings to ANSI format and call ANSI version of the function
  685. else
  686. {
  687. LPSTR pszAbs = NULL;
  688. LPSTR pszRel = NULL;
  689. pszAbs = new char[MAX_PATH * DBCS_MAXWID];
  690. if ( pszAbs )
  691. {
  692. if (!UnicodeToAnsi((WCHAR *)pwszFileName, pszRel ))
  693. hr = E_FAIL;
  694. else
  695. if ( _fullpath(pszAbs, pszRel, MAX_PATH * DBCS_MAXWID) )
  696. {
  697. // Convert the output parameter back to UNICODE
  698. AllocateAndConvertAnsiToUnicode(pszAbs,wszFileNameFull);
  699. }
  700. else
  701. hr = E_FAIL;
  702. SAFE_DELETE_ARRAY(pszAbs);
  703. SAFE_DELETE_ARRAY(pszRel);
  704. }
  705. else
  706. hr = E_OUTOFMEMORY;
  707. }
  708. return hr;
  709. }
  710. //////////////////////////////////////////////////////////////////////////////////////////////////////////////
  711. // Function to set a specific Datasource Initialization property
  712. //////////////////////////////////////////////////////////////////////////////////////////////////////////////
  713. HRESULT CImpIPersistFile::SetDBInitProp(DBPROPID propId ,GUID guidPropSet,VARIANT vPropValue)
  714. {
  715. DBPROPSET rgPropertySets[1];
  716. DBPROP rgprop[1];
  717. HRESULT hr = S_OK;
  718. memset(&rgprop[0],0,sizeof(DBPROP));
  719. memset(&rgPropertySets[0],0,sizeof(DBPROPSET));
  720. rgprop[0].dwPropertyID = propId;
  721. VariantCopy(&rgprop[0].vValue,&vPropValue);
  722. rgPropertySets[0].rgProperties = &rgprop[0];
  723. rgPropertySets[0].cProperties = 1;
  724. rgPropertySets[0].guidPropertySet = guidPropSet;
  725. hr = m_pObj->m_pUtilProp->SetProperties( PROPSET_INIT,1,rgPropertySets);
  726. VariantClear(&rgprop[0].vValue);
  727. return hr;
  728. }