winutil.c

来自「开放源码的编译器open watcom 1.6.0版的源代码」· C语言 代码 · 共 512 行 · 第 1/2 页

C
512
字号
/****************************************************************************
*
*                            Open Watcom Project
*
*    Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
*
*  ========================================================================
*
*    This file contains Original Code and/or Modifications of Original
*    Code as defined in and that are subject to the Sybase Open Watcom
*    Public License version 1.0 (the 'License'). You may not use this file
*    except in compliance with the License. BY USING THIS FILE YOU AGREE TO
*    ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is
*    provided with the Original Code and Modifications, and is also
*    available at www.sybase.com/developer/opensource.
*
*    The Original Code and all software distributed under the License are
*    distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
*    EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM
*    ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
*    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
*    NON-INFRINGEMENT. Please see the License for the specific language
*    governing rights and limitations under the License.
*
*  ========================================================================
*
* Description:  WHEN YOU FIGURE OUT WHAT THIS FILE DOES, PLEASE
*               DESCRIBE IT HERE!
*
****************************************************************************/


#if defined( __NT__ )  ||  defined( __WINDOWS__ )
    #include <windows.h>
    #include <commdlg.h>
    #include "fontstr.h"
#endif
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define INCL_WINSHELLDATA
#include "gui.h"
#include "setup.h"
#include "setupinf.h"
#include "utils.h"
#include "genvbl.h"


// *********************** Function for writing to WIN.INI *******************

#if defined( __NT__ )

void CreateRegEntry( char *hive_key, char *app_name, char *key_name,
                     char *value, char *file_name, bool add )
{
    char                buf[_MAX_PATH];
    long                rc;
    HKEY                hkey1;
    DWORD               disposition;
    DWORD               type;
    DWORD               old_type;
    int                 len;
    long                dword_val;
    HKEY                key;
    unsigned char       *bin_buf;
    int                 i;

    strcpy( buf, file_name );
    len = strlen( buf );
    if( buf[len-1] != '\\' ) {
        strcat( buf, "\\" );
    }
    strcat( buf, app_name );
    if( stricmp( hive_key, "local_machine" ) == 0 ) {
        key = HKEY_LOCAL_MACHINE;
    } else if( stricmp( hive_key, "current_user" ) == 0 ) {
        key = HKEY_CURRENT_USER;
    } else {
        key = HKEY_LOCAL_MACHINE;
    }
    if( add ) {
        rc = RegCreateKeyEx( key, buf, 0, NULL, REG_OPTION_NON_VOLATILE,
                             KEY_WRITE, NULL, &hkey1, &disposition );
        if( key_name[0] != '\0' ) {
            if( value[0] == '#' ) {     // dword
                dword_val = atoi( value + 1 );
                rc = RegSetValueEx( hkey1, key_name, 0,
                        REG_DWORD, (LPBYTE) &dword_val, sizeof( long ) );
            } else if( value[0] == '%' ) {      // binary
                ++value;
                len = strlen( value );
                bin_buf = malloc( len / 2 );
                if( bin_buf != NULL ) {
                    for( i = 0; i < len / 2; ++i ) {
                        if( tolower( value[ 0 ] ) >= 'a' ) {
                            bin_buf[ i ] = value[ 0 ] - 'a' + 10;
                        } else {
                            bin_buf[ i ] = value[ 0 ] - '0';
                        }
                        bin_buf[ i ] = bin_buf[ i ] * 16;
                        if( tolower( value[ 1 ] ) >= 'a' ) {
                            bin_buf[ i ] += value[ 1 ] - 'a' + 10;
                        } else {
                            bin_buf[ i ] += value[ 1 ] - '0';
                        }
                        value += 2;
                    }
                    rc = RegSetValueEx( hkey1, key_name, 0,
                        REG_BINARY, (LPBYTE) bin_buf, len / 2 );
                    free( bin_buf );
                }
            } else {
                rc = RegQueryValueEx( hkey1, key_name, NULL, &old_type,
                                      NULL, NULL );
                if( rc == 0 ) {
                    type = old_type;
                } else {
                    type = REG_SZ;
                }
                rc = RegSetValueEx( hkey1, key_name, 0,
                        type, value, strlen( value ) + 1 );
            }
        }
    } else {
        rc = RegDeleteKey( key, buf );
    }
}


