/*----------------------------------------------------------------------------*/
//  概要
//    CRegistry クラスのインターフェイス
//  作成者
//    ぺがらぼ
/*----------------------------------------------------------------------------*/

#if !defined(AFX_REGISTRY_H__INCLUDED_)
#define AFX_REGISTRY_H__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

/* 定数宣言 */
#ifndef __MAX_BUFFER_SIZE__
#define __MAX_BUFFER_SIZE__
  // バッファーサイズ
  const int MAX_BUFFER_SIZE = 32767;
#endif // __MAX_BUFFER_SIZE__

/*----------------------------------------------------------------------------*/
//  クラス : CRegistry
//
//  1.日本語名
//    レジストリ情報操作
//  2.概要
//    レジストリ情報を操作するクラス
//  3.機能説明
//    なし。
//  4.備考
//    なし。
/*----------------------------------------------------------------------------*/
class CRegistry
{
public:
  // 関数定義

  // INIファイル用
  UINT    GetIniKeyInt(CString strSection, CString strKey, CString strFile);
  float   GetIniKeyFlo(CString strSection, CString strKey, CString strFile);
  CString GetIniKeyStr(CString strSection, CString strKey, CString strFile);
  int     GetIniKeyStrArray(CString strSection, CString strFile, 
                            CStringArray *ArrayKeyName, CStringArray *ArrayKeyData);
  BOOL    SetIniKeyStr(CString strSection, CString strKey, CString strValue, CString strFile);
  // レジストリ用
  CString GetRegValue(HKEY hKey, CString SubKey, CString KeyName, CString DefaultString);
  CString GetRegValue_CU(CString SubKey, CString KeyName, CString DefaultString);
  BOOL    SetRegValueStr(CString SubKey, CString KeyName, CString strValue);
};

#endif // !defined(AFX_REGISTRY_H__INCLUDED_)Registry.h
****************************************************************************************************
↓Registry.cpp


// Registry.cpp: CRegistry クラスのインプリメンテーション
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Registry.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

/*----------------------------------------------------------------------------*/
//  1.日本語名
//    INI情報取得(数字)
//  2.パラメタ説明
//    CString strSection    : セクション
//    CString strKey        : キー
//    CString strFIle       : INIファイルパス
//  3.概要
//    パラメタ指定したINI情報取得(数字)を取得する。
//  4.機能説明
//    (1)  INIファイルより値を取得する(数値)
//  5.戻り値
//    キーの値
//  6.備考
//    なし。
/*----------------------------------------------------------------------------*/
UINT CRegistry::GetIniKeyInt(CString strSection, CString strKey, CString strFile)
{
  UINT ret;
  // APIによりINIファイルキー値取得
  ret = GetPrivateProfileInt( strSection, strKey, 0, strFile );

  return(ret);
}

/*----------------------------------------------------------------------------*/
//  1.日本語名
//    INI情報取得(float)
//  2.パラメタ説明
//    CString strSection    : セクション
//    CString strKey        : キー
//    CString strFIle       : INIファイルパス
//  3.概要
//    パラメタ指定したINI情報取得(float)を取得する。
//  4.機能説明
//    (1)  INIファイルより値を取得するし、文字を浮動小数点型に変換して返す。
//  5.戻り値
//    キーの値
//  6.備考
//    なし。
/*----------------------------------------------------------------------------*/
float CRegistry::GetIniKeyFlo(CString strSection, CString strKey, CString strFile)
{
  char buff[MAX_PATH];
  CString str;
  float ret;

  // APIによりINIファイルキー値取得
  GetPrivateProfileString(strSection, strKey, "", buff, 1024, strFile);

  str = CString(buff);
  ret = float(atof(str));

  return (ret);
}

/*----------------------------------------------------------------------------*/
//  1.日本語名
//    INI情報取得(文字)
//  2.パラメタ説明
//    CString strSection    : セクション
//    CString strKey        : キー
//    CString strFIle       : INIファイルパス
//  3.概要
//    パラメタ指定したINI情報取得(文字)を取得する。
//  4.機能説明
//    (1)  INIファイルより値を取得する(文字)
//  5.戻り値
//    キーの値
//  6.備考
//    なし。
/*----------------------------------------------------------------------------*/
CString CRegistry::GetIniKeyStr(CString strSection, CString strKey, CString strFile)
{
  char buff[MAX_PATH];
  CString ret;

  // APIによりINIファイルキー値取得
  GetPrivateProfileString(strSection, strKey, "", buff, 1024, strFile);

  ret = CString(buff);

  return (ret);
}

