わびさびサンプルソース

WindowsやHTML5などのプログラムのサンプルコードやフリーソフトを提供します。

レジストリキーの作成

レジストリキーの作成するには、RegCreateKeyEx()関数を使います。

#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <string>
#include <windows.h>



/*
	レジストリキーの作成
*/
int _tmain
(
	  int argc
	, _TCHAR* argv[]
)
{
	// std::wcoutのロケールを設定
	std::wcout.imbue( std::locale( "", std::locale::ctype ) );

	// エラーコード
	HRESULT hResult = S_OK;

	{
		// 戻り値
		DWORD dwResult = 0;

		// HKEY
		HKEY hKey = NULL;

		// キーが存在していたかどうか
		DWORD dwDisposition = 0;

		/*
			レジストリキーの作成
		*/
		dwResult = ::RegCreateKeyEx(
				  HKEY_LOCAL_MACHINE			// レジストリキー
				, L"SOFTWARE¥¥TestApplication"	// レジストリサブキー
				, 0								// Reserved(0固定)
				, NULL							// クラス(オブジェクトタイプ)
				, REG_OPTION_NON_VOLATILE		// 格納方法(不揮発)
				, KEY_ALL_ACCESS				// アクセス権
				, NULL							// Security Attributes
				, &hKey							// キーハンドルの受け取り位置
				, &dwDisposition				// キーが存在していたかどうかを示す値
			);
		if ( ERROR_SUCCESS != dwResult ) {

			// エラー
			hResult = ::HRESULT_FROM_WIN32( dwResult );
			goto err;
		}

		switch( dwDisposition ) {
		case REG_CREATED_NEW_KEY:     std::wcout << L"新しいキーを作成しました。"   << std::endl; break;
		case REG_OPENED_EXISTING_KEY: std::wcout << L"キーは既に存在していました。" << std::endl; break;
		}

err:
		// キーハンドルの破棄
		if ( NULL != hKey ) {
			::RegCloseKey( hKey );
		}
	}

	// 処理結果を返す
	return( 0 );
}



実行結果

新しいキーを作成しました。






わびさびサンプルソース

WindowsやHTML5などのプログラムのサンプルコードやフリーソフトを提供します。