C++レジストリ操作

1625 ワード

プロセッサの名前を読み込みます.
#include <windows.h>
#include <stdio.h>

int main()
{
	LONG lResult;
	LONG lResult2;
	HKEY hKey;
	TCHAR tchData[64];
	DWORD dwSize;
	int nInput;
	lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, 
		"Hardware\\Description\\System\\CentralProcessor\\0",
		0,
		KEY_QUERY_VALUE,
		&hKey);
	if (ERROR_SUCCESS == lResult)
	{
		dwSize = sizeof (tchData);
		lResult2 = RegQueryValueEx(hKey, 
			"ProcessorNameString",
			NULL,
			NULL,
			(LPBYTE)tchData,
			&dwSize);
		if (ERROR_SUCCESS == lResult2)
		{
			printf("ProcessorName: %s
", tchData); scanf("%d", &nInput); } } RegCloseKey(hKey); return 0; }

POST:
#include <Windows.h>
#include <string.h>
#include <stdio.h>

int main()
{
	HKEY hKey;
	DWORD dwDisposition = REG_OPENED_EXISTING_KEY;
	LONG lResult;
	lResult = RegCreateKeyEx(HKEY_LOCAL_MACHINE,
		"Software\\Microsoft\\Windows\\CurrentVersion\\Run",
		0,
		NULL,
		REG_OPTION_NON_VOLATILE,
		KEY_ALL_ACCESS,
		NULL,
		&hKey,
		&dwDisposition);

	if (ERROR_SUCCESS == lResult)
	{
		TCHAR szModule[MAX_PATH];
		DWORD moduleLength;
		moduleLength = GetModuleFileName(NULL, szModule, MAX_PATH);
		if (moduleLength > 0)
		{
			printf("%s
", szModule); LONG lResult2; lResult2 = RegSetValueEx(hKey, "autorun", 0, REG_SZ, (BYTE *)szModule, strlen(szModule)); if (ERROR_SUCCESS == lResult2) { printf("autorun success
"); } } RegCloseKey(hKey); } return 0; }