0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

WindowsAPIでレジストリの操作

0
Posted at

レジストリの登録

RegCreateKeyExA 使い方
RegSetValueExA 使い方

C:\Users\test\kaihatsu>gcc main.c -o main.exe

C:\Users\test\kaihatsu>main
image
main.c
#include <windows.h>

int main() {
    HKEY hKey;
    RegCreateKeyExA(
        HKEY_CURRENT_USER,
        "Software\\TestApp1220",
        0,
        NULL,
        0,
        KEY_WRITE,
        NULL,
        &hKey,
        NULL
    );

    const char *value = "1234.5678";
    RegSetValueExA(
        hKey,
        "Version",
        0,
        REG_SZ,
        (const BYTE*)value,
        strlen(value) + 1
    );

    RegCloseKey(hKey);
    return 0;
}

レジストリの削除

RegDeleteKeyA 使い方

C:\Users\test\kaihatsu>gcc main.c -o main.exe

C:\Users\test\kaihatsu>main
image
main.c
#include <windows.h>
#include <stdio.h>

int main() {
    LONG result;

    result = RegDeleteKeyA(
        HKEY_CURRENT_USER,
        "Software\\TestApp1220"
    );

    return 0;
}

使ってみる

設定値として使用

レジストリに登録した値を、キー指定で取得する。自作のソフトで必要な設定値を自由に設定可能。

image
C:\Users\test\kaihatsu>gcc main.c -o main.exe

C:\Users\test\kaihatsu>main
Value: AAAABBBBCCCC

C:\Users\test\kaihatsu>
main.c
#include <windows.h>
#include <stdio.h>

int main() {
    HKEY hKey;
    char buffer[256];
    DWORD bufferSize = sizeof(buffer);
    DWORD type;

    // キーを開く
    if (RegOpenKeyExA(
            HKEY_CURRENT_USER,
            "Software\\TestApp1220",
            0,
            KEY_READ,
            &hKey) != ERROR_SUCCESS) {
        printf("Failed to open key\n");
        return 1;
    }

    // 値を読む
    if (RegQueryValueExA(
            hKey,
            "TESTMSG1220",
            NULL,
            &type,
            (LPBYTE)buffer,
            &bufferSize) == ERROR_SUCCESS) {

        printf("Value: %s\n", buffer);
    } else {
        printf("Failed to read value\n");
    }

    RegCloseKey(hKey);
    return 0;
}

「プログラムと機能」に登録

Software\Microsoft\Windows\CurrentVersion\Uninstall\に登録すると「プログラムと機能」に表示される

image image
main.c
#include <windows.h>
#include <stdio.h>

int main() {
    HKEY hKey;
    const char *subkey =
        "Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\TestApp1220";

    if (RegCreateKeyExA(
            HKEY_CURRENT_USER,
            subkey,
            0, NULL, 0,
            KEY_WRITE,
            NULL,
            &hKey,
            NULL) != ERROR_SUCCESS) {
        printf("Failed to create key\n");
        return 1;
    }

    const char *name = "TestApp1220";
    const char *version = "1234.5678";
    const char *publisher = "HISSHA";
    const char *uninstall = "C:\\TestApp\\uninstall.exe";

    RegSetValueExA(hKey, "DisplayName", 0, REG_SZ,
        (const BYTE*)name, strlen(name)+1);

    RegSetValueExA(hKey, "DisplayVersion", 0, REG_SZ,
        (const BYTE*)version, strlen(version)+1);

    RegSetValueExA(hKey, "Publisher", 0, REG_SZ,
        (const BYTE*)publisher, strlen(publisher)+1);

    RegSetValueExA(hKey, "UninstallString", 0, REG_SZ,
        (const BYTE*)uninstall, strlen(uninstall)+1);

    RegCloseKey(hKey);

    printf("Registered in Uninstall list.\n");
    return 0;
}
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?