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.

1049 lines
25 KiB

  1. /****************************************************************************
  2. FILE: actestp.c
  3. PURPOSE:
  4. FUNCTIONS:
  5. COMMENTS:
  6. ****************************************************************************/
  7. #include <windows.h>
  8. #include <ole2.h>
  9. #include <oleext.h> // IAccessControl interface definition
  10. #include <wchar.h>
  11. #include <tchar.h>
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include "oactest.h" // header file generated by MIDL compiler
  15. void DecrementLockCount();
  16. void IncrementLockCount();
  17. void ObjectCreated();
  18. void ObjectDestroyed();
  19. const CLSID CLSID_COAccessControl_DCOM
  20. = {0x52c0e9e1,0xc0c0,0x11cf,{0xae,0xec,0x00,0xaa,0x00,0x44,0xfb,0x89}};
  21. extern "C" const CLSID CLSID_COAccessControlTest;
  22. extern ULONG g_ulFrequency;
  23. DWORD g_dwRegister;
  24. long g_fClassRegistered = FALSE;
  25. // IAccessControlTest implementation class
  26. class CAccessControlTest : public IAccessControlTest
  27. {
  28. private:
  29. IUnknown *m_pIUnknown;
  30. IPersistStream *m_pIPersistStream;
  31. IAccessControl *m_pIAccessControl;
  32. ULONG m_cRef;
  33. BOOL m_bInitialized;
  34. // destructor
  35. ~CAccessControlTest()
  36. {
  37. DecrementLockCount();
  38. ObjectDestroyed();
  39. }
  40. public:
  41. // contructor
  42. CAccessControlTest()
  43. {
  44. m_cRef = 0;
  45. m_bInitialized = FALSE;
  46. ObjectCreated();
  47. IncrementLockCount();
  48. }
  49. STDMETHODIMP_(HRESULT) QueryInterface
  50. (
  51. REFIID iid,
  52. void **ppv
  53. );
  54. STDMETHODIMP_(ULONG) AddRef();
  55. STDMETHODIMP_(ULONG) Release();
  56. STDMETHODIMP_(HRESULT) TestServer
  57. (
  58. LPSTR pszTestString
  59. );
  60. STDMETHODIMP_(HRESULT) GetClassID
  61. (
  62. CLSID *pClassID,
  63. DOUBLE *pdMillisec
  64. );
  65. STDMETHODIMP_(HRESULT) InitNewACL
  66. (
  67. DOUBLE*pdMillisec
  68. );
  69. STDMETHODIMP_(HRESULT) LoadACL
  70. (
  71. LPCSTR pszFilename,
  72. DOUBLE *pdMillisec
  73. );
  74. STDMETHODIMP_(HRESULT) SaveACL
  75. (
  76. LPCSTR pszFilename,
  77. BOOL fClearDirty,
  78. ULONG *pulBytesWritten,
  79. DOUBLE *pdMillisec
  80. );
  81. STDMETHODIMP_(HRESULT) GetSizeMax
  82. (
  83. ULONG *pcdSize,
  84. DOUBLE *pdMillisec
  85. );
  86. STDMETHODIMP_(HRESULT) IsDirty
  87. (
  88. DOUBLE *pdMillisec
  89. );
  90. STDMETHODIMP_(HRESULT) GrantAccessRights
  91. (
  92. ULONG cCount,
  93. E_ACCESS_REQUEST *pAccessRequestList,
  94. DOUBLE *pdMillisec
  95. );
  96. STDMETHODIMP_(HRESULT) DenyAccessRights
  97. (
  98. ULONG cCount,
  99. E_ACCESS_REQUEST pAccessRequestList[],
  100. DOUBLE *pdMillisec
  101. );
  102. STDMETHODIMP_(HRESULT) SetAccessRights
  103. (
  104. ULONG cCount,
  105. E_ACCESS_REQUEST pAccessRequestList[],
  106. DOUBLE *pdMillisec
  107. );
  108. STDMETHODIMP_(HRESULT) ReplaceAllAccessRights
  109. (
  110. ULONG cCount,
  111. E_EXPLICIT_ACCESS pExplicitAccessList[],
  112. DOUBLE *pdMillisec
  113. );
  114. STDMETHODIMP_(HRESULT) RevokeExplicitAccessRights
  115. (
  116. ULONG cCount,
  117. E_TRUSTEE pTrustee[],
  118. DOUBLE *pdMillisec
  119. );
  120. STDMETHODIMP_(HRESULT) IsAccessPermitted
  121. (
  122. E_TRUSTEE *pTrustee,
  123. DWORD grfAccessPermissions,
  124. DOUBLE *pdMillisec
  125. );
  126. STDMETHODIMP_(HRESULT) GetEffectiveAccessRights
  127. (
  128. E_TRUSTEE *pTrustee,
  129. DWORD *pdwRights,
  130. DOUBLE *pdMillisec
  131. );
  132. STDMETHODIMP_(HRESULT) GetExplicitAccessRights
  133. (
  134. ULONG *pcCount,
  135. PE_EXPLICIT_ACCESS *ppExplicitAccessList,
  136. DOUBLE *pdMillisec
  137. );
  138. STDMETHODIMP_(HRESULT) RevertAccessRights
  139. (
  140. );
  141. STDMETHODIMP_(HRESULT) CommitAccessRights
  142. (
  143. DWORD grfCommitFlags
  144. );
  145. }; // CAccessControlTest
  146. //+-------------------------------------------------------------------------
  147. //
  148. // Method: CAccessControlTest::AddRef, public
  149. //
  150. // Synopsis: Increment reference count
  151. //
  152. // See Also: IUnknown::AddRef
  153. //
  154. //--------------------------------------------------------------------------
  155. STDMETHODIMP_(ULONG)
  156. CAccessControlTest::AddRef()
  157. {
  158. InterlockedIncrement((long *) &m_cRef);
  159. return m_cRef;
  160. }
  161. //+-------------------------------------------------------------------------
  162. //
  163. // Method: CAccessControlTest::Release, public
  164. //
  165. // Synopsis: Decrement DLL reference count
  166. //
  167. // Notes: After the m_cRef is decremented, the object may be
  168. // deleted by another thread. In order to make this code safe
  169. // for multiple threads, we have to access the object state
  170. // before decrementing m_cRef.
  171. //
  172. // See Also: IUnknown::Release.
  173. //
  174. //--------------------------------------------------------------------------
  175. STDMETHODIMP_(ULONG)
  176. CAccessControlTest::Release()
  177. {
  178. unsigned long count;
  179. count = m_cRef - 1;
  180. if(InterlockedDecrement((long *) &m_cRef) == 0)
  181. {
  182. count = 0;
  183. if(m_bInitialized)
  184. {
  185. m_pIUnknown->Release();
  186. m_pIPersistStream->Release();
  187. m_pIAccessControl->Release();
  188. }
  189. delete this;
  190. }
  191. return count;
  192. }
  193. //+-------------------------------------------------------------------------
  194. //
  195. // Method: CAccessControlTest::QueryInterface, public
  196. //
  197. // Synopsis: Query for an interface on the class factory.
  198. //
  199. // See Also: IUnknown:QueryInterface
  200. //
  201. //--------------------------------------------------------------------------
  202. STDMETHODIMP_(HRESULT)
  203. CAccessControlTest::QueryInterface
  204. (
  205. REFIID iid,
  206. void **ppv
  207. )
  208. {
  209. HRESULT hr;
  210. if (!m_bInitialized)
  211. {
  212. hr = CoCreateInstance( CLSID_COAccessControl_DCOM
  213. , NULL
  214. , CLSCTX_INPROC_SERVER
  215. , IID_IUnknown
  216. , (void **)&m_pIUnknown);
  217. if(FAILED(hr))
  218. {
  219. printf("Failed to create an instance of COAccessControl\n.");
  220. return hr;
  221. }
  222. hr = m_pIUnknown->QueryInterface(IID_IPersistStream, (void **)&m_pIPersistStream);
  223. if(FAILED(hr))
  224. {
  225. printf("Failed to query for the IPersistStream Interface.\n");
  226. return hr;
  227. }
  228. hr = m_pIUnknown->QueryInterface(IID_IAccessControl, (void **)&m_pIAccessControl);
  229. if(FAILED(hr))
  230. {
  231. printf("Failed to query for the IAccessControl interface.\n");
  232. return hr;
  233. }
  234. m_bInitialized = TRUE;
  235. }
  236. if ( IsEqualGUID( iid, IID_IUnknown ) )
  237. {
  238. *ppv = (IUnknown *) this;
  239. ((IUnknown *)(*ppv))->AddRef();
  240. hr = S_OK;
  241. }
  242. else if (IsEqualGUID( iid, IID_IAccessControlTest))
  243. {
  244. *ppv = (IUnknown *)(IAccessControlTest *)this;
  245. ((IAccessControlTest *)(*ppv))->AddRef();
  246. hr = S_OK;
  247. }
  248. else
  249. {
  250. *ppv = 0;
  251. hr = E_NOINTERFACE;
  252. }
  253. return hr;
  254. }
  255. STDMETHODIMP_(HRESULT) CAccessControlTest::TestServer
  256. (
  257. LPSTR pszTestString
  258. )
  259. {
  260. printf("The test string is: %s\n", pszTestString);
  261. return S_OK;
  262. }
  263. STDMETHODIMP_(HRESULT) CAccessControlTest::GetClassID
  264. (
  265. CLSID *pClassID,
  266. DOUBLE *pdMillisec
  267. )
  268. {
  269. HRESULT localhr;
  270. CLSID clsid;
  271. LARGE_INTEGER liCount1;
  272. LARGE_INTEGER liCount2;
  273. QueryPerformanceCounter(&liCount1);
  274. localhr = m_pIPersistStream->GetClassID(&clsid);
  275. QueryPerformanceCounter(&liCount2);
  276. // Assign calues to the out parameters
  277. *pClassID = clsid;
  278. *pdMillisec = ((DOUBLE)(liCount2.LowPart) - (DOUBLE)(liCount1.LowPart)) / (DOUBLE)(g_ulFrequency) * 1000.0;
  279. return localhr;
  280. }
  281. STDMETHODIMP_(HRESULT) CAccessControlTest::InitNewACL
  282. (
  283. DOUBLE *pdMillisec
  284. )
  285. {
  286. HRESULT localhr;
  287. LARGE_INTEGER liCount1;
  288. LARGE_INTEGER liCount2;
  289. QueryPerformanceCounter(&liCount1);
  290. localhr = m_pIPersistStream->Load(NULL);
  291. QueryPerformanceCounter(&liCount2);
  292. // Assign calues to the out parameters
  293. *pdMillisec = ((DOUBLE)(liCount2.LowPart) - (DOUBLE)(liCount1.LowPart)) / (DOUBLE)(g_ulFrequency) * 1000.0;
  294. return localhr;
  295. }
  296. STDMETHODIMP_(HRESULT) CAccessControlTest::LoadACL
  297. (
  298. LPCSTR pszFilename,
  299. DOUBLE *pdMillisec
  300. )
  301. {
  302. HRESULT localhr;
  303. IStream *pIStream = NULL;
  304. HANDLE FileHandle;
  305. DWORD dwFileSize;
  306. void *pvBuffer;
  307. LARGE_INTEGER liCount1;
  308. LARGE_INTEGER liCount2;
  309. // Open the file specified by the client
  310. FileHandle = CreateFileA( pszFilename
  311. , GENERIC_READ
  312. , FILE_SHARE_READ | FILE_SHARE_WRITE
  313. , NULL
  314. , OPEN_EXISTING
  315. , FILE_ATTRIBUTE_NORMAL
  316. , NULL );
  317. if( FileHandle == INVALID_HANDLE_VALUE )
  318. {
  319. printf("Cannot open file %s.\n", pszFilename);
  320. *pdMillisec = 0;
  321. return HRESULT_FROM_WIN32(GetLastError());
  322. }
  323. // Get the size of the file
  324. dwFileSize = GetFileSize(FileHandle, NULL);
  325. // Create a buffer to hold the data in the file
  326. pvBuffer = CoTaskMemAlloc(dwFileSize);
  327. DWORD dwBytesRead;
  328. // Read the data of the opened file into a buffer
  329. if(!ReadFile( FileHandle
  330. , pvBuffer
  331. , dwFileSize
  332. , &dwBytesRead
  333. , NULL ))
  334. {
  335. return HRESULT_FROM_WIN32(GetLastError());
  336. }
  337. // Once we have read the file data into a buffer, we can
  338. // close the file handle
  339. CloseHandle( FileHandle);
  340. // Create a stream on the the buffer
  341. localhr = CreateStreamOnHGlobal(NULL, TRUE, &pIStream);
  342. if (FAILED(localhr))
  343. {
  344. printf("Cannot create stream object.\n");
  345. CoTaskMemFree(pvBuffer);
  346. *pdMillisec = 0;
  347. return localhr;
  348. }
  349. // Load the data in the buffer into the IStream object
  350. localhr = pIStream->Write(pvBuffer, dwFileSize, NULL);
  351. // Release the local buffer
  352. CoTaskMemFree(pvBuffer);
  353. if (FAILED(localhr))
  354. {
  355. printf("Failed to load data into stream object.\n");
  356. *pdMillisec = 0;
  357. return localhr;
  358. }
  359. // Rewind the stream pointer the starting position
  360. LARGE_INTEGER li;
  361. li.LowPart = 0;
  362. li.HighPart = 0;
  363. localhr = pIStream->Seek(li, STREAM_SEEK_SET, NULL);
  364. if (FAILED(localhr))
  365. {
  366. printf("Failed to set the stream pointer to the starting position.\n");
  367. *pdMillisec = 0;
  368. return localhr;
  369. }
  370. QueryPerformanceCounter(&liCount1);
  371. localhr = m_pIPersistStream->Load(pIStream);
  372. QueryPerformanceCounter(&liCount2);
  373. // Assign calues to the out parameters
  374. *pdMillisec = ((DOUBLE)(liCount2.LowPart) - (DOUBLE)(liCount1.LowPart)) / (DOUBLE)(g_ulFrequency) * 1000.0;
  375. // Release the local stream pointer
  376. pIStream->Release();
  377. return localhr;
  378. }
  379. STDMETHODIMP_(HRESULT) CAccessControlTest::SaveACL
  380. (
  381. LPCSTR pszFilename,
  382. BOOL fClearDirty,
  383. ULONG *pulNumOfBytesWritten,
  384. DOUBLE *pdMillisec
  385. )
  386. {
  387. HRESULT localhr;
  388. IStream *pIStream = NULL;
  389. HANDLE FileHandle;
  390. DWORD dwFileSize;
  391. void *pvBuffer;
  392. STATSTG StreamInfo;
  393. LARGE_INTEGER liCount1;
  394. LARGE_INTEGER liCount2;
  395. // Create a stream on the the buffer
  396. localhr = CreateStreamOnHGlobal(NULL, TRUE, &pIStream);
  397. if (FAILED(localhr))
  398. {
  399. printf("Cannot create stream object.\n");
  400. *pdMillisec = 0;
  401. return localhr;
  402. }
  403. QueryPerformanceCounter(&liCount1);
  404. localhr = m_pIPersistStream->Save(pIStream, fClearDirty);
  405. QueryPerformanceCounter(&liCount2);
  406. if(FAILED(localhr))
  407. {
  408. *pdMillisec = ((DOUBLE)(liCount2.LowPart) - (DOUBLE)(liCount1.LowPart)) / (DOUBLE)(g_ulFrequency) * 1000.0;
  409. return localhr;
  410. }
  411. // Get the number of bytes written to the stream
  412. localhr = pIStream->Stat(&StreamInfo, STATFLAG_NONAME);
  413. if (FAILED(localhr))
  414. {
  415. printf("Unable to get information about the local stream.\n");
  416. *pdMillisec = ((DOUBLE)(liCount2.LowPart) - (DOUBLE)(liCount1.LowPart)) / (DOUBLE)(g_ulFrequency) * 1000.0;
  417. return localhr;
  418. }
  419. dwFileSize = StreamInfo.cbSize.LowPart;
  420. LARGE_INTEGER liOffset;
  421. liOffset.QuadPart = Int32x32To64(dwFileSize, -1);
  422. pIStream->Seek(liOffset,STREAM_SEEK_CUR, NULL);
  423. // Allocate a buffer to store the data in the stream
  424. pvBuffer = CoTaskMemAlloc(dwFileSize);
  425. // Write the stream
  426. localhr = pIStream->Read(pvBuffer, dwFileSize, NULL);
  427. if(FAILED(localhr))
  428. {
  429. printf("Unable to write data from stream to buffer.\n");
  430. CoTaskMemFree(pvBuffer);
  431. *pdMillisec = ((DOUBLE)(liCount2.LowPart) - (DOUBLE)(liCount1.LowPart)) / (DOUBLE)(g_ulFrequency) * 1000.0;
  432. return localhr;
  433. }
  434. // Open the file specified by the client
  435. FileHandle = CreateFileA( pszFilename
  436. , GENERIC_WRITE | GENERIC_READ
  437. , FILE_SHARE_READ | FILE_SHARE_WRITE
  438. , NULL
  439. , CREATE_ALWAYS
  440. , FILE_ATTRIBUTE_NORMAL
  441. , NULL );
  442. if( FileHandle == INVALID_HANDLE_VALUE )
  443. {
  444. printf("Cannot open file %s.\n", pszFilename);
  445. CoTaskMemFree(pvBuffer);
  446. *pdMillisec = ((DOUBLE)(liCount2.LowPart) - (DOUBLE)(liCount1.LowPart)) / (DOUBLE)(g_ulFrequency) * 1000.0;
  447. return HRESULT_FROM_WIN32(GetLastError());
  448. }
  449. // Read the data of the opened file into a buffer
  450. if(!WriteFile( FileHandle
  451. , pvBuffer
  452. , dwFileSize
  453. , pulNumOfBytesWritten
  454. , NULL ))
  455. {
  456. DWORD dwLastError;
  457. dwLastError = GetLastError();
  458. printf("Write failed with error code %x.", dwLastError);
  459. }
  460. // Flush the file buffers
  461. FlushFileBuffers(FileHandle);
  462. // Close the file handle
  463. CloseHandle(FileHandle);
  464. CoTaskMemFree(pvBuffer);
  465. // Assign calues to the out parameters
  466. *pdMillisec = ((DOUBLE)(liCount2.LowPart) - (DOUBLE)(liCount1.LowPart)) / (DOUBLE)(g_ulFrequency) * 1000.0;
  467. *pulNumOfBytesWritten = dwFileSize;
  468. // Release the local stream pointer
  469. pIStream->Release();
  470. return localhr;
  471. }
  472. STDMETHODIMP_(HRESULT) CAccessControlTest::GetSizeMax
  473. (
  474. ULONG *pcdSize,
  475. DOUBLE *pdMillisec
  476. )
  477. {
  478. ULARGE_INTEGER uliSize;
  479. HRESULT localhr;
  480. LARGE_INTEGER liCount1;
  481. LARGE_INTEGER liCount2;
  482. QueryPerformanceCounter(&liCount1);
  483. localhr = m_pIPersistStream->GetSizeMax(&uliSize);
  484. QueryPerformanceCounter(&liCount2);
  485. // Assign calues to the out parameters
  486. *pcdSize = uliSize.LowPart;
  487. *pdMillisec = ((DOUBLE)(liCount2.LowPart) - (DOUBLE)(liCount1.LowPart)) / (DOUBLE)(g_ulFrequency) * 1000.0;
  488. return localhr;
  489. }
  490. STDMETHODIMP_(HRESULT) CAccessControlTest::IsDirty
  491. (
  492. DOUBLE *pdMillisec
  493. )
  494. {
  495. HRESULT localhr;
  496. LARGE_INTEGER liCount1;
  497. LARGE_INTEGER liCount2;
  498. QueryPerformanceCounter(&liCount1);
  499. localhr = m_pIPersistStream->IsDirty();
  500. QueryPerformanceCounter(&liCount2);
  501. // Assign calues to the out parameters
  502. *pdMillisec = ((DOUBLE)(liCount2.LowPart) - (DOUBLE)(liCount1.LowPart)) / (DOUBLE)(g_ulFrequency) * 1000.0;
  503. return localhr;
  504. }
  505. STDMETHODIMP_(HRESULT) CAccessControlTest::GrantAccessRights
  506. (
  507. ULONG cCount,
  508. E_ACCESS_REQUEST pAccessRequestList[],
  509. DOUBLE *pdMillisec
  510. )
  511. {
  512. HRESULT localhr;
  513. LARGE_INTEGER liCount1;
  514. LARGE_INTEGER liCount2;
  515. QueryPerformanceCounter(&liCount1);
  516. localhr = m_pIAccessControl->GrantAccessRights(cCount, (ACCESS_REQUEST_W *)pAccessRequestList);
  517. QueryPerformanceCounter(&liCount2);
  518. // Assign calues to the out parameters
  519. *pdMillisec = ((DOUBLE)(liCount2.LowPart) - (DOUBLE)(liCount1.LowPart)) / (DOUBLE)(g_ulFrequency) * 1000.0;
  520. return localhr;
  521. }
  522. STDMETHODIMP_(HRESULT) CAccessControlTest::SetAccessRights
  523. (
  524. ULONG cCount,
  525. E_ACCESS_REQUEST pAccessRequestList[],
  526. DOUBLE *pdMillisec
  527. )
  528. {
  529. HRESULT localhr;
  530. LARGE_INTEGER liCount1;
  531. LARGE_INTEGER liCount2;
  532. QueryPerformanceCounter(&liCount1);
  533. localhr = m_pIAccessControl->SetAccessRights(cCount, (ACCESS_REQUEST_W *)pAccessRequestList);
  534. QueryPerformanceCounter(&liCount2);
  535. // Assign calues to the out parameters
  536. *pdMillisec = ((DOUBLE)(liCount2.LowPart) - (DOUBLE)(liCount1.LowPart)) / (DOUBLE)(g_ulFrequency) * 1000.0;
  537. return localhr;
  538. }
  539. STDMETHODIMP_(HRESULT) CAccessControlTest::DenyAccessRights
  540. (
  541. ULONG cCount,
  542. E_ACCESS_REQUEST pAccessRequestList[],
  543. DOUBLE *pdMillisec
  544. )
  545. {
  546. HRESULT localhr;
  547. LARGE_INTEGER liCount1;
  548. LARGE_INTEGER liCount2;
  549. QueryPerformanceCounter(&liCount1);
  550. localhr = m_pIAccessControl->DenyAccessRights(cCount, (ACCESS_REQUEST_W *)pAccessRequestList);
  551. QueryPerformanceCounter(&liCount2);
  552. // Assign calues to the out parameters
  553. *pdMillisec = ((DOUBLE)(liCount2.LowPart) - (DOUBLE)(liCount1.LowPart)) / (DOUBLE)(g_ulFrequency) * 1000.0;
  554. return localhr;
  555. }
  556. STDMETHODIMP_(HRESULT) CAccessControlTest::ReplaceAllAccessRights
  557. (
  558. ULONG cCount,
  559. E_EXPLICIT_ACCESS pExplicitAccessList[],
  560. DOUBLE *pdMillisec
  561. )
  562. {
  563. HRESULT localhr;
  564. LARGE_INTEGER liCount1;
  565. LARGE_INTEGER liCount2;
  566. QueryPerformanceCounter(&liCount1);
  567. localhr = m_pIAccessControl->ReplaceAllAccessRights(cCount, (EXPLICIT_ACCESS_W *)pExplicitAccessList);
  568. QueryPerformanceCounter(&liCount2);
  569. // Assign calues to the out parameters
  570. *pdMillisec = ((DOUBLE)(liCount2.LowPart) - (DOUBLE)(liCount1.LowPart)) / (DOUBLE)(g_ulFrequency) * 1000.0;
  571. return localhr;
  572. }
  573. STDMETHODIMP_(HRESULT) CAccessControlTest::RevokeExplicitAccessRights
  574. (
  575. ULONG cCount,
  576. E_TRUSTEE pTrustee[],
  577. DOUBLE *pdMillisec
  578. )
  579. {
  580. char *pszClientName;
  581. RPC_STATUS status;
  582. HRESULT localhr;
  583. LARGE_INTEGER liCount1;
  584. LARGE_INTEGER liCount2;
  585. QueryPerformanceCounter(&liCount1);
  586. localhr = m_pIAccessControl->RevokeExplicitAccessRights(cCount, (TRUSTEE_W *)pTrustee);
  587. QueryPerformanceCounter(&liCount2);
  588. // Assign calues to the out parameters
  589. *pdMillisec = ((DOUBLE)(liCount2.LowPart) - (DOUBLE)(liCount1.LowPart)) / (DOUBLE)(g_ulFrequency) * 1000.0;
  590. return localhr;
  591. }
  592. STDMETHODIMP_(HRESULT) CAccessControlTest::IsAccessPermitted
  593. (
  594. E_TRUSTEE *pTrustee,
  595. DWORD grfAccessPermissions,
  596. DOUBLE *pdMillisec
  597. )
  598. {
  599. HRESULT localhr;
  600. LARGE_INTEGER liCount1;
  601. LARGE_INTEGER liCount2;
  602. QueryPerformanceCounter(&liCount1);
  603. localhr = m_pIAccessControl->IsAccessPermitted((TRUSTEE_W *)pTrustee, grfAccessPermissions);
  604. QueryPerformanceCounter(&liCount2);
  605. // Assign calues to the out parameters
  606. *pdMillisec = ((DOUBLE)(liCount2.LowPart) - (DOUBLE)(liCount1.LowPart)) / (DOUBLE)(g_ulFrequency) * 1000.0;
  607. return localhr;
  608. }
  609. STDMETHODIMP_(HRESULT) CAccessControlTest::GetEffectiveAccessRights
  610. (
  611. E_TRUSTEE *pTrustee,
  612. DWORD *pdwRights,
  613. DOUBLE *pdMillisec
  614. )
  615. {
  616. HRESULT localhr;
  617. LARGE_INTEGER liCount1;
  618. LARGE_INTEGER liCount2;
  619. QueryPerformanceCounter(&liCount1);
  620. localhr = m_pIAccessControl->GetEffectiveAccessRights((TRUSTEE_W *)pTrustee, pdwRights);
  621. QueryPerformanceCounter(&liCount2);
  622. // Assign calues to the out parameters
  623. *pdMillisec = ((DOUBLE)(liCount2.LowPart) - (DOUBLE)(liCount1.LowPart)) / (DOUBLE)(g_ulFrequency) * 1000.0;
  624. return localhr;
  625. }
  626. STDMETHODIMP_(HRESULT) CAccessControlTest::GetExplicitAccessRights
  627. (
  628. ULONG *pcCount,
  629. PE_EXPLICIT_ACCESS *ppExplicitAccessList,
  630. DOUBLE *pdMillisec
  631. )
  632. {
  633. HRESULT localhr;
  634. LARGE_INTEGER liCount1;
  635. LARGE_INTEGER liCount2;
  636. QueryPerformanceCounter(&liCount1);
  637. localhr = m_pIAccessControl->GetExplicitAccessRights(pcCount, (PEXPLICIT_ACCESS_W *)ppExplicitAccessList);
  638. QueryPerformanceCounter(&liCount2);
  639. // Assign calues to the out parameters
  640. *pdMillisec = ((DOUBLE)(liCount2.LowPart) - (DOUBLE)(liCount1.LowPart)) / (DOUBLE)(g_ulFrequency) * 1000.0;
  641. return localhr;
  642. }
  643. STDMETHODIMP_(HRESULT) CAccessControlTest::RevertAccessRights
  644. (
  645. )
  646. {
  647. return m_pIAccessControl->RevertAccessRights();
  648. }
  649. STDMETHODIMP_(HRESULT) CAccessControlTest::CommitAccessRights
  650. (
  651. DWORD grfCommitFlags
  652. )
  653. {
  654. return m_pIAccessControl->CommitAccessRights(grfCommitFlags);
  655. }
  656. class CACTestClassFactory : public IClassFactory
  657. {
  658. private:
  659. unsigned long m_cRef;
  660. //destructor
  661. ~CACTestClassFactory()
  662. {
  663. ObjectDestroyed();
  664. }
  665. public:
  666. //constructor
  667. CACTestClassFactory()
  668. {
  669. m_cRef = 0;
  670. ObjectCreated();
  671. }
  672. HRESULT STDMETHODCALLTYPE QueryInterface(
  673. REFIID iid,
  674. void **ppv);
  675. ULONG STDMETHODCALLTYPE AddRef();
  676. ULONG STDMETHODCALLTYPE Release();
  677. HRESULT STDMETHODCALLTYPE CreateInstance(
  678. IUnknown *punkOuter,
  679. REFIID riid,
  680. void **ppv);
  681. HRESULT STDMETHODCALLTYPE LockServer(
  682. BOOL fLock );
  683. };
  684. //+-------------------------------------------------------------------------
  685. //
  686. // Method: CACTestClassFactory::AddRef, public
  687. //
  688. // Synopsis: Increment DLL reference counts
  689. //
  690. // See Also: IUnknown::AddRef
  691. //
  692. //--------------------------------------------------------------------------
  693. ULONG STDMETHODCALLTYPE
  694. CACTestClassFactory::AddRef()
  695. {
  696. InterlockedIncrement((long *) &m_cRef);
  697. return m_cRef;
  698. }
  699. //+-------------------------------------------------------------------------
  700. //
  701. // Method: CACTestClassFactory::CreateInstance, public
  702. //
  703. // Synopsis: Create an instance of CAccessControlTest.
  704. //
  705. // See Also: IClassFactory::CreateInstance
  706. //
  707. //--------------------------------------------------------------------------
  708. HRESULT STDMETHODCALLTYPE
  709. CACTestClassFactory::CreateInstance
  710. (
  711. IUnknown *punkOuter,
  712. REFIID riid,
  713. void **ppv
  714. )
  715. {
  716. HRESULT hr;
  717. CAccessControlTest *pACTest;
  718. if(punkOuter != 0)
  719. {
  720. //The CAccessControlTest class doesn't support aggregation.
  721. return CLASS_E_NOAGGREGATION;
  722. }
  723. pACTest = new CAccessControlTest();
  724. if(pACTest != 0)
  725. {
  726. hr = pACTest->QueryInterface(riid, ppv);
  727. }
  728. else
  729. {
  730. hr = E_OUTOFMEMORY;
  731. *ppv = 0;
  732. }
  733. return hr;
  734. }
  735. //+-------------------------------------------------------------------------
  736. //
  737. // Method: CACTestClassFactory::LockServer, public
  738. //
  739. // Synopsis: Lock the server in memory (by adding an extra reference)
  740. //
  741. // Notes: The class factory will be revoked when the lock count
  742. // is decremented to zero. LockServer(TRUE) will increment the
  743. // lock count and ensure that the class factory will
  744. // not be revoked.
  745. //
  746. // See Also: IClassFactory::LockServer
  747. //
  748. //--------------------------------------------------------------------------
  749. HRESULT STDMETHODCALLTYPE
  750. CACTestClassFactory::LockServer(
  751. BOOL fLock )
  752. {
  753. if (fLock == TRUE)
  754. IncrementLockCount();
  755. else
  756. DecrementLockCount();
  757. return S_OK;
  758. }
  759. //+-------------------------------------------------------------------------
  760. //
  761. // Method: CACTestClassFactory::QueryInterface, public
  762. //
  763. // Synopsis: Query for an interface on the class factory.
  764. //
  765. // See Also: IUnknown::QueryInterface
  766. //
  767. //--------------------------------------------------------------------------
  768. HRESULT STDMETHODCALLTYPE
  769. CACTestClassFactory::QueryInterface (
  770. REFIID iid,
  771. void **ppv )
  772. {
  773. HRESULT hr;
  774. if ( IsEqualGUID( iid, IID_IUnknown) ||
  775. IsEqualGUID( iid, IID_IClassFactory ) )
  776. {
  777. *ppv = this;
  778. ((IUnknown *)(*ppv))->AddRef();
  779. hr = S_OK;
  780. }
  781. else
  782. {
  783. *ppv = 0;
  784. hr = E_NOINTERFACE;
  785. }
  786. return hr;
  787. }
  788. //+-------------------------------------------------------------------------
  789. //
  790. // Method: CACTestClassFactory::Release, public
  791. //
  792. // Synopsis: Decrement DLL reference count
  793. //
  794. // See Also: IUnknown::Release
  795. //
  796. //--------------------------------------------------------------------------
  797. ULONG STDMETHODCALLTYPE
  798. CACTestClassFactory::Release()
  799. {
  800. unsigned long count;
  801. count = m_cRef - 1;
  802. if(InterlockedDecrement((long *) &m_cRef) == 0)
  803. {
  804. count = 0;
  805. delete this;
  806. }
  807. return count;
  808. }
  809. //+-------------------------------------------------------------------------
  810. //
  811. // Function: RegisterClassFactory.
  812. //
  813. // Synopsis: Register the class factory if it is not currently registered.
  814. //
  815. //--------------------------------------------------------------------------
  816. HRESULT RegisterClassFactory()
  817. {
  818. HRESULT hr;
  819. CACTestClassFactory *pClassFactory;
  820. if(InterlockedExchange(&g_fClassRegistered, TRUE) == FALSE)
  821. {
  822. pClassFactory = new CACTestClassFactory;
  823. if(pClassFactory != 0)
  824. {
  825. hr = CoRegisterClassObject(CLSID_COAccessControlTest,
  826. (IUnknown *) pClassFactory,
  827. CLSCTX_LOCAL_SERVER,
  828. REGCLS_MULTIPLEUSE,
  829. &g_dwRegister);
  830. }
  831. else
  832. {
  833. hr = E_OUTOFMEMORY;
  834. }
  835. }
  836. else
  837. {
  838. hr = S_OK;
  839. }
  840. return hr;
  841. }
  842. //+-------------------------------------------------------------------------
  843. //
  844. // Function: RevokeClassFactory.
  845. //
  846. // Synopsis: Revoke the registered class factories if they have not
  847. // already been revoked.
  848. //
  849. //--------------------------------------------------------------------------
  850. HRESULT RevokeClassFactory()
  851. {
  852. HRESULT hr;
  853. if(InterlockedExchange(&g_fClassRegistered, FALSE) == TRUE)
  854. {
  855. hr = CoRevokeClassObject(g_dwRegister);
  856. }
  857. else
  858. {
  859. hr = S_OK;
  860. }
  861. return hr;
  862. }