/*----------------------------------------------------------------------------*/
//  1.日本語名
//    INI情報取得(文字配列)
//  2.パラメタ説明
//    CString strSection    : セクション名
//    CString strFIle       : INIファイルパス
//  3.概要
//    パラメタ指定したINI情報取得(文字配列)を取得する。
//  4.機能説明
//    (1)  INIファイルよりキー名が不特定多数の場合のキー値を取得する(文字配列)
//  5.戻り値
//    int 配列の最大のインデックス値
//    ポインタによる戻り値
//      CStringArray *ArrayKeyName : 配列によるキー名
//      CStringArray *ArrayKeyData : 配列によるキー値
//  6.備考
//    なし。
/*----------------------------------------------------------------------------*/
int CRegistry::GetIniKeyStrArray(CString strSection, CString strFile, 
                                CStringArray *ArrayKeyName, CStringArray *ArrayKeyData)
{
  int           iRet;
  char          buff[MAX_BUFFER_SIZE];
  char          Getbuff[MAX_BUFFER_SIZE];
  CString       WorkStr;

  // APIによりINIファイルのセクション内にあるキー名取得
  GetPrivateProfileString(strSection, NULL, "", buff, MAX_BUFFER_SIZE, strFile);
  
  for( int i = 0; i <= MAX_BUFFER_SIZE; i += 2 ){
    WorkStr = CString(buff[i]); // キー名を細分化
    if( WorkStr == "" ) break; // 最後ならば抜ける
    // APIによりINIファイルキー値取得
    GetPrivateProfileString(strSection, WorkStr, "", Getbuff, MAX_BUFFER_SIZE, strFile);
    ArrayKeyName->Add(WorkStr);           // キー名格納
    ArrayKeyData->Add(CString(Getbuff));  // キー値格納
  }
  iRet = ArrayKeyName->GetSize() - 1;
  return (iRet);
}

/*----------------------------------------------------------------------------*/
//  1.日本語名
//    INI情報設定(文字)
//  2.パラメタ説明
//    CString strSection    : セクション
//    CString strKey        : キー
//    CString strValue      : キー値
//    CString strFIle       : INIファイルパス
//  3.概要
//    INIファイルに値を設定する(文字)。
//  4.機能説明
//    (1)  キー値が[""]であれば、[NULL]をセットしてキーを削除する。
//    (2)  それ以外はキー値を指定したキーにセットする。
//  5.戻り値
//    BOOL : 成功時 TRUE
//  6.備考
//    なし。
/*----------------------------------------------------------------------------*/
BOOL CRegistry::SetIniKeyStr(CString strSection, CString strKey, CString strValue, CString strFile)
{
  BOOL BRet;

  // APIによりINIファイルキー値取得
  if( strValue != "" )
    BRet = (WritePrivateProfileString(strSection, strKey, strValue, strFile) == TRUE);
  else
    BRet = (WritePrivateProfileString(strSection, strKey, NULL, strFile) == TRUE);
  return (BRet);
}

/*----------------------------------------------------------------------------*/
//  1.日本語名
//    レジストリ値取得
//  2.パラメタ説明
//    HKEY    hKey;         : キーのハンドル
//    CString SubKey        : 取得するサブキーのパス
//    CString KeyName       : 取得する値の名前
//    CString DefaultString : データが無い場合の戻り値
//  3.概要
//    APIによりレジストリの値を取得する。
//  4.機能説明
//    (1)  レジストリから値を取得する(文字)
//  5.戻り値
//    CString : レジストリの値
//  6.備考
//    無し。
/*----------------------------------------------------------------------------*/
CString CRegistry::GetRegValue(HKEY hKey, CString SubKey, CString KeyName, CString DefaultString)
{
  LONG    lResult;    // 関数の戻り値を格納する
  DWORD   dwType;          // 値の種類を受け取る
  DWORD   dwSize;          // データのサイズを受け取る
  char    szBuf[MAX_PATH]; // データ格納用
  CString SRet;       // 戻り値を代入

  // レジストリキーを開く
  lResult = RegOpenKeyEx(hKey, SubKey, 0, KEY_ALL_ACCESS, &hKey);
  if( lResult == ERROR_SUCCESS ){ // 成功チェック
    // レジストリ値の問い合わせ
    lResult = RegQueryValueEx(hKey, KeyName, NULL, &dwType, NULL, &dwSize); // サイズの取得
    lResult = RegQueryValueEx(hKey, KeyName, NULL, &dwType, (LPBYTE)szBuf,&dwSize); // データ取得
    if( lResult == ERROR_SUCCESS ) // 成功チェック
      SRet.Format("%s", szBuf);
    else
      // データが無い場合の戻り値を引数に渡す
      SRet = DefaultString;

    lResult = RegCloseKey(hKey); // レジストリキーを閉じる
  }
  return SRet;
}

