⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dxutil.cpp

📁 VC游戏编程基础
💻 CPP
📖 第 1 页 / 共 3 页
字号:
//-----------------------------------------------------------------------------
// File: DXUtil.cpp
//
// Desc: Shortcut macros and functions for using DX objects
//
// Copyright (c) Microsoft Corporation. All rights reserved
//-----------------------------------------------------------------------------
#include "dxstdafx.h"

#ifdef UNICODE
    typedef HINSTANCE (WINAPI* LPShellExecute)(HWND hwnd, LPCWSTR lpOperation, LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd);
#else
    typedef HINSTANCE (WINAPI* LPShellExecute)(HWND hwnd, LPCSTR lpOperation, LPCSTR lpFile, LPCSTR lpParameters, LPCSTR lpDirectory, INT nShowCmd);
#endif


#ifndef UNDER_CE
//-----------------------------------------------------------------------------
// Name: DXUtil_GetDXSDKMediaPathCch()
// Desc: Returns the DirectX SDK media path
//       cchDest is the size in TCHARs of strDest.  Be careful not to 
//       pass in sizeof(strDest) on UNICODE builds.
//-----------------------------------------------------------------------------
HRESULT DXUtil_GetDXSDKMediaPathCch( TCHAR* strDest, int cchDest )
{
    if( strDest == NULL || cchDest < 1 )
        return E_INVALIDARG;

    lstrcpy( strDest, TEXT("") );

    // Open the appropriate registry key
    HKEY  hKey;
    LONG lResult = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
                                _T("Software\\Microsoft\\DirectX SDK"),
                                0, KEY_READ, &hKey );
    if( ERROR_SUCCESS != lResult )
        return E_FAIL;

    DWORD dwType;
    DWORD dwSize = cchDest * sizeof(TCHAR);
    lResult = RegQueryValueEx( hKey, _T("DX9J3SDK Samples Path"), NULL,
                              &dwType, (BYTE*)strDest, &dwSize );
    strDest[cchDest-1] = 0; // RegQueryValueEx doesn't NULL term if buffer too small
    RegCloseKey( hKey );

    if( ERROR_SUCCESS != lResult )
        return E_FAIL;

    const TCHAR* strMedia = _T("\\Media\\");
    if( lstrlen(strDest) + lstrlen(strMedia) < cchDest )
        _tcscat( strDest, strMedia );
    else
        return E_INVALIDARG;

    return S_OK;
}
#endif // !UNDER_CE



