读
bool readReg()
{
HKEY hKey;
char sz[256];
DWORD dwtype;
DWORD sl=256;
LONG result=RegOpenKey(HKEY_LOCAL_MACHINE,"software\\company\\",&hKey);
result=RegQueryValueEx(hKey,"num",NULL,&dwtype,(LPBYTE)sz,&sl);
RegCloseKey(hKey);
MessageBox(sz);
return true;
}
写
bool writeReg()
{
HKEY hKey;
unsigned char sz[10];
strcpy((char*)sz,"800");
DWORD dwtype=REG_SZ;
DWORD sl=10;
LONG result=RegOpenKey(HKEY_LOCAL_MACHINE,"software\\company\\",&hKey);
result=RegSetValueEx(hKey,"num",NULL,dwtype,sz,sl);
RegCloseKey(hKey);
return true;
}
下面是一个更为完善的例子:
int CEaOilCanApp::ReadReg()
{
HKEY hKey;
int reValue;
DWORD dwtype;
DWORD sl=256;
char sz[256];
LONG lResult=RegOpenKey(HKEY_LOCAL_MACHINE,"SOFTWARE\\EaOilCan",&hKey);
if (lResult !=ERROR_SUCCESS)
{
AfxMessageBox("读取信息错误!");
return -1;
}
lResult=RegQueryValueEx(hKey,"num",NULL,&dwtype,(LPBYTE)sz,&sl);
if(lResult != ERROR_SUCCESS)
{
AfxMessageBox("读取信息错误!");
return -1;
}
reValue=atoi(sz);
RegCloseKey(hKey);
return reValue;
}
BOOL CEaOilCanApp::WriteReg(int nValue)
{
HKEY hKey;
unsigned char sz[10];
char sTemp[10];
DWORD sl=10;
_itoa(nValue,sTemp,10);
strcpy((char*)sz,sTemp);
DWORD dwtype=REG_SZ;
LONG lResult=RegOpenKey(HKEY_LOCAL_MACHINE,"SOFTWARE\\EaOilCan",&hKey);
if (lResult != ERROR_SUCCESS)
{
AfxMessageBox("读取信息错误!");
return FALSE;
}
lResult=RegSetValueEx(hKey,"num",NULL,dwtype,sz,sl);
if(lResult != ERROR_SUCCESS)
{
AfxMessageBox("读取信息错误!");
return FALSE;
}
RegCloseKey(hKey);
return TRUE;
}
阅读(1655) | 评论(0) | 转发(0) |