天天看點

7、系統資料庫讀寫的一個例子

7、系統資料庫讀寫的一個例子
7、系統資料庫讀寫的一個例子

系統資料庫讀寫的一個例子

#include "stdafx.h"

#include "iostream"

#include "string"

#include "Windows.h"

//系統資料庫項

#ifndef TEST_APP_REG

#define TEST_APP_REG "Software\\MySoft\\Common\\InstallRoot"

#endif

//系統資料庫中虛拟磁盤映射的盤符鍵

#ifndef TEST_APP_REG_DRIVE

#define TEST_APP_REG_DRIVE "Driver"

#ifndef RETURN_OK

#define RETURN_OK 1

#ifndef RETURN_ERROR

#define RETURN_ERROR 0

using namespace std;

int GetRegStringValue(const wstring &wstrPath, const wstring &wstrName, wstring &wstrValue)

{

HKEY hkey;

LONG lErrCode;

DWORD dwPathSize = 512;

BYTE bPath[512] = {0};

lErrCode = RegOpenKeyEx(HKEY_LOCAL_MACHINE ,

(LPCWSTR)(wstrPath.c_str()),

NULL,

KEY_QUERY_VALUE,

&hkey);

if (lErrCode != ERROR_SUCCESS)

return lErrCode;

}

lErrCode = RegQueryValueEx(hkey, (LPCWSTR)(wstrName.c_str()), NULL, NULL,

(LPBYTE)bPath, &dwPathSize);

if (ERROR_SUCCESS == lErrCode)

//lint -save -e534

wstrValue.append((wchar_t *) bPath);

//lint -restore

RegCloseKey(hkey);

int CreateRegForSoftward()

LPCWSTR lpSubKey = L"Software\\MySoft\\Common\\InstallRoot";

LPCWSTR lpSubKeyValue = L"Driver";

CONST BYTE lpData[512] = {"Y"};

HKEY hMyKey, hMyKey1;

LONG lRet = RegCreateKeyEx(HKEY_LOCAL_MACHINE,lpSubKey,NULL,NULL,REG_OPTION_NON_VOLATILE,

KEY_ALL_ACCESS,NULL,&hMyKey,NULL);

if (ERROR_SUCCESS == lRet)

lRet = RegSetValueEx(hMyKey,lpSubKeyValue,NULL,REG_SZ,lpData,4);

if (lRet == ERROR_SUCCESS)

RegCloseKey(hMyKey);

return RETURN_OK;

return RETURN_ERROR;

int main()

wstring wstrDrive, wstrPath = L"Y:\WINDDK";

CreateRegForSoftward();

if (0 != GetRegStringValue(TEXT(TEST_APP_REG), TEXT(TEST_APP_REG_DRIVE), wstrDrive))

return S_FALSE;

if (wstrPath.empty() || wstrDrive.empty())

std::cout<< _wcsnicmp(wstrPath.c_str(), wstrDrive.c_str(), 1);

繼續閱讀