#ifndef UNDER_CE
//-----------------------------------------------------------------------------
// Name: DXUtil_FindMediaFileCch()
// Desc: Trys to find the location of a SDK media file
//       cchDest is the size in TCHARs of strDestPath.  Be careful not to 
//       pass in sizeof(strDest) on UNICODE builds.
//-----------------------------------------------------------------------------
HRESULT DXUtil_FindMediaFileCch( TCHAR* strDestPath, int cchDest, LPCTSTR strFilename )
{
    HRESULT hr;
    TCHAR* strLeafNameTmp = NULL;
    TCHAR strLeafName[MAX_PATH];
    TCHAR strExePath[MAX_PATH];
    TCHAR strExeName[MAX_PATH];
    TCHAR strMediaDir[MAX_PATH];
    TCHAR strSearchPath[MAX_PATH];
    TCHAR* strLastSlash = NULL;
    int cchPath;
    BOOL bFound = FALSE;

    if( NULL==strFilename || NULL==strDestPath || cchDest < 10 )
        return E_INVALIDARG;

    lstrcpy( strDestPath, TEXT("") );
    lstrcpy( strLeafName, TEXT("") );

    // Append current working directory to strFilename and extract the leaf filename 
    cchPath = GetFullPathName(strFilename, MAX_PATH, strSearchPath, &strLeafNameTmp);
    if ((cchPath == 0) || (MAX_PATH <= cchPath))
        return E_FAIL;
    if( strLeafNameTmp )
        lstrcpyn( strLeafName, strLeafNameTmp, MAX_PATH );

    // Search in .\ 
    if( GetFileAttributes( strSearchPath ) != 0xFFFFFFFF )
        bFound = true;

    if( !bFound )
    {
        // Search in ..\ 
        _sntprintf( strSearchPath, MAX_PATH, TEXT("..\\%s"), strLeafName );
        strSearchPath[MAX_PATH-1] = 0;
        if( GetFileAttributes( strSearchPath ) != 0xFFFFFFFF )
            bFound = true;
    }

    if( !bFound )
    {
        // Search in ..\..\  
        _sntprintf( strSearchPath, MAX_PATH, TEXT("..\\..\\%s"), strLeafName );
        strSearchPath[MAX_PATH-1] = 0;
        if( GetFileAttributes( strSearchPath ) != 0xFFFFFFFF )
            bFound = true;
    }
    
    if( !bFound )
    {
        // Get the exe name, and exe path
        GetModuleFileName( NULL, strExePath, MAX_PATH );
        strExePath[MAX_PATH-1]=0;
        strLastSlash = _tcsrchr( strExePath, TEXT('\\') );
        if( strLastSlash )
        {
            lstrcpyn( strExeName, &strLastSlash[1], MAX_PATH );

            // Chop the exe name from the exe path
            *strLastSlash = 0;

            // Chop the .exe from the exe name
            strLastSlash = _tcsrchr( strExeName, TEXT('.') );
            if( strLastSlash )
                *strLastSlash = 0;
        }

        // Search in the executable directory
        _sntprintf( strSearchPath, MAX_PATH, _T("%s\\%s"), strExePath, strLeafName );
        strSearchPath[MAX_PATH-1] = 0;
        if( GetFileAttributes( strSearchPath ) != 0xFFFFFFFF )
            bFound = TRUE;

        if( !bFound )
        {
            // Search in "%EXE_DIR%\.."
            _sntprintf( strSearchPath, MAX_PATH, _T("%s\\..\\%s"), strExePath, strLeafName );
            strSearchPath[MAX_PATH-1] = 0;
            if( GetFileAttributes( strSearchPath ) != 0xFFFFFFFF )
                bFound = TRUE;
        }

        if( !bFound )
        {
            // Search in "%EXE_DIR%\..\.."
            _sntprintf( strSearchPath, MAX_PATH, _T("%s\\..\\..\\%s"), strExePath, strLeafName );
            strSearchPath[MAX_PATH-1] = 0;
            if( GetFileAttributes( strSearchPath ) != 0xFFFFFFFF )
                bFound = TRUE;
        }

        if( !bFound )
        {
            // Search in "%EXE_DIR%\..\%EXE_NAME%\".  This matches the DirectX SDK layout
            _sntprintf( strSearchPath, MAX_PATH, TEXT("%s\\..\\%s\\%s"), strExePath, strExeName, strLeafName );
            strSearchPath[MAX_PATH-1] = 0;
            if( GetFileAttributes( strSearchPath ) != 0xFFFFFFFF )
                bFound = TRUE;
        }
    }
    
    if( !bFound )
    {
        // Get the media dir 
        if( FAILED( hr = DXUtil_GetDXSDKMediaPathCch( strMediaDir, MAX_PATH ) ) )
            return hr;

        // Search in SDK's media dir 
        _sntprintf( strSearchPath, MAX_PATH, TEXT("%s%s"), strMediaDir, strLeafName );
        strSearchPath[MAX_PATH-1] = 0;
        if( GetFileAttributes( strSearchPath ) != 0xFFFFFFFF )
            bFound = TRUE;
    }

    if( bFound )
    {
        // Found the file, so copy the search path to the output buffer
        // and return success
        lstrcpyn( strDestPath, strSearchPath, cchDest );
        return S_OK;
    }
    else
    {
        // On failure, return the file as the path but return an error code
        lstrcpyn( strDestPath, strFilename, cchDest );
        return HRESULT_FROM_WIN32( ERROR_FILE_NOT_FOUND );
    }
}
#endif // !UNDER_CE




//-----------------------------------------------------------------------------
// Name: DXUtil_ReadStringRegKeyCch()
// Desc: Helper function to read a registry key string
//       cchDest is the size in TCHARs of strDest.  Be careful not to 
//       pass in sizeof(strDest) on UNICODE builds.
//-----------------------------------------------------------------------------
HRESULT DXUtil_ReadStringRegKeyCch( HKEY hKey, LPCTSTR strRegName, TCHAR* strDest, 
                                    DWORD cchDest, LPCTSTR strDefault )
{
    DWORD dwType;
    DWORD cbDest = cchDest * sizeof(TCHAR);

    if( ERROR_SUCCESS != RegQueryValueEx( hKey, strRegName, 0, &dwType, 
                                          (BYTE*)strDest, &cbDest ) )
    {
        _tcsncpy( strDest, strDefault, cchDest );
        strDest[cchDest-1] = 0;
        return S_FALSE;
    }
    else
    {     
        if( dwType != REG_SZ )
        {
            _tcsncpy( strDest, strDefault, cchDest );
            strDest[cchDest-1] = 0;
            return S_FALSE;
        }   
    }

    return S_OK;
}




//-----------------------------------------------------------------------------
// Name: DXUtil_WriteStringRegKey()
// Desc: Helper function to write a registry key string
//-----------------------------------------------------------------------------
HRESULT DXUtil_WriteStringRegKey( HKEY hKey, LPCTSTR strRegName,
                                  LPCTSTR strValue )
{
    if( NULL == strValue )
        return E_INVALIDARG;
        
    DWORD cbValue = ((DWORD)_tcslen(strValue)+1) * sizeof(TCHAR);

    if( ERROR_SUCCESS != RegSetValueEx( hKey, strRegName, 0, REG_SZ, 
                                        (BYTE*)strValue, cbValue ) )
        return E_FAIL;

    return S_OK;
}




