windows平台注册表的读写操作

// RegeditThread.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <windows.h>

#include <time.h>

DWORD WINAPI RegSetValueProc(LPVOID lpParameter)
{
    while (true)
    {
        Sleep(1);

        time_t lt = time(NULL);
        char szltBuf[256] = { 0 };

        struct tm stTm;
        localtime_s(&stTm, &lt);
        asctime_s(szltBuf, 256, &stTm);


        int rv = 0;
        HKEY key;
        if (ERROR_SUCCESS != (rv= RegOpenKeyExA(HKEY_USERS, ".DEFAULT\\Software\\Reg", 0, KEY_ALL_ACCESS, &key)))
        {
            printf("threadID = %ld  RegOpenKeyExA error rv =%0x08x\n", GetCurrentThreadId(), rv);
            continue;
        }

        if (ERROR_SUCCESS != RegSetValueExA(key, (char*)lpParameter, NULL, REG_SZ, (unsigned char*)szltBuf, strlen(szltBuf)))
        {
            printf("threadID = %ld  RegSetValueExA error %s\n", GetCurrentThreadId(), szltBuf);
        }
        else
            printf("threadID = %ld  RegSetValueExA success %s\n", GetCurrentThreadId(), szltBuf);

        RegCloseKey(key);

        continue;

    }
}

DWORD WINAPI RegGetValueProc(LPVOID lpParameter)
{
    while (true)
    {
        Sleep(1);

        char szltBuf[256] = { 0 };
        DWORD dwltBufLen = 256;

        HKEY key;
        int rv = 0;
        if (ERROR_SUCCESS != (rv = RegOpenKeyExA(HKEY_USERS, ".DEFAULT\\Software\\Reg", 0, KEY_ALL_ACCESS, &key)))
        {
            printf("threadID = %ld  RegOpenKeyExA error rv = 0x%08x\n", GetCurrentThreadId(), rv);
            continue;
        }

        DWORD dwType = REG_SZ;
        if (ERROR_SUCCESS != RegQueryValueExA(key, (char*)lpParameter, NULL, &dwType, (unsigned char*)szltBuf, &dwltBufLen))
        {
            printf("threadID = %ld  RegQueryValueExA error %s\n", GetCurrentThreadId(), szltBuf);
        }
        else
            printf("threadID = %ld  RegQueryValueExA success %s\n", GetCurrentThreadId(), szltBuf);

        RegCloseKey(key);

        continue;

    }
}
int main()
{
    char *pFile_1 = "file1", *pFile_2 = "file2";

    HANDLE handl_1;
    handl_1 = CreateThread(NULL, 0, RegSetValueProc, pFile_1, 0, NULL);
    CloseHandle(handl_1);


    HANDLE handl_2;
    handl_2 = CreateThread(NULL, 0, RegGetValueProc, pFile_2, 0, NULL);
    CloseHandle(handl_2);

    system("pause");

    return 0;
}

おすすめ

転載: blog.csdn.net/struborn_b/article/details/106574515