/*----------------------------------------------------------------------------*/
//  1.日本語名
//    HKEY_CURRENT_USERからレジストリ値を取得
//  2.パラメタ説明
//    CString SubKey        : 取得するサブキーのパス
//    CString KeyName       : 取得する値の名前
//    CString DefaultString : データが無い場合の戻り値
//  3.概要
//    APIによりレジストリの値を取得する。
//  4.機能説明
//    (1)  レジストリから値を取得する(文字)
//  5.戻り値
//    CString : レジストリの値
//  6.備考
//    レジストリキーの場所は[HKEY_CURRENT_USER]の[Software]とする。
/*----------------------------------------------------------------------------*/
CString CRegistry::GetRegValue_CU(CString SubKey, CString KeyName, CString DefaultString)
{
  HKEY    hKey;       // キーのハンドル
  LONG    lResult;    // 関数の戻り値を格納する
  DWORD   dwType;          // 値の種類を受け取る
  DWORD   dwSize;          // データのサイズを受け取る
  char    szBuf[MAX_PATH]; // データ格納用
  CString SRet;       // 戻り値を代入

  SubKey = "Software\\" + SubKey; // SOFTWAREのサブキーを連結
  // レジストリキーを開く
  lResult = RegOpenKeyEx(HKEY_CURRENT_USER, SubKey, 0, KEY_ALL_ACCESS, &hKey);
  if( lResult == ERROR_SUCCESS ){ // 成功チェック
    // レジストリ値の問い合わせ
    lResult = RegQueryValueEx(hKey, KeyName, NULL, &dwType, NULL, &dwSize); // サイズの取得
    lResult = RegQueryValueEx(hKey, KeyName, NULL, &dwType, (LPBYTE)szBuf,&dwSize); // データ取得
    if( lResult == ERROR_SUCCESS ) // 成功チェック
      SRet.Format("%s", szBuf);
    else
      // データが無い場合の戻り値を引数に渡す
      SRet = DefaultString;

    lResult = RegCloseKey(hKey); // レジストリキーを閉じる
  }
  return (SRet);
}

/*----------------------------------------------------------------------------*/
//  1.日本語名
//    レジストリ情報設定(文字)
//  2.パラメタ説明
//    CString SubKey          : サブキー
//    CString KeyName         : キー名
//    CString strValue        : キー値
//  3.概要
//    レジストリに値を設定する(文字)。
//  4.機能説明
//    (1)  キー値を指定したキーにセットする。
//  5.戻り値
//    BOOL : 成功時 TRUE
//  6.備考
//    レジストリキーの場所は[HKEY_CURRENT_USER]の[Software]とする。
/*----------------------------------------------------------------------------*/
BOOL CRegistry::SetRegValueStr(CString SubKey, CString KeyName, CString strValue)
{
  HKEY  hKey;     // キーのハンドル
  DWORD dwDisp;   // 結果を受け取る
  LONG  lResult;  // 関数の戻り値を格納する
  char  szBuf[MAX_PATH]; // データ格納用
  BOOL  BRet = FALSE; // まずは失敗の値を格納
  
  // レジストリキーを開く
  lResult = RegOpenKeyEx(HKEY_CURRENT_USER, "Software", 0, KEY_ALL_ACCESS, &hKey);
  
  if( lResult == ERROR_SUCCESS ){ // 成功チェック
    // サブキーの作成
    lResult = RegCreateKeyEx( hKey,
                              SubKey,
                              NULL,
                              NULL,
                              REG_OPTION_NON_VOLATILE,
                              KEY_ALL_ACCESS,
                              NULL,
                              &hKey,
                              &dwDisp);
    if( lResult == ERROR_SUCCESS ){ // 成功チェック
      // キー値を格納
      strcpy(szBuf, strValue);
      lResult = RegSetValueEx(hKey, KeyName, 0, REG_SZ, (LPBYTE)szBuf, strlen(szBuf) + 1);
      if( lResult == ERROR_SUCCESS ) // 成功チェック
        BRet = TRUE; // 書込み成功
    }
    lResult = RegCloseKey(hKey); // レジストリを閉じる
  }

  return (BRet);
}