//-----------------------------------------------------------------------------
// Name: DXUtil_ReadFloatRegKey()
// Desc: Helper function to read a registry key string
//-----------------------------------------------------------------------------
HRESULT DXUtil_ReadFloatRegKey( HKEY hKey, LPCTSTR strRegName, FLOAT* pfDest, FLOAT fDefault )
{
    if( NULL == pfDest )
        return E_INVALIDARG;

    TCHAR sz[256];
    float fResult;

    TCHAR strDefault[256];
    _sntprintf( strDefault, 256, TEXT("%f"), fDefault );
    strDefault[255] = 0;
   
    if( SUCCEEDED( DXUtil_ReadStringRegKeyCch( hKey, strRegName, sz, 256, strDefault ) ) )
    {
        int nResult = _stscanf( sz, TEXT("%f"), &fResult );
        if( nResult == 1 )
        {
            *pfDest = fResult;
            return S_OK;           
        }
    }

    *pfDest = fDefault;
    return S_FALSE;
}




//-----------------------------------------------------------------------------
// Name: DXUtil_WriteFloatRegKey()
// Desc: Helper function to write a registry key string
//-----------------------------------------------------------------------------
HRESULT DXUtil_WriteFloatRegKey( HKEY hKey, LPCTSTR strRegName, FLOAT fValue )
{
    TCHAR strValue[256];
    _sntprintf( strValue, 256, TEXT("%f"), fValue );
    strValue[255] = 0;

    return DXUtil_WriteStringRegKey( hKey, strRegName, strValue );
}




//-----------------------------------------------------------------------------
// Name: DXUtil_ReadIntRegKey()
// Desc: Helper function to read a registry key int
//-----------------------------------------------------------------------------
HRESULT DXUtil_ReadIntRegKey( HKEY hKey, LPCTSTR strRegName, DWORD* pdwDest, 
                              DWORD dwDefault )
{
    DWORD dwType;
    DWORD dwLength = sizeof(DWORD);

    if( ERROR_SUCCESS != RegQueryValueEx( hKey, strRegName, 0, &dwType, 
                                          (BYTE*)pdwDest, &dwLength ) )
    {
        *pdwDest = dwDefault;
        return S_FALSE;
    }
    else
    {
        if( dwType != REG_DWORD )
        {
            *pdwDest = dwDefault;
            return S_FALSE;
        }
    }

    return S_OK;
}




//-----------------------------------------------------------------------------
// Name: DXUtil_WriteIntRegKey()
// Desc: Helper function to write a registry key int
//-----------------------------------------------------------------------------
HRESULT DXUtil_WriteIntRegKey( HKEY hKey, LPCTSTR strRegName, DWORD dwValue )
{
    if( ERROR_SUCCESS != RegSetValueEx( hKey, strRegName, 0, REG_DWORD, 
                                        (BYTE*)&dwValue, sizeof(DWORD) ) )
        return E_FAIL;

    return S_OK;
}




//-----------------------------------------------------------------------------
// Name: DXUtil_ReadBoolRegKey()
// Desc: Helper function to read a registry key BOOL
//-----------------------------------------------------------------------------
HRESULT DXUtil_ReadBoolRegKey( HKEY hKey, LPCTSTR strRegName, BOOL* pbDest, 
                              BOOL bDefault )
{
    DWORD dwType;
    DWORD dwLength = sizeof(BOOL);

    if( ERROR_SUCCESS != RegQueryValueEx( hKey, strRegName, 0, &dwType, 
                                          (BYTE*)pbDest, &dwLength ) )
    {
        *pbDest = bDefault;
        return S_FALSE;
    }
    else
    {
        if( dwType != REG_DWORD )
        {
            *pbDest = bDefault;
            return S_FALSE;
        }
    }

    return S_OK;
}




//-----------------------------------------------------------------------------
// Name: DXUtil_WriteBoolRegKey()
// Desc: Helper function to write a registry key BOOL
//-----------------------------------------------------------------------------
HRESULT DXUtil_WriteBoolRegKey( HKEY hKey, LPCTSTR strRegName, BOOL bValue )
{
    if( ERROR_SUCCESS != RegSetValueEx( hKey, strRegName, 0, REG_DWORD, 
                                        (BYTE*)&bValue, sizeof(BOOL) ) )
        return E_FAIL;

    return S_OK;
}




//-----------------------------------------------------------------------------
// Name: DXUtil_ReadGuidRegKey()
// Desc: Helper function to read a registry key guid
//-----------------------------------------------------------------------------
HRESULT DXUtil_ReadGuidRegKey( HKEY hKey, LPCTSTR strRegName, GUID* pGuidDest, 
                               GUID& guidDefault )
{
    DWORD dwType;
    DWORD dwLength = sizeof(GUID);

    if( ERROR_SUCCESS != RegQueryValueEx( hKey, strRegName, 0, &dwType, 
                                          (LPBYTE) pGuidDest, &dwLength ) )
    {
        *pGuidDest = guidDefault;
        return S_FALSE;
    }
    else
    {
        if( dwType != REG_BINARY )
        {
            *pGuidDest = guidDefault;
            return S_FALSE;
        }
    }

    return S_OK;
}




//-----------------------------------------------------------------------------
// Name: DXUtil_WriteGuidRegKey()
// Desc: Helper function to write a registry key guid
//-----------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -