VOID Trace(_In_ LPCTSTR pszFormat, ...) { #ifdef _DEBUG PTSTR lpMsg, lpOut; va_list argList; HANDLE hHeap; size_t cbLen; HRESULT hr; hHeap = GetProcessHeap(); if (hHeap) { va_start(argList, pszFormat); hr = StringCchAVPrintf(hHeap, &lpMsg, &cbLen, pszFormat, argList); va_end(argList); if (SUCCEEDED(hr)) { hr = StringCchAPrintf(hHeap, &lpOut, &cbLen, TEXT("devsane: %s\r\n"), lpMsg); if (SUCCEEDED(hr)) { OutputDebugString(lpOut); HeapFree(hHeap, 0, lpOut); } HeapFree(hHeap, 0, lpMsg); } } #else UNREFERENCED_PARAMETER(pszFormat); #endif }
/** * Fatal error handler. Provides support for formatting the system * error message and showing errors with format strings etc blahblah. */ STATIC NORETURN FatalError(DWORD dwError, LPTSTR lpMessage, ...) { DWORD dwExit; va_list lpArgs = NULL; LPTSTR lpDisplayBuf = NULL; // If dwError has not been set, use the value of GetLastError if(dwError == ERROR_SUCCESS) { dwError = GetLastError(); } // Format message against args. va_start(lpArgs, lpMessage); lpDisplayBuf = StringCchVAPrintf(lpMessage, lpArgs); va_end(lpArgs); // If dwError contains an error code, get the system error message for it. if(dwError != ERROR_SUCCESS) { TCHAR *lpErrMsg = NULL, *lpTemp = NULL; dwExit = dwError; FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dwError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpErrMsg, 0, NULL ); lpTemp = StringCchAPrintf(_T("%s - Windows Error [0x%08X]: %s"), lpDisplayBuf, dwError, lpErrMsg); xfree(lpDisplayBuf); lpDisplayBuf = lpTemp; LocalFree((HLOCAL)lpErrMsg); } else { dwExit = 1; } ShowErrorMessage(lpDisplayBuf); xfree(lpDisplayBuf); ExitProcess(dwExit); }