わびさびサンプルソース

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

レジストリの書き込み

レジストリに値を書き込むには、RegSetValueEx()関数を使います。

レジストリの書き込み
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#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;
 
        /*
            レジストリオープン
        */
        dwResult = ::RegOpenKeyEx(
                  HKEY_LOCAL_MACHINE            // レジストリキー
                , L"SOFTWARE¥¥TestApplication"  // レジストリサブキー
                , 0                             // Reserved(0固定)
                , KEY_SET_VALUE                 // アクセス権
                , &hKey                         // キーハンドルの受け取り位置
            );
        if ( ERROR_SUCCESS != dwResult ) {
 
            // エラー
            hResult = ::HRESULT_FROM_WIN32( dwResult );
            goto err;
        }
 
 
        /*
            レジストリに値を書き込み
        */
        {
            std::wstring strWriteValue = L"Test Value String";
 
            dwResult = ::RegSetValueEx(
                      hKey                                  // キーハンドル
                    , L"TestVersion"                        // ValueName
                    , 0                                     // Reserved(0固定)
                    , REG_SZ                                // データ型
                    , (LPBYTE)strWriteValue.c_str()         // 書き込み内容
                    , strWriteValue.size() * sizeof(TCHAR// 書き込みサイズ(BYTE)
                );
            if ( ERROR_SUCCESS != dwResult ) {
 
                // エラー
                hResult = ::HRESULT_FROM_WIN32( dwResult );
                goto err;
            }
        }
 
        std::wcout << L"書き込みました。" << std::endl;
 
err:
        // キーハンドルの破棄
        if ( NULL != hKey ) {
            ::RegCloseKey( hKey );
        }
    }
 
    // 処理結果を返す
    return( 0 );
}

実行結果

書き込みました。






わびさびサンプルソース

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