HRESULT GetWICFactory(IWICImagingFactory** factory) { static CComAutoCriticalSection m_pWICFactoryLock; // keep this variable "private" static CComPtr<IWICImagingFactory> m_pWICFactory; if (!factory) return E_INVALIDARG; // We may have async methods calling this function, so we will protect it // with a critical section object m_pWICFactoryLock.Lock(); HRESULT hr = S_OK; if (nullptr == m_pWICFactory) { hr = CoCreateInstance( CLSID_WICImagingFactory, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&m_pWICFactory)); } if (SUCCEEDED(hr)) { hr = AssignToOutputPointer(factory, m_pWICFactory); } m_pWICFactoryLock.Unlock(); return hr; }
crGetLastErrorMsgW(LPWSTR pszBuffer, UINT uBuffSize) { if(pszBuffer==NULL || uBuffSize==0) return -1; // Null pointer to buffer strconv_t strconv; g_cs.Lock(); DWORD dwThreadId = GetCurrentThreadId(); std::map<DWORD, CString>::iterator it = g_sErrorMsg.find(dwThreadId); if(it==g_sErrorMsg.end()) { // No error message for current thread. CString sErrorMsg = _T("No error."); LPCWSTR pwszErrorMsg = strconv.t2w(sErrorMsg.GetBuffer(0)); int size = min(sErrorMsg.GetLength(), (int)uBuffSize-1); WCSNCPY_S(pszBuffer, uBuffSize, pwszErrorMsg, size); g_cs.Unlock(); return size; } LPCWSTR pwszErrorMsg = strconv.t2w(it->second.GetBuffer(0)); int size = min((int)wcslen(pwszErrorMsg), (int)uBuffSize-1); WCSNCPY_S(pszBuffer, uBuffSize, pwszErrorMsg, size); pszBuffer[uBuffSize-1] = 0; // Zero terminator g_cs.Unlock(); return size; }
SDBAPIHANDLE SdbObjectHandle::CreateHandle() { SDBAPIHANDLE ret = NULL; // We have to hold this lock while we're dealing with the handle // and the table the_map_locker.Lock(); ATLASSERT(!IsOpened()); if (!IsOpened()) { try { // Generate next handle value next_sdb_handle_value++; ret = reinterpret_cast<SDBAPIHANDLE>(next_sdb_handle_value); // Add ourselves to the map the_map[ret] = this; // Save handle, addref and return sdb_handle_ = ret; AddRef(); } catch (...) { ret = NULL; SetLastError(ERROR_OUTOFMEMORY); } } else { // Signaling that this object is already opened SetLastError(ERROR_GEN_FAILURE); } the_map_locker.Unlock(); return ret; }
int crSetErrorMsg(PTSTR pszErrorMsg) { g_cs.Lock(); DWORD dwThreadId = GetCurrentThreadId(); g_sErrorMsg[dwThreadId] = pszErrorMsg; g_cs.Unlock(); return 0; }
int crClearErrorMsg() { // This method erases the error message for the caller thread. // This may be required to avoid "memory leaks". g_cs.Lock(); DWORD dwThreadId = GetCurrentThreadId(); std::map<DWORD, CString>::iterator itMsg = g_sErrorMsg.find(dwThreadId); if(itMsg==g_sErrorMsg.end()) { g_sErrorMsg.erase(itMsg); } g_cs.Unlock(); return 0; }
int WINAPI SetEventCallBack(ECallBack Func) { try { TRACE("调用SetEventCallBack"); if(Func==NULL) return 1; g_objectCComAutoCriticalSection.Lock(); EventCallBackFunc=Func; g_objectCComAutoCriticalSection.Unlock(); return 0; } catch (CMemoryException* e) { return 30050; } catch (CFileException* e) { return 30051; } catch (CException* e) { return 30052; } catch(...) { return 30053; } }
int WINAPI SetDataCallBack(PFCALLBACK Func) { try { TRACE("调用SetDataCallBack"); if(Func==NULL) return 1; g_objectCComAutoCriticalSection.Lock(); DataCallBackFunc=Func; g_objectCComAutoCriticalSection.Unlock(); TRACE("调用SetDataCallBack,%d",DataCallBackFunc); return 0; } catch (CMemoryException* e) { return 30050; } catch (CFileException* e) { return 30051; } catch (CException* e) { return 30052; } catch(...) { return 30053; } }
SdbObjectHandle* SdbObjectHandle::Lookup(SDBAPIHANDLE sdb_hndl) { SdbObjectHandle* ret = NULL; the_map_locker.Lock(); try { // Look us up in the map. SdbObjectHandleMap::iterator found = the_map.find(sdb_hndl); if (found != the_map.end()) { ret = found->second; ret->AddRef(); } } catch (...) { SetLastError(ERROR_OUTOFMEMORY); } the_map_locker.Unlock(); return ret; }
bool SdbObjectHandle::CloseHandle() { bool ret = false; // Addref just in case that last reference to this object is being // held in the map AddRef(); the_map_locker.Lock(); ATLASSERT(IsOpened()); if (IsOpened()) { try { // Look us up in the map. SdbObjectHandleMap::iterator found = the_map.find(sdb_handle()); ATLASSERT((found != the_map.end()) && (this == found->second)); if ((found != the_map.end()) && (this == found->second)) { // Remove ourselves from the map, close and release the object the_map.erase(found); sdb_handle_ = NULL; Release(); ret = true; } else { SetLastError(ERROR_INVALID_HANDLE); } } catch (...) { ret = false; SetLastError(ERROR_OUTOFMEMORY); } } else { SetLastError(ERROR_INVALID_HANDLE); } the_map_locker.Unlock(); Release(); return ret; }
CRASHRPTAPI(int) crUninstall() { crSetErrorMsg(_T("Success.")); // Get crash handler singleton CCrashHandler *pCrashHandler = CCrashHandler::GetCurrentProcessCrashHandler(); // Check if found if(pCrashHandler==NULL || !pCrashHandler->IsInitialized()) { crSetErrorMsg(_T("Crash handler wasn't preiviously installed for this process.")); return 1; } // Uninstall main thread's C++ exception handlers int nUnset = pCrashHandler->UnSetThreadExceptionHandlers(); if(nUnset!=0) return 2; // Destroy the crash handler. int nDestroy = pCrashHandler->Destroy(); if(nDestroy!=0) return 3; // Free the crash handler object. delete pCrashHandler; // Clear last error message list. g_cs.Lock(); g_sErrorMsg.clear(); g_cs.Unlock(); return 0; }
void WINAPI ReceiveFromLC( int flag,char *lcName,char *methodName,void *ptrPara1,void *ptrPara2,void *ptrPara3) { try { TRACE("ReceiveFromLC flag:%d,%s,%s",flag,lcName,methodName ); if (strcmp(lcName,"_XSM_R3")==0) { if (DataCallBackFunc==NULL) { TRACE("DataCallBackFunc,NULL"); } else { PLCControlFava* pPLCControlFava=new PLCControlFava(); pPLCControlFava->szFlag=flag; pPLCControlFava->szMethodName=methodName; pPLCControlFava->szPara1=(char*)ptrPara1; pPLCControlFava->szPara2=(char*)ptrPara2; pPLCControlFava->szPara3=(char*)ptrPara3; TRACE("即将调用XSMtoR3 回调函数"); g_objectCComAutoCriticalSection.Lock(); (*DataCallBackFunc)(flag,(void*)pPLCControlFava); g_objectCComAutoCriticalSection.Unlock(); TRACE(" 回调函数返回"); delete pPLCControlFava; } return ; } if (strcmp(lcName,"_webToAir")==0) { TRACE("发现 _webToAir"); if (strcmp(methodName,"webToAirHandler")==0) { TRACE("发现 webToAirHandler"); TRACE("webToAirHandler"); if (GetSockStatus()!=0) { TRACE("发现 不联网"); return; } TRACE("发现 联网"); g_objectCComAutoCriticalSection.Lock(); CDataClass * pCDataClass=new CDataClass((BYTE*)ptrPara1); global_socket->ListInPacket.push(pCDataClass); g_objectCComAutoCriticalSection.Unlock(); EventSendFromFlex.SetEvent(); /*int i=SendPackge(g_socketServer);*/ //global_socket.AsyncSelect(FD_WRITE); //return i; return; } } return; } catch (CMemoryException* e) { return; } catch (CFileException* e) { return; } catch (CException* e) { return; } catch(...) { return; } }
//以下为Delphi传入数据包 extern "C" _declspec(dllexport) int __stdcall Send(int flag,void * Pin) { try { if (flag==1||flag==2||flag==3) { if (g_LCStatusFlag<=1 ) { return MMLCConnect(flag,Pin); } else { return 30040; } } if (flag==1003) { if (strlen(((SingleFava*)Pin)->chatTxt)>1000) { return 30030; } } TRACE("调用Send flag:%d",flag); if (GetSockStatus()!=0) { return GetSockStatus(); } g_objectCComAutoCriticalSection.Lock(); CDataClass * pCDataClass=new CDataClass(flag,Pin); global_socket->ListInPacket_Push(pCDataClass); /*int i=SendPackge(g_socketServer);*/ g_objectCComAutoCriticalSection.Unlock(); EventSend.SetEvent(); //global_socket.AsyncSelect(FD_WRITE); return 0; } catch (CMemoryException* e) { return 30050; } catch (CFileException* e) { return 30051; } catch (CException* e) { return 30052; } catch(...) { return 30053; } // return 0; }
DWORD WINAPI ServiceThread(LPVOID lpThreadParameter) //UINT ServiceThread( LPVOID lpThreadParameter ) //unsigned int WINAPI ServiceThread(void* lpThreadParameter) //////////////////////////////////////////////// { ::CoInitialize(NULL); ST_THREAD_PARAM* pThread = (ST_THREAD_PARAM*)lpThreadParameter; SOCKET socketServer = pThread->socket; WSAEVENT wsaEvent = pThread->wsaEvent; //////////////////////////// HANDLE hmultihandle[4]; hmultihandle[0]=wsaEvent; hmultihandle[1]=EventExit.m_hObject; hmultihandle[2]=EventSend.m_hObject; hmultihandle[3]=EventSendFromFlex.m_hObject; TRACE("新线程%d起动/n",GetCurrentThreadId()); try { while(true) { int nRet=::WSAWaitForMultipleEvents(4,hmultihandle,FALSE,10000,FALSE); TRACE("WSAWaitForMultipleEvents %d",nRet); if(nRet==WAIT_FAILED) // 失败 { /////////////////////////////// TRACE("failed WSAWaitForMultipleEvents/n"); THROW(1); break; } else if(nRet==WSA_WAIT_TIMEOUT) // 超时 { TRACE(" WSA_WAIT_TIMEOUT ... /n"); ////////////////////////////////// /* int i=0; THROW(i);*/ /////////////////////////////////////////// continue; } else if (nRet==WSA_WAIT_EVENT_0) // 成功 -- 网络事件发生 { WSANETWORKEVENTS wsaNetEvent; ::WSAEnumNetworkEvents(socketServer,wsaEvent,&wsaNetEvent); TRACE("WSAEnumNetworkEvents",nRet); if(wsaNetEvent.lNetworkEvents&FD_READ) { g_objectCComAutoCriticalSection.Lock(); Receive(g_socketServer); g_objectCComAutoCriticalSection.Unlock(); } else if(wsaNetEvent.lNetworkEvents&FD_WRITE) { g_objectCComAutoCriticalSection.Lock(); SendPackge(g_socketServer); g_objectCComAutoCriticalSection.Unlock(); EventSend.ResetEvent(); } if(wsaNetEvent.lNetworkEvents&FD_CLOSE) { TRACE("FD_CLOSE event occurs.../n"); int nErrorCode = WSAGetLastError(); TRACE("Error code is %d/n",nErrorCode); if (nErrorCode == WSAECONNRESET) { TRACE("WSAECONNRESET error./n"); } else if (nErrorCode == WSAENETDOWN) { TRACE("WSAENETDOWN error./n"); } else if (nErrorCode == WSAENETRESET) { TRACE("WSAENETRESET error./n"); } THROW(1); TRACE("线程%d退出/n",GetCurrentThreadId()); TRACE("线程%d退出/n",GetCurrentThreadId()); return 0; TRACE("无法执行的代码"); } } else if (nRet==WSA_WAIT_EVENT_0+1) { THROW(2); return 0; TRACE("无法执行的代码"); } else if (nRet==WSA_WAIT_EVENT_0+2) { EventSend.ResetEvent(); g_objectCComAutoCriticalSection.Lock(); int i=SendPackge(g_socketServer); g_objectCComAutoCriticalSection.Unlock(); //send 方法 } else if (nRet==WSA_WAIT_EVENT_0+3) { EventSendFromFlex.ResetEvent(); g_objectCComAutoCriticalSection.Lock(); int i=SendPackge(g_socketServer); g_objectCComAutoCriticalSection.Unlock(); //send 方法 } } } catch(int& j) { TRACE("线程%d退出/n",GetCurrentThreadId()); TRACE("线程%d退出/n",GetCurrentThreadId()); if (j==1) { (*EventCallBackFunc)(2,0,NULL); UnInitSock(); delete global_socket; } if (j==2) { UnInitSock(); delete global_socket; EventExit.ResetEvent(); EventExitBack.SetEvent(); } return 0; } catch (CMemoryException* e) { } catch (CFileException* e) { } catch (CException* e) { } TRACE("线程1%d退出/n",GetCurrentThreadId()); EventExit.SetEvent(); return 0; }
extern "C" _declspec(dllexport) int __stdcall SendSync(int flag,void * Pin) { try { if (flag==1003) { if (strlen(((SingleFava*)Pin)->chatTxt)>MAXMESSAGELENGTH) { return 30030; } } if (GetSockStatus()!=0) { return GetSockStatus(); } TRACE("调用SendSync flag:%d",flag); TRACE("调用SendSync flag:%d",flag); DWORD iErrorNo; DWORD iRetWait; g_objectCComAutoCriticalSection.Lock(); TRACE("开始组织数据\n"); CDataClass * pCDataClass=new CDataClass(flag,Pin); //global_socket->ListInPacket.push(pCDataClass); global_socket->ListInPacket_Push(pCDataClass); g_objectCComAutoCriticalSection.Unlock(); EventSend.SetEvent(); //global_socket.AsyncSelect(FD_WRITE); TRACE("数据完全生成"); TRACE("数据完全生成 开始调用发送"); //int i=SendPackge(g_socketServer); // g_objectCComAutoCriticalSection.Unlock(); if (flag==1000) { TRACE("1000节点 开始等待"); TRACE("开始等待1000"); BOOL i=EventConnectFava.ResetEvent(); int dRet=WaitForSingleObject(EventConnectFava,RECVTIMEOUT); TRACE("WaitForSingleObject退出"); if(dRet == WAIT_TIMEOUT) { TRACE("WaitForSingleObject超时"); EventExit.SetEvent(); return 1460; } else if(dRet == WAIT_OBJECT_0) { TRACE("WaitForSingleObject正常退出1000________________ iError: %d",iError); TRACE("正常退出1000________________"); return iError; } } if (flag==1012) { EventPlayerFava1012.ResetEvent(); int dRet=WaitForSingleObject(EventPlayerFava1012,RECVTIMEOUT); if(dRet == WAIT_TIMEOUT) { EventExit.SetEvent(); return 1460; } else if(dRet == WAIT_OBJECT_0) { TRACE("正常退出1000________________"); return iError; } ///////////////////////////////////////////////////////////// } if (flag==1013) { EventPlayerFava1013.ResetEvent(); int dRet=WaitForSingleObject(EventPlayerFava1013,RECVTIMEOUT); if(dRet == WAIT_TIMEOUT) { EventExit.SetEvent(); return 1460; } else if(dRet == WAIT_OBJECT_0) { TRACE("正常退出1000________________"); return iError; } ///////////////////////////////////////////////////////////// } if (flag==1014) { EventPlayerFava1014.ResetEvent(); int dRet=WaitForSingleObject(EventPlayerFava1014,RECVTIMEOUT); if(dRet == WAIT_TIMEOUT) { EventExit.SetEvent(); return 1460; } else if(dRet == WAIT_OBJECT_0) { TRACE("正常退出1000________________"); return iError; } ///////////////////////////////////////////////////////////// } return 0; } catch (CMemoryException* e) { return 30050; } catch (CFileException* e) { return 30051; } catch (CException* e) { return 30052; } catch(...) { return 30053; } }
HRESULT HandleEvent(TAPI_EVENT TapiEvent, IDispatch* pEvent) { USES_CONVERSION; HRESULT hr = S_OK; CComQIPtr<ITCallInfo> pCallInfo; if (TapiEvent == TE_CALLNOTIFICATION) { CComQIPtr<ITCallNotificationEvent> pNotification = pEvent; ATLASSERT(pNotification != NULL); if (pNotification == NULL) return E_POINTER; hr = pNotification->get_Call(&pCallInfo); ATLASSERT(SUCCEEDED(hr)); if (FAILED(hr)) return hr; csGlobals.Lock(); // ATLASSERT(g_pCurrCallInfo == NULL); g_pCurrCallInfo = pCallInfo; g_bCallWasNotified = FALSE; g_nNumRings = 0; csGlobals.Unlock(); // Return quickly here... usually no caller ID information will be available now // so we just save the CallInfo object for later in hope that it'll be quick enough // to catch the TE_CALLINFOCHANGE event // If it's not quick enough, we'll get the information in the next ring // This is a known annoyance in the TAPI 3 events handling, which forces to save // the latest call in a global variable return S_OK; } else if (TapiEvent == TE_CALLINFOCHANGE) { CComQIPtr<ITCallInfoChangeEvent> pNotification = pEvent; ATLASSERT(pNotification != NULL); if (pNotification == NULL) return E_POINTER; CALLINFOCHANGE_CAUSE eCause = CIC_OTHER; hr = pNotification->get_Cause(&eCause); ATLASSERT(SUCCEEDED(hr)); if (FAILED(hr)) return hr; if (eCause != CIC_CALLERID) return S_OK; hr = pNotification->get_Call(&pCallInfo); ATLASSERT(SUCCEEDED(hr)); if (FAILED(hr)) return hr; } else if (TapiEvent == TE_ADDRESS) { CComQIPtr<ITAddressEvent> pNotification = pEvent; ATLASSERT(pNotification != NULL); if (pNotification == NULL) return E_POINTER; ADDRESS_EVENT eEvent = AE_STATE; hr = pNotification->get_Event(&eEvent); ATLASSERT(SUCCEEDED(hr)); if (FAILED(hr)) return hr; if (eEvent != AE_RINGING) return S_OK; csGlobals.Lock(); pCallInfo = g_pCurrCallInfo; g_nNumRings++; csGlobals.Unlock(); } else if (TapiEvent == TE_CALLSTATE) { CComQIPtr<ITCallStateEvent> pNotification = pEvent; ATLASSERT(pNotification != NULL); if (pNotification == NULL) return E_POINTER; hr = pNotification->get_Call(&pCallInfo); ATLASSERT(SUCCEEDED(hr)); if (FAILED(hr)) return hr; CALL_STATE eState; hr = pCallInfo->get_CallState(&eState); ATLASSERT(SUCCEEDED(hr)); if (FAILED(hr)) return hr; if (eState != CS_DISCONNECTED) return S_OK; csGlobals.Lock(); g_pCurrCallInfo = (ITCallInfo*) NULL; g_nNumRings = 0; csGlobals.Unlock(); return S_OK; } // if (pCallInfo == NULL) // Happens after we've already notified the event // return S_OK; CComBSTR bstrString; hr = pCallInfo->get_CallInfoString(CIS_CALLERIDNUMBER, &bstrString); csGlobals.Lock(); // Do not notify the call: // 1. Again // 2. If there's no call info available and the phone rang less than twice (give // caller ID info time to become available) if ((bstrString.Length() == 0 && g_nNumRings < 2) || g_bCallWasNotified == TRUE) { csGlobals.Unlock(); return S_OK; } csGlobals.Unlock(); CALLERIDINCOMINGCALLINFO ici = {0}; ici.cbSize = sizeof(CALLERIDINCOMINGCALLINFO); if (bstrString.m_str != NULL) { ici.szCallerIdNumber = OLE2CA(bstrString.m_str); } else { ici.szCallerIdNumber = ""; } NotifyEventHooks(g_hEventIncomingCall, 0, reinterpret_cast<LPARAM>(&ici)); bstrString.Empty(); csGlobals.Lock(); // g_pCurrCallInfo = NULL; g_nNumRings = 0; g_bCallWasNotified = TRUE; csGlobals.Unlock(); return S_OK; }
CCaptureCriticalSection( CComAutoCriticalSection& criticalSection ) : criticalSection( criticalSection ) { criticalSection.Lock(); }