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

📄 utils.c

📁 Xcale270Bsp包,wince平台
💻 C
字号:
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//
/*++

THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.

Module Name: 

    utils.c

Abstract:

    Common USB Client Driver utils

--*/

#include "utils.h"

//
// GetSetKeyValues
// Get or Set the specified KeyName and its Values to the registry under HKLM
//
// KeyName: HKLM\KeyName
// ValDesc: pointer to a Reg Value Descriptor array. Note the last array entry must be NULL.
// bSet: TRUE to Set, else Get
// bOverwrite: TRUE to overwrite it the key already exists
//
BOOL
GetSetKeyValues(
   LPCTSTR          KeyName,
   PREG_VALUE_DESCR ValDesc,
   BOOL             bSet,
   BOOL             bOverwrite
   )
{
    HKEY   hKey;
    DWORD  dwStatus;
    DWORD  dwDisp;
    DWORD  dwVal;    
    DWORD  dwValLen = 0;
    LPWSTR pwStr;
    PREG_VALUE_DESCR pValueDesc = NULL;

    if (!KeyName || !ValDesc) {
        return FALSE;
    }

    dwStatus = RegOpenKeyEx( HKEY_LOCAL_MACHINE, KeyName, 0, 0, &hKey);

    if ( SET == bSet ) {

        if ( dwStatus == ERROR_SUCCESS ) {
            // Registry Key already exist, do not overwrite.
            // Assumes the values are correct.
            RegCloseKey( hKey); 
            if (!bOverwrite)
                return TRUE;
        };

        dwStatus = RegCreateKeyEx( HKEY_LOCAL_MACHINE,
                                   KeyName,
                                   0,
                                   NULL,
                                   REG_OPTION_NON_VOLATILE,
                                   0,
                                   NULL,
                                   &hKey,
                                   &dwDisp);

        if (dwStatus != ERROR_SUCCESS) {
            return FALSE;
        }

        pValueDesc = ValDesc;
        while (pValueDesc->Name) {
            switch (pValueDesc->Type) {
                case REG_DWORD:
                    dwValLen = sizeof(DWORD);
                    break;

                case REG_SZ:
                    dwValLen = (wcslen((LPWSTR)pValueDesc->Data) + 1) * sizeof(WCHAR);
                    break;

                case REG_MULTI_SZ:
                    pwStr = (LPWSTR)pValueDesc->Data;
                    dwVal = 0;
                    while (*pwStr) {
                        dwDisp = wcslen(pwStr) + 1;
                        dwVal += dwDisp;
                        pwStr += dwDisp;
                    }
                    dwVal++;

                    dwValLen = dwVal*sizeof(WCHAR);
                    break;
        
                default:
                    ASSERT(0);
                    RegCloseKey(hKey);
                    return FALSE;
                    break;
            }

            __try {
                
                dwStatus = RegSetValueEx( hKey,
                                          pValueDesc->Name,
                                          0,
                                          pValueDesc->Type,
                                          pValueDesc->Data,
                                          dwValLen );

            } __except ( EXCEPTION_EXECUTE_HANDLER ) {
                //
                // did you forgot to allocate space for pValueDesc->Data
                //
                RegCloseKey(hKey);
                return FALSE;
            }

            if (dwStatus != ERROR_SUCCESS ) {
                RegCloseKey(hKey);
                return FALSE;
            }

            pValueDesc++;
        }
    
    } else {

        if ( dwStatus != ERROR_SUCCESS ) {
            return FALSE;
        }

        pValueDesc = ValDesc;
        while (pValueDesc->Name) {
        
            dwStatus = RegQueryValueEx( hKey,
                                        pValueDesc->Name,
                                        NULL,
                                        &pValueDesc->Type,
                                        pValueDesc->Data,
                                        &pValueDesc->Size);

            if (dwStatus != ERROR_SUCCESS ) {
                RegCloseKey(hKey);
                return FALSE;
            }

            pValueDesc++;
        }

    }

    RegCloseKey(hKey);

    return (ERROR_SUCCESS == dwStatus);
}

⌨️ 快捷键说明

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