12 #define STRSAFE_NO_DEPRECATE
17 CAMEvent::CAMEvent(BOOL fManualReset, __inout_opt HRESULT *phr)
21 if (
NULL != phr && SUCCEEDED(*phr)) {
27 CAMEvent::CAMEvent(__inout_opt HRESULT *phr)
31 if (
NULL != phr && SUCCEEDED(*phr)) {
61 DWORD dwWaitTime = dwTimeout;
66 if (dwWaitTime != INFINITE) {
67 dwStartTime = timeGetTime();
71 dwWait = MsgWaitForMultipleObjects(1,&
m_hEvent,FALSE, dwWaitTime, QS_SENDMESSAGE);
72 if (dwWait == WAIT_OBJECT_0 + 1) {
74 PeekMessage(&Message,
NULL,0,0,PM_NOREMOVE);
79 if (dwWaitTime != INFINITE) {
81 DWORD dwElapsed = timeGetTime()-dwStartTime;
84 (dwElapsed >= dwTimeout)
86 : dwTimeout-dwElapsed;
89 }
while (dwWait == WAIT_OBJECT_0 + 1);
93 return (dwWait == WAIT_OBJECT_0);
99 CAMThread::CAMThread(__inout_opt HRESULT *phr)
100 : m_EventSend(TRUE, phr),
101 m_EventComplete(FALSE, phr)
106 CAMThread::~CAMThread() {
114 CAMThread::InitialThreadProc(__inout LPVOID
pv)
116 HRESULT hrCoInit = CAMThread::CoInitializeHelper();
117 if(FAILED(hrCoInit)) {
121 CAMThread * pThread = (CAMThread *)
pv;
123 HRESULT
hr = pThread->ThreadProc();
125 if(SUCCEEDED(hrCoInit)) {
139 if (ThreadExists()) {
143 m_hThread = CreateThread(
146 CAMThread::InitialThreadProc,
159 CAMThread::CallWorker(DWORD dwParam)
164 if (!ThreadExists()) {
165 return (DWORD) E_FAIL;
175 m_EventComplete.Wait();
178 return m_dwReturnVal;
183 CAMThread::GetRequest()
191 CAMThread::CheckRequest(__out_opt DWORD * pParam)
193 if (!m_EventSend.Check()) {
205 CAMThread::Reply(DWORD dw)
220 m_EventComplete.Set();
223 HRESULT CAMThread::CoInitializeHelper()
238 HINSTANCE hOle = GetModuleHandle(TEXT(
"ole32.dll"));
241 typedef HRESULT (STDAPICALLTYPE *PCoInitializeEx)(
242 LPVOID pvReserved, DWORD dwCoInit);
243 PCoInitializeEx pCoInitializeEx =
244 (PCoInitializeEx)(GetProcAddress(hOle,
"CoInitializeEx"));
247 hr = (*pCoInitializeEx)(0, COINIT_DISABLE_OLE1DDE );
253 DbgBreak(
"couldn't locate ole32.dll");
264 if (m_hThread !=
NULL) {
265 WaitForSingleObject(m_hThread, INFINITE);
291 (LPVOID)
this, 0, &m_ThreadId);
292 return m_hThread !=
NULL;
302 CMsgThread::DefaultThreadProc(
303 __inout LPVOID lpParam
320 }
while (lResult == 0L);
325 return (DWORD)lResult;
348 WaitForSingleObject(
m_hSem, INFINITE);
359 void WINAPI
IntToWstr(
int i, __out_ecount(12) LPWSTR wstr)
362 if (FAILED(StringCchPrintf(wstr, 12, L
"%d", i))) {
367 if (FAILED(StringCchPrintf(temp,
NUMELMS(temp),
"%d", i))) {
370 MultiByteToWideChar(CP_ACP, 0, temp, -1, wstr, 12);
376 #define MEMORY_ALIGNMENT 4
377 #define MEMORY_ALIGNMENT_LOG2 2
378 #define MEMORY_ALIGNMENT_MASK MEMORY_ALIGNMENT - 1
385 if (dst <= src || (
char *)dst >= ((
char *)src + count)) {
426 MoveMemory(dst, src, count);
433 __in_bcount(dst_size)
void * dst,
434 __in
size_t dst_size,
435 __in DWORD cb_dst_offset,
436 __in_bcount(src_size)
const void * src,
437 __in
size_t src_size,
438 __in DWORD cb_src_offset,
442 if( count + cb_src_offset < count ||
443 count + cb_src_offset > src_size)
449 if( count + cb_dst_offset < count ||
450 count + cb_dst_offset > dst_size)
455 memmoveInternal( (BYTE *)dst+cb_dst_offset, (BYTE *)src+cb_src_offset, count);
474 InitializeCriticalSection(&m_CritSec);
475 m_currentOwner = m_lockCount = 0;
481 DeleteCriticalSection(&m_CritSec);
487 DWORD us = GetCurrentThreadId();
488 DWORD currentOwner = m_currentOwner;
489 if (currentOwner && (currentOwner != us)) {
493 GetCurrentThreadId(), &m_CritSec, currentOwner));
500 EnterCriticalSection(&m_CritSec);
501 if (0 == m_lockCount++) {
506 DbgLog((
LOG_LOCKING, tracelevel, TEXT(
"Thread %d now owns lock %x"), m_currentOwner, &m_CritSec));
512 if (0 == --m_lockCount) {
515 DbgLog((
LOG_LOCKING, 3, TEXT(
"Thread %d releasing lock %x"), m_currentOwner, &m_CritSec));
520 LeaveCriticalSection(&m_CritSec);
525 pcCrit->m_fTrace = fTrace;
530 return (GetCurrentThreadId() == pcCrit->m_currentOwner);
535 return (GetCurrentThreadId() == pcCrit->m_currentOwner);
540 return (GetCurrentThreadId() != pcCrit->m_currentOwner);
545 return (GetCurrentThreadId() != pcCrit->m_currentOwner);
550 STDAPI
WriteBSTR(__deref_out BSTR *pstrDest, LPCWSTR szSrc)
552 *pstrDest = SysAllocString( szSrc );
553 if( !(*pstrDest) )
return E_OUTOFMEMORY;
560 if( (
PVOID)*pstr ==
NULL )
return S_FALSE;
561 SysFreeString( *pstr );
577 HRESULT
hr = StringCbLengthW(psz, 100000, &nameLen);
581 *ppszReturn = (LPWSTR)CoTaskMemAlloc(nameLen +
sizeof(WCHAR));
582 if (*ppszReturn ==
NULL) {
583 return E_OUTOFMEMORY;
585 CopyMemory(*ppszReturn, psz, nameLen +
sizeof(WCHAR));
601 BOOL bPeeked = FALSE;
604 DWORD dwThreadPriority;
606 static UINT uMsgId = 0;
608 HANDLE hObjects[2] = { hObject, hEvent };
609 if (dwWait != INFINITE && dwWait != 0) {
610 dwStart = GetTickCount();
613 DWORD nCount =
NULL != hEvent ? 2 : 1;
617 dwResult = WaitForMultipleObjects(nCount, hObjects, FALSE, 0);
618 if (dwResult < WAIT_OBJECT_0 + nCount) {
622 DWORD dwTimeOut = dwWait;
623 if (dwTimeOut > 10) {
626 dwResult = MsgWaitForMultipleObjects(
631 hwnd ==
NULL ? QS_SENDMESSAGE :
632 QS_SENDMESSAGE + QS_POSTMESSAGE);
633 if (dwResult == WAIT_OBJECT_0 + nCount ||
634 dwResult == WAIT_TIMEOUT && dwTimeOut != dwWait) {
637 while (PeekMessage(&msg, hwnd, uMsg, uMsg, PM_REMOVE)) {
638 DispatchMessage(&msg);
643 PeekMessage(&msg,
NULL, 0, 0, PM_NOREMOVE);
645 if (dwWait != INFINITE && dwWait != 0) {
646 DWORD dwNow = GetTickCount();
649 DWORD dwDiff = dwNow - dwStart;
650 if (dwDiff > dwWait) {
660 dwThreadPriority = GetThreadPriority(GetCurrentThread());
661 if (dwThreadPriority < THREAD_PRIORITY_HIGHEST) {
662 SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
671 SetThreadPriority(GetCurrentThread(), dwThreadPriority);
672 if (HIWORD(GetQueueStatus(QS_POSTMESSAGE)) & QS_POSTMESSAGE) {
674 uMsgId = RegisterWindowMessage(TEXT(
"AMUnblock"));
679 while (PeekMessage(&msg, (HWND)-1, uMsgId, uMsgId, PM_REMOVE)) {
682 PostThreadMessage(GetCurrentThreadId(), uMsgId, 0, 0);
690 DWORD dwLastError = GetLastError();
693 return HRESULT_FROM_WIN32(dwLastError);
728 MMRESULT
CompatibleTimeSetEvent( UINT uDelay, UINT uResolution, __in LPTIMECALLBACK lpTimeProc, DWORD_PTR dwUser, UINT fuEvent )
732 static bool fCheckedVersion =
false;
733 static bool fTimeKillSynchronousFlagAvailable =
false;
735 if( !fCheckedVersion ) {
737 fCheckedVersion =
true;
740 if( fTimeKillSynchronousFlagAvailable ) {
741 fuEvent = fuEvent | TIME_KILL_SYNCHRONOUS;
744 #endif // WINVER >= 0x0501
746 return timeSetEvent( uDelay, uResolution, lpTimeProc, dwUser, fuEvent );
751 OSVERSIONINFO osverinfo;
753 osverinfo.dwOSVersionInfoSize =
sizeof(osverinfo);
755 if( GetVersionEx( &osverinfo ) ) {
760 if( (osverinfo.dwMajorVersion > 5) ||
761 ( (osverinfo.dwMajorVersion == 5) && (osverinfo.dwMinorVersion >= 1) ) ) {