bool GetRegString( HKEY hive, char *section, char *value,
                   char *buffer, DWORD buff_size )
/************************************************/
{
    HKEY                hkey;
    LONG                rc;
    DWORD               type;
    bool                ret;

    ret = FALSE;
    rc = RegOpenKeyEx( hive, section, 0L, KEY_ALL_ACCESS, &hkey );
    if( rc == ERROR_SUCCESS ) {
        // get the value
        rc = RegQueryValueEx( hkey, value, NULL, &type, (LPBYTE)buffer, &buff_size );
        RegCloseKey( hkey );
        ret = ( rc == ERROR_SUCCESS );
    }
    return( ret );
}

DWORD ConvertDataToDWORD( BYTE *data, DWORD num_bytes, DWORD type )
/*****************************************************************/
{
    int                         i;
    DWORD                       temp;

    if( type == REG_DWORD || type == REG_DWORD_LITTLE_ENDIAN || type  == REG_BINARY ) {
        return (DWORD)(*data);
    } else if( type == REG_DWORD_BIG_ENDIAN ) {
        temp = 0;
        for( i = 0; i < num_bytes; i++ ) {
            temp |= ( ( DWORD ) data[ num_bytes- 1 - i ] ) << ( i * 8 );
        }
        return temp;
    }
    return 0;
}

BYTE *ConvertDWORDToData( DWORD number, DWORD type )
/**************************************************/
{
    int                         i;
    static BYTE                 buff[ 5 ];

    memset( buff, 0, sizeof( buff ) );
    if( type == REG_DWORD || type == REG_DWORD_LITTLE_ENDIAN || type  == REG_BINARY ) {
        memcpy( buff, &number, sizeof( number ) );
    } else if( type == REG_DWORD_BIG_ENDIAN ) {
        for( i = 0; i < sizeof( number ); i++ ) {
            buff[ i ] = ( ( BYTE * )( &number ) )[ sizeof( number ) - 1 - i ];
        }
    }
    return buff;
}

signed int AddToUsageCount( char *path, signed int value )
/********************************************************/
{
    HKEY                        key_handle;
    LONG                        result;
    DWORD                       value_type;
    DWORD                       orig_value;
    BYTE                        buff[ 5 ];
    DWORD                       buff_size =                     sizeof( buff );
    signed int                  return_value;
    LONG                        new_value;

    result = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
                           "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\SharedDLLs",
                           0,
                           KEY_ALL_ACCESS,
                           &key_handle );
    if( result != ERROR_SUCCESS ) {
        return -1;
    }

    result = RegQueryValueEx( key_handle, path, 0, &value_type, buff, &buff_size );
    if( result != ERROR_SUCCESS ) {
        orig_value = 0;
        value_type = REG_DWORD;
    } else {
        orig_value = ConvertDataToDWORD( buff, buff_size, value_type );
    }

    // Don't increment if reinstalling and file already has a nonzero count
    if( GetVariableIntVal( "ReInstall" ) != 0
        && orig_value != 0
        && value > 0 ) {
        value = 0;
    }

    new_value = (long) orig_value + value;

    if( new_value > 0 ) {
        memcpy( buff, ConvertDWORDToData( new_value, value_type ), sizeof( new_value ) );
        result = RegSetValueEx( key_handle, path, 0, value_type, (LPBYTE)&buff, sizeof( new_value ) );
    } else if( new_value == 0 ) {
        result = RegDeleteValue( key_handle, path );
    }

    return_value = new_value;

    if( new_value >= 0 && result != ERROR_SUCCESS ) return_value = -1;

    if( RegFlushKey( key_handle ) != ERROR_SUCCESS ) return_value = -1;

    if( RegCloseKey( key_handle ) != ERROR_SUCCESS ) return_value = -1;

    return return_value;
}

signed int IncrementDLLUsageCount( char *path )
/*********************************************/
{
    return AddToUsageCount( path, 1 );
}

signed int DecrementDLLUsageCount( char *path )
/*********************************************/
{
    return AddToUsageCount( path, -1 );
}

⌨️ 快捷键说明

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