wxutil.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. //------------------------------------------------------------------------------
  2. // File: WXUtil.cpp
  3. //
  4. // Desc: DirectShow base classes - implements helper classes for building
  5. // multimedia filters.
  6. //
  7. // Copyright (c) 1992-2001 Microsoft Corporation. All rights reserved.
  8. //------------------------------------------------------------------------------
  9. #include <streams.h>
  10. #define STRSAFE_NO_DEPRECATE
  11. #include <strsafe.h>
  12. // --- CAMEvent -----------------------
  13. CAMEvent::CAMEvent(BOOL fManualReset, __inout_opt HRESULT *phr)
  14. {
  15. m_hEvent = CreateEvent(NULL, fManualReset, FALSE, NULL);
  16. if (NULL == m_hEvent) {
  17. if (NULL != phr && SUCCEEDED(*phr)) {
  18. *phr = E_OUTOFMEMORY;
  19. }
  20. }
  21. }
  22. CAMEvent::CAMEvent(__inout_opt HRESULT *phr)
  23. {
  24. m_hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  25. if (NULL == m_hEvent) {
  26. if (NULL != phr && SUCCEEDED(*phr)) {
  27. *phr = E_OUTOFMEMORY;
  28. }
  29. }
  30. }
  31. CAMEvent::~CAMEvent()
  32. {
  33. if (m_hEvent) {
  34. EXECUTE_ASSERT(CloseHandle(m_hEvent));
  35. }
  36. }
  37. // --- CAMMsgEvent -----------------------
  38. // One routine. The rest is handled in CAMEvent
  39. CAMMsgEvent::CAMMsgEvent(__inout_opt HRESULT *phr) : CAMEvent(FALSE, phr)
  40. {
  41. }
  42. BOOL CAMMsgEvent::WaitMsg(DWORD dwTimeout)
  43. {
  44. // wait for the event to be signalled, or for the
  45. // timeout (in MS) to expire. allow SENT messages
  46. // to be processed while we wait
  47. DWORD dwWait;
  48. DWORD dwStartTime;
  49. // set the waiting period.
  50. DWORD dwWaitTime = dwTimeout;
  51. // the timeout will eventually run down as we iterate
  52. // processing messages. grab the start time so that
  53. // we can calculate elapsed times.
  54. if (dwWaitTime != INFINITE) {
  55. dwStartTime = timeGetTime();
  56. }
  57. do {
  58. dwWait = MsgWaitForMultipleObjects(1,&m_hEvent,FALSE, dwWaitTime, QS_SENDMESSAGE);
  59. if (dwWait == WAIT_OBJECT_0 + 1) {
  60. MSG Message;
  61. PeekMessage(&Message,NULL,0,0,PM_NOREMOVE);
  62. // If we have an explicit length of time to wait calculate
  63. // the next wake up point - which might be now.
  64. // If dwTimeout is INFINITE, it stays INFINITE
  65. if (dwWaitTime != INFINITE) {
  66. DWORD dwElapsed = timeGetTime()-dwStartTime;
  67. dwWaitTime =
  68. (dwElapsed >= dwTimeout)
  69. ? 0 // wake up with WAIT_TIMEOUT
  70. : dwTimeout-dwElapsed;
  71. }
  72. }
  73. } while (dwWait == WAIT_OBJECT_0 + 1);
  74. // return TRUE if we woke on the event handle,
  75. // FALSE if we timed out.
  76. return (dwWait == WAIT_OBJECT_0);
  77. }
  78. // --- CAMThread ----------------------
  79. CAMThread::CAMThread(__inout_opt HRESULT *phr)
  80. : m_EventSend(TRUE, phr), // must be manual-reset for CheckRequest()
  81. m_EventComplete(FALSE, phr)
  82. {
  83. m_hThread = NULL;
  84. }
  85. CAMThread::~CAMThread() {
  86. Close();
  87. }
  88. // when the thread starts, it calls this function. We unwrap the 'this'
  89. //pointer and call ThreadProc.
  90. DWORD WINAPI
  91. CAMThread::InitialThreadProc(__inout LPVOID pv)
  92. {
  93. HRESULT hrCoInit = CAMThread::CoInitializeHelper();
  94. if(FAILED(hrCoInit)) {
  95. DbgLog((LOG_ERROR, 1, TEXT("CoInitializeEx failed.")));
  96. }
  97. CAMThread * pThread = (CAMThread *) pv;
  98. HRESULT hr = pThread->ThreadProc();
  99. if(SUCCEEDED(hrCoInit)) {
  100. CoUninitialize();
  101. }
  102. return hr;
  103. }
  104. BOOL
  105. CAMThread::Create()
  106. {
  107. DWORD threadid;
  108. CAutoLock lock(&m_AccessLock);
  109. if (ThreadExists()) {
  110. return FALSE;
  111. }
  112. m_hThread = CreateThread(
  113. NULL,
  114. 0,
  115. CAMThread::InitialThreadProc,
  116. this,
  117. 0,
  118. &threadid);
  119. if (!m_hThread) {
  120. return FALSE;
  121. }
  122. return TRUE;
  123. }
  124. DWORD
  125. CAMThread::CallWorker(DWORD dwParam)
  126. {
  127. // lock access to the worker thread for scope of this object
  128. CAutoLock lock(&m_AccessLock);
  129. if (!ThreadExists()) {
  130. return (DWORD) E_FAIL;
  131. }
  132. // set the parameter
  133. m_dwParam = dwParam;
  134. // signal the worker thread
  135. m_EventSend.Set();
  136. // wait for the completion to be signalled
  137. m_EventComplete.Wait();
  138. // done - this is the thread's return value
  139. return m_dwReturnVal;
  140. }
  141. // Wait for a request from the client
  142. DWORD
  143. CAMThread::GetRequest()
  144. {
  145. m_EventSend.Wait();
  146. return m_dwParam;
  147. }
  148. // is there a request?
  149. BOOL
  150. CAMThread::CheckRequest(__out_opt DWORD * pParam)
  151. {
  152. if (!m_EventSend.Check()) {
  153. return FALSE;
  154. } else {
  155. if (pParam) {
  156. *pParam = m_dwParam;
  157. }
  158. return TRUE;
  159. }
  160. }
  161. // reply to the request
  162. void
  163. CAMThread::Reply(DWORD dw)
  164. {
  165. m_dwReturnVal = dw;
  166. // The request is now complete so CheckRequest should fail from
  167. // now on
  168. //
  169. // This event should be reset BEFORE we signal the client or
  170. // the client may Set it before we reset it and we'll then
  171. // reset it (!)
  172. m_EventSend.Reset();
  173. // Tell the client we're finished
  174. m_EventComplete.Set();
  175. }
  176. HRESULT CAMThread::CoInitializeHelper()
  177. {
  178. // call CoInitializeEx and tell OLE not to create a window (this
  179. // thread probably won't dispatch messages and will hang on
  180. // broadcast msgs o/w).
  181. //
  182. // If CoInitEx is not available, threads that don't call CoCreate
  183. // aren't affected. Threads that do will have to handle the
  184. // failure. Perhaps we should fall back to CoInitialize and risk
  185. // hanging?
  186. //
  187. // older versions of ole32.dll don't have CoInitializeEx
  188. HRESULT hr = E_FAIL;
  189. HINSTANCE hOle = GetModuleHandle(TEXT("ole32.dll"));
  190. if(hOle)
  191. {
  192. typedef HRESULT (STDAPICALLTYPE *PCoInitializeEx)(
  193. LPVOID pvReserved, DWORD dwCoInit);
  194. PCoInitializeEx pCoInitializeEx =
  195. (PCoInitializeEx)(GetProcAddress(hOle, "CoInitializeEx"));
  196. if(pCoInitializeEx)
  197. {
  198. hr = (*pCoInitializeEx)(0, COINIT_DISABLE_OLE1DDE );
  199. }
  200. }
  201. else
  202. {
  203. // caller must load ole32.dll
  204. DbgBreak("couldn't locate ole32.dll");
  205. }
  206. return hr;
  207. }
  208. // destructor for CMsgThread - cleans up any messages left in the
  209. // queue when the thread exited
  210. CMsgThread::~CMsgThread()
  211. {
  212. if (m_hThread != NULL) {
  213. WaitForSingleObject(m_hThread, INFINITE);
  214. EXECUTE_ASSERT(CloseHandle(m_hThread));
  215. }
  216. POSITION pos = m_ThreadQueue.GetHeadPosition();
  217. while (pos) {
  218. CMsg * pMsg = m_ThreadQueue.GetNext(pos);
  219. delete pMsg;
  220. }
  221. m_ThreadQueue.RemoveAll();
  222. if (m_hSem != NULL) {
  223. EXECUTE_ASSERT(CloseHandle(m_hSem));
  224. }
  225. }
  226. BOOL
  227. CMsgThread::CreateThread(
  228. )
  229. {
  230. m_hSem = CreateSemaphore(NULL, 0, 0x7FFFFFFF, NULL);
  231. if (m_hSem == NULL) {
  232. return FALSE;
  233. }
  234. m_hThread = ::CreateThread(NULL, 0, DefaultThreadProc,
  235. (LPVOID)this, 0, &m_ThreadId);
  236. return m_hThread != NULL;
  237. }
  238. // This is the threads message pump. Here we get and dispatch messages to
  239. // clients thread proc until the client refuses to process a message.
  240. // The client returns a non-zero value to stop the message pump, this
  241. // value becomes the threads exit code.
  242. DWORD WINAPI
  243. CMsgThread::DefaultThreadProc(
  244. __inout LPVOID lpParam
  245. )
  246. {
  247. CMsgThread *lpThis = (CMsgThread *)lpParam;
  248. CMsg msg;
  249. LRESULT lResult;
  250. // !!!
  251. CoInitialize(NULL);
  252. // allow a derived class to handle thread startup
  253. lpThis->OnThreadInit();
  254. do {
  255. lpThis->GetThreadMsg(&msg);
  256. lResult = lpThis->ThreadMessageProc(msg.uMsg,msg.dwFlags,
  257. msg.lpParam, msg.pEvent);
  258. } while (lResult == 0L);
  259. // !!!
  260. CoUninitialize();
  261. return (DWORD)lResult;
  262. }
  263. // Block until the next message is placed on the list m_ThreadQueue.
  264. // copies the message to the message pointed to by *pmsg
  265. void
  266. CMsgThread::GetThreadMsg(__out CMsg *msg)
  267. {
  268. CMsg * pmsg = NULL;
  269. // keep trying until a message appears
  270. while (TRUE) {
  271. {
  272. CAutoLock lck(&m_Lock);
  273. pmsg = m_ThreadQueue.RemoveHead();
  274. if (pmsg == NULL) {
  275. m_lWaiting++;
  276. } else {
  277. break;
  278. }
  279. }
  280. // the semaphore will be signalled when it is non-empty
  281. WaitForSingleObject(m_hSem, INFINITE);
  282. }
  283. // copy fields to caller's CMsg
  284. *msg = *pmsg;
  285. // this CMsg was allocated by the 'new' in PutThreadMsg
  286. delete pmsg;
  287. }
  288. // Helper function - convert int to WSTR
  289. void WINAPI IntToWstr(int i, __out_ecount(12) LPWSTR wstr)
  290. {
  291. #ifdef UNICODE
  292. if (FAILED(StringCchPrintf(wstr, 12, L"%d", i))) {
  293. wstr[0] = 0;
  294. }
  295. #else
  296. TCHAR temp[12];
  297. if (FAILED(StringCchPrintf(temp, NUMELMS(temp), "%d", i))) {
  298. wstr[0] = 0;
  299. } else {
  300. MultiByteToWideChar(CP_ACP, 0, temp, -1, wstr, 12);
  301. }
  302. #endif
  303. } // IntToWstr
  304. #define MEMORY_ALIGNMENT 4
  305. #define MEMORY_ALIGNMENT_LOG2 2
  306. #define MEMORY_ALIGNMENT_MASK MEMORY_ALIGNMENT - 1
  307. void * __stdcall memmoveInternal(void * dst, const void * src, size_t count)
  308. {
  309. void * ret = dst;
  310. #ifdef _X86_
  311. if (dst <= src || (char *)dst >= ((char *)src + count)) {
  312. /*
  313. * Non-Overlapping Buffers
  314. * copy from lower addresses to higher addresses
  315. */
  316. _asm {
  317. mov esi,src
  318. mov edi,dst
  319. mov ecx,count
  320. cld
  321. mov edx,ecx
  322. and edx,MEMORY_ALIGNMENT_MASK
  323. shr ecx,MEMORY_ALIGNMENT_LOG2
  324. rep movsd
  325. or ecx,edx
  326. jz memmove_done
  327. rep movsb
  328. memmove_done:
  329. }
  330. }
  331. else {
  332. /*
  333. * Overlapping Buffers
  334. * copy from higher addresses to lower addresses
  335. */
  336. _asm {
  337. mov esi,src
  338. mov edi,dst
  339. mov ecx,count
  340. std
  341. add esi,ecx
  342. add edi,ecx
  343. dec esi
  344. dec edi
  345. rep movsb
  346. cld
  347. }
  348. }
  349. #else
  350. MoveMemory(dst, src, count);
  351. #endif
  352. return ret;
  353. }
  354. HRESULT AMSafeMemMoveOffset(
  355. __in_bcount(dst_size) void * dst,
  356. __in size_t dst_size,
  357. __in DWORD cb_dst_offset,
  358. __in_bcount(src_size) const void * src,
  359. __in size_t src_size,
  360. __in DWORD cb_src_offset,
  361. __in size_t count)
  362. {
  363. // prevent read overruns
  364. if( count + cb_src_offset < count || // prevent integer overflow
  365. count + cb_src_offset > src_size) // prevent read overrun
  366. {
  367. return E_INVALIDARG;
  368. }
  369. // prevent write overruns
  370. if( count + cb_dst_offset < count || // prevent integer overflow
  371. count + cb_dst_offset > dst_size) // prevent write overrun
  372. {
  373. return E_INVALIDARG;
  374. }
  375. memmoveInternal( (BYTE *)dst+cb_dst_offset, (BYTE *)src+cb_src_offset, count);
  376. return S_OK;
  377. }
  378. #ifdef DEBUG
  379. /******************************Public*Routine******************************\
  380. * Debug CCritSec helpers
  381. *
  382. * We provide debug versions of the Constructor, destructor, Lock and Unlock
  383. * routines. The debug code tracks who owns each critical section by
  384. * maintaining a depth count.
  385. *
  386. * History:
  387. *
  388. \**************************************************************************/
  389. CCritSec::CCritSec()
  390. {
  391. InitializeCriticalSection(&m_CritSec);
  392. m_currentOwner = m_lockCount = 0;
  393. m_fTrace = FALSE;
  394. }
  395. CCritSec::~CCritSec()
  396. {
  397. DeleteCriticalSection(&m_CritSec);
  398. }
  399. void CCritSec::Lock()
  400. {
  401. UINT tracelevel=3;
  402. DWORD us = GetCurrentThreadId();
  403. DWORD currentOwner = m_currentOwner;
  404. if (currentOwner && (currentOwner != us)) {
  405. // already owned, but not by us
  406. if (m_fTrace) {
  407. DbgLog((LOG_LOCKING, 2, TEXT("Thread %d about to wait for lock %x owned by %d"),
  408. GetCurrentThreadId(), &m_CritSec, currentOwner));
  409. tracelevel=2;
  410. // if we saw the message about waiting for the critical
  411. // section we ensure we see the message when we get the
  412. // critical section
  413. }
  414. }
  415. EnterCriticalSection(&m_CritSec);
  416. if (0 == m_lockCount++) {
  417. // we now own it for the first time. Set owner information
  418. m_currentOwner = us;
  419. if (m_fTrace) {
  420. DbgLog((LOG_LOCKING, tracelevel, TEXT("Thread %d now owns lock %x"), m_currentOwner, &m_CritSec));
  421. }
  422. }
  423. }
  424. void CCritSec::Unlock() {
  425. if (0 == --m_lockCount) {
  426. // about to be unowned
  427. if (m_fTrace) {
  428. DbgLog((LOG_LOCKING, 3, TEXT("Thread %d releasing lock %x"), m_currentOwner, &m_CritSec));
  429. }
  430. m_currentOwner = 0;
  431. }
  432. LeaveCriticalSection(&m_CritSec);
  433. }
  434. void WINAPI DbgLockTrace(CCritSec * pcCrit, BOOL fTrace)
  435. {
  436. pcCrit->m_fTrace = fTrace;
  437. }
  438. BOOL WINAPI CritCheckIn(CCritSec * pcCrit)
  439. {
  440. return (GetCurrentThreadId() == pcCrit->m_currentOwner);
  441. }
  442. BOOL WINAPI CritCheckIn(const CCritSec * pcCrit)
  443. {
  444. return (GetCurrentThreadId() == pcCrit->m_currentOwner);
  445. }
  446. BOOL WINAPI CritCheckOut(CCritSec * pcCrit)
  447. {
  448. return (GetCurrentThreadId() != pcCrit->m_currentOwner);
  449. }
  450. BOOL WINAPI CritCheckOut(const CCritSec * pcCrit)
  451. {
  452. return (GetCurrentThreadId() != pcCrit->m_currentOwner);
  453. }
  454. #endif
  455. STDAPI WriteBSTR(__deref_out BSTR *pstrDest, LPCWSTR szSrc)
  456. {
  457. *pstrDest = SysAllocString( szSrc );
  458. if( !(*pstrDest) ) return E_OUTOFMEMORY;
  459. return NOERROR;
  460. }
  461. STDAPI FreeBSTR(__deref_in BSTR* pstr)
  462. {
  463. if( (PVOID)*pstr == NULL ) return S_FALSE;
  464. SysFreeString( *pstr );
  465. return NOERROR;
  466. }
  467. // Return a wide string - allocating memory for it
  468. // Returns:
  469. // S_OK - no error
  470. // E_POINTER - ppszReturn == NULL
  471. // E_OUTOFMEMORY - can't allocate memory for returned string
  472. STDAPI AMGetWideString(LPCWSTR psz, __deref_out LPWSTR *ppszReturn)
  473. {
  474. CheckPointer(ppszReturn, E_POINTER);
  475. ValidateReadWritePtr(ppszReturn, sizeof(LPWSTR));
  476. *ppszReturn = NULL;
  477. size_t nameLen;
  478. HRESULT hr = StringCbLengthW(psz, 100000, &nameLen);
  479. if (FAILED(hr)) {
  480. return hr;
  481. }
  482. *ppszReturn = (LPWSTR)CoTaskMemAlloc(nameLen + sizeof(WCHAR));
  483. if (*ppszReturn == NULL) {
  484. return E_OUTOFMEMORY;
  485. }
  486. CopyMemory(*ppszReturn, psz, nameLen + sizeof(WCHAR));
  487. return NOERROR;
  488. }
  489. // Waits for the HANDLE hObject. While waiting messages sent
  490. // to windows on our thread by SendMessage will be processed.
  491. // Using this function to do waits and mutual exclusion
  492. // avoids some deadlocks in objects with windows.
  493. // Return codes are the same as for WaitForSingleObject
  494. DWORD WINAPI WaitDispatchingMessages(
  495. HANDLE hObject,
  496. DWORD dwWait,
  497. HWND hwnd,
  498. UINT uMsg,
  499. HANDLE hEvent)
  500. {
  501. BOOL bPeeked = FALSE;
  502. DWORD dwResult;
  503. DWORD dwStart;
  504. DWORD dwThreadPriority;
  505. static UINT uMsgId = 0;
  506. HANDLE hObjects[2] = { hObject, hEvent };
  507. if (dwWait != INFINITE && dwWait != 0) {
  508. dwStart = GetTickCount();
  509. }
  510. for (; ; ) {
  511. DWORD nCount = NULL != hEvent ? 2 : 1;
  512. // Minimize the chance of actually dispatching any messages
  513. // by seeing if we can lock immediately.
  514. dwResult = WaitForMultipleObjects(nCount, hObjects, FALSE, 0);
  515. if (dwResult < WAIT_OBJECT_0 + nCount) {
  516. break;
  517. }
  518. DWORD dwTimeOut = dwWait;
  519. if (dwTimeOut > 10) {
  520. dwTimeOut = 10;
  521. }
  522. dwResult = MsgWaitForMultipleObjects(
  523. nCount,
  524. hObjects,
  525. FALSE,
  526. dwTimeOut,
  527. hwnd == NULL ? QS_SENDMESSAGE :
  528. QS_SENDMESSAGE + QS_POSTMESSAGE);
  529. if (dwResult == WAIT_OBJECT_0 + nCount ||
  530. dwResult == WAIT_TIMEOUT && dwTimeOut != dwWait) {
  531. MSG msg;
  532. if (hwnd != NULL) {
  533. while (PeekMessage(&msg, hwnd, uMsg, uMsg, PM_REMOVE)) {
  534. DispatchMessage(&msg);
  535. }
  536. }
  537. // Do this anyway - the previous peek doesn't flush out the
  538. // messages
  539. PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE);
  540. if (dwWait != INFINITE && dwWait != 0) {
  541. DWORD dwNow = GetTickCount();
  542. // Working with differences handles wrap-around
  543. DWORD dwDiff = dwNow - dwStart;
  544. if (dwDiff > dwWait) {
  545. dwWait = 0;
  546. } else {
  547. dwWait -= dwDiff;
  548. }
  549. dwStart = dwNow;
  550. }
  551. if (!bPeeked) {
  552. // Raise our priority to prevent our message queue
  553. // building up
  554. dwThreadPriority = GetThreadPriority(GetCurrentThread());
  555. if (dwThreadPriority < THREAD_PRIORITY_HIGHEST) {
  556. SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
  557. }
  558. bPeeked = TRUE;
  559. }
  560. } else {
  561. break;
  562. }
  563. }
  564. if (bPeeked) {
  565. SetThreadPriority(GetCurrentThread(), dwThreadPriority);
  566. if (HIWORD(GetQueueStatus(QS_POSTMESSAGE)) & QS_POSTMESSAGE) {
  567. if (uMsgId == 0) {
  568. uMsgId = RegisterWindowMessage(TEXT("AMUnblock"));
  569. }
  570. if (uMsgId != 0) {
  571. MSG msg;
  572. // Remove old ones
  573. while (PeekMessage(&msg, (HWND)-1, uMsgId, uMsgId, PM_REMOVE)) {
  574. }
  575. }
  576. PostThreadMessage(GetCurrentThreadId(), uMsgId, 0, 0);
  577. }
  578. }
  579. return dwResult;
  580. }
  581. HRESULT AmGetLastErrorToHResult()
  582. {
  583. DWORD dwLastError = GetLastError();
  584. if(dwLastError != 0)
  585. {
  586. return HRESULT_FROM_WIN32(dwLastError);
  587. }
  588. else
  589. {
  590. return E_FAIL;
  591. }
  592. }
  593. IUnknown* QzAtlComPtrAssign(__deref_inout_opt IUnknown** pp, __in_opt IUnknown* lp)
  594. {
  595. if (lp != NULL)
  596. lp->AddRef();
  597. if (*pp)
  598. (*pp)->Release();
  599. *pp = lp;
  600. return lp;
  601. }
  602. /******************************************************************************
  603. CompatibleTimeSetEvent
  604. CompatibleTimeSetEvent() sets the TIME_KILL_SYNCHRONOUS flag before calling
  605. timeSetEvent() if the current operating system supports it. TIME_KILL_SYNCHRONOUS
  606. is supported on Windows XP and later operating systems.
  607. Parameters:
  608. - The same parameters as timeSetEvent(). See timeSetEvent()'s documentation in
  609. the Platform SDK for more information.
  610. Return Value:
  611. - The same return value as timeSetEvent(). See timeSetEvent()'s documentation in
  612. the Platform SDK for more information.
  613. ******************************************************************************/
  614. MMRESULT CompatibleTimeSetEvent( UINT uDelay, UINT uResolution, __in LPTIMECALLBACK lpTimeProc, DWORD_PTR dwUser, UINT fuEvent )
  615. {
  616. #if WINVER >= 0x0501
  617. {
  618. static bool fCheckedVersion = false;
  619. static bool fTimeKillSynchronousFlagAvailable = false;
  620. if( !fCheckedVersion ) {
  621. fTimeKillSynchronousFlagAvailable = TimeKillSynchronousFlagAvailable();
  622. fCheckedVersion = true;
  623. }
  624. if( fTimeKillSynchronousFlagAvailable ) {
  625. fuEvent = fuEvent | TIME_KILL_SYNCHRONOUS;
  626. }
  627. }
  628. #endif // WINVER >= 0x0501
  629. return timeSetEvent( uDelay, uResolution, lpTimeProc, dwUser, fuEvent );
  630. }
  631. bool TimeKillSynchronousFlagAvailable( void )
  632. {
  633. OSVERSIONINFO osverinfo;
  634. osverinfo.dwOSVersionInfoSize = sizeof(osverinfo);
  635. if( GetVersionEx( &osverinfo ) ) {
  636. // Windows XP's major version is 5 and its' minor version is 1.
  637. // timeSetEvent() started supporting the TIME_KILL_SYNCHRONOUS flag
  638. // in Windows XP.
  639. if( (osverinfo.dwMajorVersion > 5) ||
  640. ( (osverinfo.dwMajorVersion == 5) && (osverinfo.dwMinorVersion >= 1) ) ) {
  641. return true;
  642. }
  643. }
  644. return false;
  645. }