0001: //-----------------------------------------------------------------------------
0002: // File: DXUtil.cpp
0003: //
0004: // Desc: Shortcut macros and functions for using DX objects
0005: //-----------------------------------------------------------------------------
0006: #define STRICT
0007: #include <windows.h>
0008: #include <mmsystem.h>
0009: #include <tchar.h>
0010: #include <stdio.h> 
0011: #include <stdarg.h>
0012: #include "DXUtil.h"
0013: 
0014: 
0015: 
0016: 
0017: //-----------------------------------------------------------------------------
0018: // Name: DXUtil_GetDXSDKMediaPath()
0019: // Desc: Returns the DirectX SDK media path
0020: //-----------------------------------------------------------------------------
0021: const TCHAR* DXUtil_GetDXSDKMediaPath()
0022: {
0023:     static TCHAR strNull[2] = _T("");
0024:     static TCHAR strPath[MAX_PATH];
0025:     DWORD dwType;
0026:     DWORD dwSize = MAX_PATH;
0027:     HKEY  hKey;
0028: 
0029:     // Open the appropriate registry key
0030:     LONG lResult = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
0031:                                 _T("Software\\Microsoft\\DirectX SDK"),
0032:                                 0, KEY_READ, &hKey );
0033:     if( ERROR_SUCCESS != lResult )
0034:         return strNull;
0035: 
0036:     lResult = RegQueryValueEx( hKey, _T("DX81SDK Samples Path"), NULL,
0037:                               &dwType, (BYTE*)strPath, &dwSize );
0038:     RegCloseKey( hKey );
0039: 
0040:     if( ERROR_SUCCESS != lResult )
0041:         return strNull;
0042: 
0043:     _tcscat( strPath, _T("\\Media\\") );
0044: 
0045:     return strPath;
0046: }
0047: 
0048: 
0049: 
0050: 
0051: //-----------------------------------------------------------------------------
0052: // Name: DXUtil_FindMediaFile()
0053: // Desc: Returns a valid path to a DXSDK media file
0054: //-----------------------------------------------------------------------------
0055: HRESULT DXUtil_FindMediaFile( TCHAR* strPath, TCHAR* strFilename )
0056: {
0057:     HANDLE file;
0058:     TCHAR strFullPath[1024];
0059:     TCHAR *strShortName;
0060:     DWORD cchPath;
0061: 
0062:     if( NULL==strFilename || NULL==strPath )
0063:         return E_INVALIDARG;
0064: 
0065:     // Build full path name from strFileName (strShortName will be just the leaf filename)
0066:     cchPath = GetFullPathName(strFilename, sizeof(strFullPath)/sizeof(TCHAR), strFullPath, &strShortName);
0067:     if ((cchPath == 0) || (sizeof(strFullPath)/sizeof(TCHAR) <= cchPath))
0068:         return E_FAIL;
0069: 
0070:     // first try to find the filename given a full path
0071:     file = CreateFile( strFullPath, GENERIC_READ, FILE_SHARE_READ, NULL, 
0072:                        OPEN_EXISTING, 0, NULL );
0073:     if( INVALID_HANDLE_VALUE != file )
0074:     {
0075:         _tcscpy( strPath, strFullPath );
0076:         CloseHandle( file );
0077:         return S_OK;
0078:     }
0079:     
0080:     // next try to find the filename in the current working directory (path stripped)
0081:     file = CreateFile( strShortName, GENERIC_READ, FILE_SHARE_READ, NULL, 
0082:                        OPEN_EXISTING, 0, NULL );
0083:     if( INVALID_HANDLE_VALUE != file )
0084:     {
0085:         _tcscpy( strPath, strShortName );
0086:         CloseHandle( file );
0087:         return S_OK;
0088:     }
0089:     
0090:     // last, check if the file exists in the media directory
0091:     _stprintf( strPath, _T("%s%s"), DXUtil_GetDXSDKMediaPath(), strShortName );
0092: 
0093:     file = CreateFile( strPath, GENERIC_READ, FILE_SHARE_READ, NULL, 
0094:                        OPEN_EXISTING, 0, NULL );
0095:     if( INVALID_HANDLE_VALUE != file )
0096:     {
0097:         CloseHandle( file );
0098:         return S_OK;
0099:     }
0100: 
0101:     // On failure, just return the file as the path
0102:     _tcscpy( strPath, strFilename );
0103:     return E_FAIL;
0104: }
0105: 
0106: 
0107: 
0108: 
0109: //-----------------------------------------------------------------------------
0110: // Name: DXUtil_ReadStringRegKey()
0111: // Desc: Helper function to read a registry key string
0112: //-----------------------------------------------------------------------------
0113: HRESULT DXUtil_ReadStringRegKey( HKEY hKey, TCHAR* strRegName, TCHAR* strValue, 
0114:                                  DWORD dwLength, TCHAR* strDefault )
0115: {
0116:     DWORD dwType;
0117: 
0118:     if( ERROR_SUCCESS != RegQueryValueEx( hKey, strRegName, 0, &dwType, 
0119:                                           (BYTE*)strValue, &dwLength ) )
0120:     {
0121:         _tcscpy( strValue, strDefault );
0122:     }
0123: 
0124:     return S_OK;
0125: }
0126: 
0127: 
0128: 
0129: 
0130: //-----------------------------------------------------------------------------
0131: // Name: DXUtil_WriteStringRegKey()
0132: // Desc: Helper function to write a registry key string
0133: //-----------------------------------------------------------------------------
0134: HRESULT DXUtil_WriteStringRegKey( HKEY hKey, TCHAR* strRegName,
0135:                                   TCHAR* strValue )
0136: {
0137:     if( ERROR_SUCCESS != RegSetValueEx( hKey, strRegName, 0, REG_SZ, 
0138:                                         (BYTE*)strValue, 
0139:                                         (_tcslen(strValue)+1)*sizeof(TCHAR) ) )
0140:         return E_FAIL;
0141: 
0142:     return S_OK;
0143: }
0144: 
0145: 
0146: 
0147: 
0148: //-----------------------------------------------------------------------------
0149: // Name: DXUtil_ReadIntRegKey()
0150: // Desc: Helper function to read a registry key int
0151: //-----------------------------------------------------------------------------
0152: HRESULT DXUtil_ReadIntRegKey( HKEY hKey, TCHAR* strRegName, DWORD* pdwValue, 
0153:                               DWORD dwDefault )
0154: {
0155:     DWORD dwType;
0156:     DWORD dwLength = sizeof(DWORD);
0157: 
0158:     if( ERROR_SUCCESS != RegQueryValueEx( hKey, strRegName, 0, &dwType, 
0159:                                           (BYTE*)pdwValue, &dwLength ) )
0160:     {
0161:         *pdwValue = dwDefault;
0162:     }
0163: 
0164:     return S_OK;
0165: }
0166: 
0167: 
0168: 
0169: 
0170: //-----------------------------------------------------------------------------
0171: // Name: DXUtil_WriteIntRegKey()
0172: // Desc: Helper function to write a registry key int
0173: //-----------------------------------------------------------------------------
0174: HRESULT DXUtil_WriteIntRegKey( HKEY hKey, TCHAR* strRegName, DWORD dwValue )
0175: {
0176:     if( ERROR_SUCCESS != RegSetValueEx( hKey, strRegName, 0, REG_DWORD, 
0177:                                         (BYTE*)&dwValue, sizeof(DWORD) ) )
0178:         return E_FAIL;
0179: 
0180:     return S_OK;
0181: }
0182: 
0183: 
0184: 
0185: 
0186: //-----------------------------------------------------------------------------
0187: // Name: DXUtil_ReadBoolRegKey()
0188: // Desc: Helper function to read a registry key BOOL
0189: //-----------------------------------------------------------------------------
0190: HRESULT DXUtil_ReadBoolRegKey( HKEY hKey, TCHAR* strRegName, BOOL* pbValue, 
0191:                               BOOL bDefault )
0192: {
0193:     DWORD dwType;
0194:     DWORD dwLength = sizeof(BOOL);
0195: 
0196:     if( ERROR_SUCCESS != RegQueryValueEx( hKey, strRegName, 0, &dwType, 
0197:                                           (BYTE*)pbValue, &dwLength ) )
0198:     {
0199:         *pbValue = bDefault;
0200:     }
0201: 
0202:     return S_OK;
0203: }
0204: 
0205: 
0206: 
0207: 
0208: //-----------------------------------------------------------------------------
0209: // Name: DXUtil_WriteBoolRegKey()
0210: // Desc: Helper function to write a registry key BOOL
0211: //-----------------------------------------------------------------------------
0212: HRESULT DXUtil_WriteBoolRegKey( HKEY hKey, TCHAR* strRegName, BOOL bValue )
0213: {
0214:     if( ERROR_SUCCESS != RegSetValueEx( hKey, strRegName, 0, REG_DWORD, 
0215:                                         (BYTE*)&bValue, sizeof(BOOL) ) )
0216:         return E_FAIL;
0217: 
0218:     return S_OK;
0219: }
0220: 
0221: 
0222: 
0223: 
0224: //-----------------------------------------------------------------------------
0225: // Name: DXUtil_ReadGuidRegKey()
0226: // Desc: Helper function to read a registry key guid
0227: //-----------------------------------------------------------------------------
0228: HRESULT DXUtil_ReadGuidRegKey( HKEY hKey, TCHAR* strRegName, GUID* pGuidValue, 
0229:                                GUID& guidDefault )
0230: {
0231:     DWORD dwType;
0232:     DWORD dwLength = sizeof(GUID);
0233: 
0234:     if( ERROR_SUCCESS != RegQueryValueEx( hKey, strRegName, 0, &dwType, 
0235:                                           (LPBYTE) pGuidValue, &dwLength ) )
0236:     {
0237:         *pGuidValue = guidDefault;
0238:     }
0239: 
0240:     return S_OK;
0241: }
0242: 
0243: 
0244: 
0245: 
0246: //-----------------------------------------------------------------------------
0247: // Name: DXUtil_WriteGuidRegKey()
0248: // Desc: Helper function to write a registry key guid
0249: //-----------------------------------------------------------------------------
0250: HRESULT DXUtil_WriteGuidRegKey( HKEY hKey, TCHAR* strRegName, GUID guidValue )
0251: {
0252:     if( ERROR_SUCCESS != RegSetValueEx( hKey, strRegName, 0, REG_BINARY, 
0253:                                         (BYTE*)&guidValue, sizeof(GUID) ) )
0254:         return E_FAIL;
0255: 
0256:     return S_OK;
0257: }
0258: 
0259: 
0260: 
0261: 
0262: //-----------------------------------------------------------------------------
0263: // Name: DXUtil_Timer()
0264: // Desc: Performs timer opertations. Use the following commands:
0265: //          TIMER_RESET           - to reset the timer
0266: //          TIMER_START           - to start the timer
0267: //          TIMER_STOP            - to stop (or pause) the timer
0268: //          TIMER_ADVANCE         - to advance the timer by 0.1 seconds
0269: //          TIMER_GETABSOLUTETIME - to get the absolute system time
0270: //          TIMER_GETAPPTIME      - to get the current time
0271: //          TIMER_GETELAPSEDTIME  - to get the time that elapsed between 
0272: //                                  TIMER_GETELAPSEDTIME calls
0273: //-----------------------------------------------------------------------------
0274: FLOAT __stdcall DXUtil_Timer( TIMER_COMMAND command )
0275: {
0276:     static BOOL     m_bTimerInitialized = FALSE;
0277:     static BOOL     m_bUsingQPF         = FALSE;
0278:     static BOOL     m_bTimerStopped     = TRUE;
0279:     static LONGLONG m_llQPFTicksPerSec  = 0;
0280: 
0281:     // Initialize the timer
0282:     if( FALSE == m_bTimerInitialized )
0283:     {
0284:         m_bTimerInitialized = TRUE;
0285: 
0286:         // Use QueryPerformanceFrequency() to get frequency of timer.  If QPF is
0287:         // not supported, we will timeGetTime() which returns milliseconds.
0288:         LARGE_INTEGER qwTicksPerSec;
0289:         m_bUsingQPF = QueryPerformanceFrequency( &qwTicksPerSec );
0290:         if( m_bUsingQPF )
0291:             m_llQPFTicksPerSec = qwTicksPerSec.QuadPart;
0292:     }
0293: 
0294:     if( m_bUsingQPF )
0295:     {
0296:         static LONGLONG m_llStopTime        = 0;
0297:         static LONGLONG m_llLastElapsedTime = 0;
0298:         static LONGLONG m_llBaseTime        = 0;
0299:         double fTime;
0300:         double fElapsedTime;
0301:         LARGE_INTEGER qwTime;
0302:         
0303:         // Get either the current time or the stop time, depending
0304:         // on whether we're stopped and what command was sent
0305:         if( m_llStopTime != 0 && command != TIMER_START && command != TIMER_GETABSOLUTETIME)
0306:             qwTime.QuadPart = m_llStopTime;
0307:         else
0308:             QueryPerformanceCounter( &qwTime );
0309: 
0310:         // Return the elapsed time
0311:         if( command == TIMER_GETELAPSEDTIME )
0312:         {
0313:             fElapsedTime = (double) ( qwTime.QuadPart - m_llLastElapsedTime ) / (double) m_llQPFTicksPerSec;
0314:             m_llLastElapsedTime = qwTime.QuadPart;
0315:             return (FLOAT) fElapsedTime;
0316:         }
0317:     
0318:         // Return the current time
0319:         if( command == TIMER_GETAPPTIME )
0320:         {
0321:             double fAppTime = (double) ( qwTime.QuadPart - m_llBaseTime ) / (double) m_llQPFTicksPerSec;
0322:             return (FLOAT) fAppTime;
0323:         }
0324:     
0325:         // Reset the timer
0326:         if( command == TIMER_RESET )
0327:         {
0328:             m_llBaseTime        = qwTime.QuadPart;
0329:             m_llLastElapsedTime = qwTime.QuadPart;
0330:             m_llStopTime        = 0;
0331:             m_bTimerStopped     = FALSE;
0332:             return 0.0f;
0333:         }
0334:     
0335:         // Start the timer
0336:         if( command == TIMER_START )
0337:         {
0338:             if( m_bTimerStopped )
0339:                 m_llBaseTime += qwTime.QuadPart - m_llStopTime;
0340:             m_llStopTime = 0;
0341:             m_llLastElapsedTime = qwTime.QuadPart;
0342:             m_bTimerStopped = FALSE;
0343:             return 0.0f;
0344:         }
0345:     
0346:         // Stop the timer
0347:         if( command == TIMER_STOP )
0348:         {
0349:             m_llStopTime = qwTime.QuadPart;
0350:             m_llLastElapsedTime = qwTime.QuadPart;
0351:             m_bTimerStopped = TRUE;
0352:             return 0.0f;
0353:         }
0354:     
0355:         // Advance the timer by 1/10th second
0356:         if( command == TIMER_ADVANCE )
0357:         {
0358:             m_llStopTime += m_llQPFTicksPerSec/10;
0359:             return 0.0f;
0360:         }
0361: 
0362:         if( command == TIMER_GETABSOLUTETIME )
0363:         {
0364:             fTime = qwTime.QuadPart / (double) m_llQPFTicksPerSec;
0365:             return (FLOAT) fTime;
0366:         }
0367: 
0368:         return -1.0f; // Invalid command specified
0369:     }
0370:     else
0371:     {
0372:         // Get the time using timeGetTime()
0373:         static double m_fLastElapsedTime  = 0.0;
0374:         static double m_fBaseTime         = 0.0;
0375:         static double m_fStopTime         = 0.0;
0376:         double fTime;
0377:         double fElapsedTime;
0378:         
0379:         // Get either the current time or the stop time, depending
0380:         // on whether we're stopped and what command was sent
0381:         if( m_fStopTime != 0.0 && command != TIMER_START && command != TIMER_GETABSOLUTETIME)
0382:             fTime = m_fStopTime;
0383:         else
0384:             fTime = timeGetTime() * 0.001;
0385:     
0386:         // Return the elapsed time
0387:         if( command == TIMER_GETELAPSEDTIME )
0388:         {   
0389:             fElapsedTime = (double) (fTime - m_fLastElapsedTime);
0390:             m_fLastElapsedTime = fTime;
0391:             return (FLOAT) fElapsedTime;
0392:         }
0393:     
0394:         // Return the current time
0395:         if( command == TIMER_GETAPPTIME )
0396:         {
0397:             return (FLOAT) (fTime - m_fBaseTime);
0398:         }
0399:     
0400:         // Reset the timer
0401:         if( command == TIMER_RESET )
0402:         {
0403:             m_fBaseTime         = fTime;
0404:             m_fLastElapsedTime  = fTime;
0405:             m_fStopTime         = 0;
0406:             m_bTimerStopped     = FALSE;
0407:             return 0.0f;
0408:         }
0409:     
0410:         // Start the timer
0411:         if( command == TIMER_START )
0412:         {
0413:             if( m_bTimerStopped )
0414:                 m_fBaseTime += fTime - m_fStopTime;
0415:             m_fStopTime = 0.0f;
0416:             m_fLastElapsedTime  = fTime;
0417:             m_bTimerStopped = FALSE;
0418:             return 0.0f;
0419:         }
0420:     
0421:         // Stop the timer
0422:         if( command == TIMER_STOP )
0423:         {
0424:             m_fStopTime = fTime;
0425:             m_fLastElapsedTime  = fTime;
0426:             m_bTimerStopped = TRUE;
0427:             return 0.0f;
0428:         }
0429:     
0430:         // Advance the timer by 1/10th second
0431:         if( command == TIMER_ADVANCE )
0432:         {
0433:             m_fStopTime += 0.1f;
0434:             return 0.0f;
0435:         }
0436: 
0437:         if( command == TIMER_GETABSOLUTETIME )
0438:         {
0439:             return (FLOAT) fTime;
0440:         }
0441: 
0442:         return -1.0f; // Invalid command specified
0443:     }
0444: }
0445: 
0446: 
0447: 
0448: 
0449: //-----------------------------------------------------------------------------
0450: // Name: DXUtil_ConvertAnsiStringToWide()
0451: // Desc: This is a UNICODE conversion utility to convert a CHAR string into a
0452: //       WCHAR string. cchDestChar defaults -1 which means it 
0453: //       assumes strDest is large enough to store strSource
0454: //-----------------------------------------------------------------------------
0455: VOID DXUtil_ConvertAnsiStringToWide( WCHAR* wstrDestination, const CHAR* strSource, 
0456:                                      int cchDestChar )
0457: {
0458:     if( wstrDestination==NULL || strSource==NULL )
0459:         return;
0460: 
0461:     if( cchDestChar == -1 )
0462:         cchDestChar = strlen(strSource)+1;
0463: 
0464:     MultiByteToWideChar( CP_ACP, 0, strSource, -1, 
0465:                          wstrDestination, cchDestChar-1 );
0466: 
0467:     wstrDestination[cchDestChar-1] = 0;
0468: }
0469: 
0470: 
0471: 
0472: 
0473: //-----------------------------------------------------------------------------
0474: // Name: DXUtil_ConvertWideStringToAnsi()
0475: // Desc: This is a UNICODE conversion utility to convert a WCHAR string into a
0476: //       CHAR string. cchDestChar defaults -1 which means it 
0477: //       assumes strDest is large enough to store strSource
0478: //-----------------------------------------------------------------------------
0479: VOID DXUtil_ConvertWideStringToAnsi( CHAR* strDestination, const WCHAR* wstrSource, 
0480:                                      int cchDestChar )
0481: {
0482:     if( strDestination==NULL || wstrSource==NULL )
0483:         return;
0484: 
0485:     if( cchDestChar == -1 )
0486:         cchDestChar = wcslen(wstrSource)+1;
0487: 
0488:     WideCharToMultiByte( CP_ACP, 0, wstrSource, -1, strDestination, 
0489:                          cchDestChar-1, NULL, NULL );
0490: 
0491:     strDestination[cchDestChar-1] = 0;
0492: }
0493: 
0494: 
0495: 
0496: 
0497: //-----------------------------------------------------------------------------
0498: // Name: DXUtil_ConvertGenericStringToAnsi()
0499: // Desc: This is a UNICODE conversion utility to convert a TCHAR string into a
0500: //       CHAR string. cchDestChar defaults -1 which means it 
0501: //       assumes strDest is large enough to store strSource
0502: //-----------------------------------------------------------------------------
0503: VOID DXUtil_ConvertGenericStringToAnsi( CHAR* strDestination, const TCHAR* tstrSource, 
0504:                                         int cchDestChar )
0505: {
0506:     if( strDestination==NULL || tstrSource==NULL || cchDestChar == 0 )
0507:         return;
0508: 
0509: #ifdef _UNICODE
0510:     DXUtil_ConvertWideStringToAnsi( strDestination, tstrSource, cchDestChar );
0511: #else
0512:     if( cchDestChar == -1 )
0513:     {
0514:         strcpy( strDestination, tstrSource );
0515:     }
0516:     else
0517:     {
0518:         strncpy( strDestination, tstrSource, cchDestChar );
0519:         strDestination[cchDestChar-1] = '\0';
0520:     }
0521: #endif
0522: }
0523: 
0524: 
0525: 
0526: 
0527: //-----------------------------------------------------------------------------
0528: // Name: DXUtil_ConvertGenericStringToWide()
0529: // Desc: This is a UNICODE conversion utility to convert a TCHAR string into a
0530: //       WCHAR string. cchDestChar defaults -1 which means it 
0531: //       assumes strDest is large enough to store strSource
0532: //-----------------------------------------------------------------------------
0533: VOID DXUtil_ConvertGenericStringToWide( WCHAR* wstrDestination, const TCHAR* tstrSource, 
0534:                                         int cchDestChar )
0535: {
0536:     if( wstrDestination==NULL || tstrSource==NULL || cchDestChar == 0 )
0537:         return;
0538: 
0539: #ifdef _UNICODE
0540:     if( cchDestChar == -1 )
0541:     {
0542:         wcscpy( wstrDestination, tstrSource );
0543:     }
0544:     else
0545:     {
0546:         wcsncpy( wstrDestination, tstrSource, cchDestChar );
0547:         wstrDestination[cchDestChar-1] = L'\0';
0548:     }
0549: #else
0550:     DXUtil_ConvertAnsiStringToWide( wstrDestination, tstrSource, cchDestChar );
0551: #endif
0552: }
0553: 
0554: 
0555: 
0556: 
0557: //-----------------------------------------------------------------------------
0558: // Name: DXUtil_ConvertAnsiStringToGeneric()
0559: // Desc: This is a UNICODE conversion utility to convert a CHAR string into a
0560: //       TCHAR string. cchDestChar defaults -1 which means it 
0561: //       assumes strDest is large enough to store strSource
0562: //-----------------------------------------------------------------------------
0563: VOID DXUtil_ConvertAnsiStringToGeneric( TCHAR* tstrDestination, const CHAR* strSource, 
0564:                                         int cchDestChar )
0565: {
0566:     if( tstrDestination==NULL || strSource==NULL || cchDestChar == 0 )
0567:         return;
0568:         
0569: #ifdef _UNICODE
0570:     DXUtil_ConvertAnsiStringToWide( tstrDestination, strSource, cchDestChar );
0571: #else
0572:     if( cchDestChar == -1 )
0573:     {
0574:         strcpy( tstrDestination, strSource );
0575:     }
0576:     else
0577:     {
0578:         strncpy( tstrDestination, strSource, cchDestChar );
0579:         tstrDestination[cchDestChar-1] = '\0';
0580:     }
0581: #endif
0582: }
0583: 
0584: 
0585: 
0586: 
0587: //-----------------------------------------------------------------------------
0588: // Name: DXUtil_ConvertAnsiStringToGeneric()
0589: // Desc: This is a UNICODE conversion utility to convert a WCHAR string into a
0590: //       TCHAR string. cchDestChar defaults -1 which means it 
0591: //       assumes strDest is large enough to store strSource
0592: //-----------------------------------------------------------------------------
0593: VOID DXUtil_ConvertWideStringToGeneric( TCHAR* tstrDestination, const WCHAR* wstrSource, 
0594:                                         int cchDestChar )
0595: {
0596:     if( tstrDestination==NULL || wstrSource==NULL || cchDestChar == 0 )
0597:         return;
0598: 
0599: #ifdef _UNICODE
0600:     if( cchDestChar == -1 )
0601:     {
0602:         wcscpy( tstrDestination, wstrSource );
0603:     }
0604:     else
0605:     {
0606:         wcsncpy( tstrDestination, wstrSource, cchDestChar );
0607:         tstrDestination[cchDestChar-1] = L'\0';
0608:     }
0609: #else
0610:     DXUtil_ConvertWideStringToAnsi( tstrDestination, wstrSource, cchDestChar );
0611: #endif
0612: }
0613: 
0614: 
0615: 
0616: 
0617: //-----------------------------------------------------------------------------
0618: // Name: _DbgOut()
0619: // Desc: Outputs a message to the debug stream
0620: //-----------------------------------------------------------------------------
0621: HRESULT _DbgOut( TCHAR* strFile, DWORD dwLine, HRESULT hr, TCHAR* strMsg )
0622: {
0623:     TCHAR buffer[256];
0624:     wsprintf( buffer, _T("%s(%ld): "), strFile, dwLine );
0625:     OutputDebugString( buffer );
0626:     OutputDebugString( strMsg );
0627: 
0628:     if( hr )
0629:     {
0630:         wsprintf( buffer, _T("(hr=%08lx)\n"), hr );
0631:         OutputDebugString( buffer );
0632:     }
0633: 
0634:     OutputDebugString( _T("\n") );
0635: 
0636:     return hr;
0637: }
0638: 
0639: 
0640: 
0641: 
0642: //-----------------------------------------------------------------------------
0643: // Name: DXUtil_Trace()
0644: // Desc: Outputs to the debug stream a formatted string with a variable-
0645: //       argument list.
0646: //-----------------------------------------------------------------------------
0647: VOID DXUtil_Trace( TCHAR* strMsg, ... )
0648: {
0649: #if defined(DEBUG) | defined(_DEBUG)
0650:     TCHAR strBuffer[512];
0651:     
0652:     va_list args;
0653:     va_start(args, strMsg);
0654:     _vsntprintf( strBuffer, 512, strMsg, args );
0655:     va_end(args);
0656: 
0657:     OutputDebugString( strBuffer );
0658: #else
0659:     UNREFERENCED_PARAMETER(strMsg);
0660: #endif
0661: }
0662: 
0663: 
0664: 
0665: 
0666: //-----------------------------------------------------------------------------
0667: // Name: DXUtil_ConvertStringToGUID()
0668: // Desc: Converts a string to a GUID
0669: //-----------------------------------------------------------------------------
0670: BOOL DXUtil_ConvertStringToGUID( const TCHAR* strIn, GUID* pGuidOut )
0671: {
0672:     UINT aiTmp[10];
0673: 
0674:     if( _stscanf( strIn, TEXT("{%8X-%4X-%4X-%2X%2X-%2X%2X%2X%2X%2X%2X}"),
0675:                     &pGuidOut->Data1, 
0676:                     &aiTmp[0], &aiTmp[1], 
0677:                     &aiTmp[2], &aiTmp[3],
0678:                     &aiTmp[4], &aiTmp[5],
0679:                     &aiTmp[6], &aiTmp[7],
0680:                     &aiTmp[8], &aiTmp[9] ) != 11 )
0681:     {
0682:         ZeroMemory( pGuidOut, sizeof(GUID) );
0683:         return FALSE;
0684:     }
0685:     else
0686:     {
0687:         pGuidOut->Data2       = (USHORT) aiTmp[0];
0688:         pGuidOut->Data3       = (USHORT) aiTmp[1];
0689:         pGuidOut->Data4[0]    = (BYTE) aiTmp[2];
0690:         pGuidOut->Data4[1]    = (BYTE) aiTmp[3];
0691:         pGuidOut->Data4[2]    = (BYTE) aiTmp[4];
0692:         pGuidOut->Data4[3]    = (BYTE) aiTmp[5];
0693:         pGuidOut->Data4[4]    = (BYTE) aiTmp[6];
0694:         pGuidOut->Data4[5]    = (BYTE) aiTmp[7];
0695:         pGuidOut->Data4[6]    = (BYTE) aiTmp[8];
0696:         pGuidOut->Data4[7]    = (BYTE) aiTmp[9];
0697:         return TRUE;
0698:     }
0699: }
0700: 
0701: 
0702: 
0703: 
0704: //-----------------------------------------------------------------------------
0705: // Name: DXUtil_ConvertGUIDToString()
0706: // Desc: Converts a GUID to a string 
0707: //-----------------------------------------------------------------------------
0708: VOID DXUtil_ConvertGUIDToString( const GUID* pGuidIn, TCHAR* strOut )
0709: {
0710:     _stprintf( strOut, TEXT("{%0.8X-%0.4X-%0.4X-%0.2X%0.2X-%0.2X%0.2X%0.2X%0.2X%0.2X%0.2X}"),
0711:                pGuidIn->Data1, pGuidIn->Data2, pGuidIn->Data3,
0712:                pGuidIn->Data4[0], pGuidIn->Data4[1],
0713:                pGuidIn->Data4[2], pGuidIn->Data4[3],
0714:                pGuidIn->Data4[4], pGuidIn->Data4[5],
0715:                pGuidIn->Data4[6], pGuidIn->Data4[7] );
0716: }
0717: