mfc写注册表与写文件代码

函数声明

直接把ini文件放在debug下或者release版本下

用注册表时要人为的自定义注册表目录

//用注册表存储原ini文件中的信息
#define reg_dir "Software\\TempoTec\\HIFIER\\"  //x86

#define reg_dir_custom "Software\\TempoTec\\HIFIER\\Custom\\"  //x86

//#define reg_dir "Software\\Wow6432Node\\TempoTec\\HIFIER\\" //x64
//#define reg_dir_custom "Software\\Wow6432Node\\TempoTec\\HIFIER\\Custom\\"  //x64

//从文件中读取字符串以及写字符串信息

BOOL WriteStrByName(CString file,CString section,CString key,CString str);
int GetIntByName(CString file,CString section,CString key);
CString GetStrByName(CString file,CString section,CString key);
CString GetSectionByName(CString file,CString section);

BOOL WriteSectionByName(CString file,CString section,CString str);

int GetIntByNameReg(CString default_dir, CString section, CString key);//读写注册表
CString GetStrByNameReg(CString default_dir, CString section, CString key);//读写注册表
bool WriteStrByNameReg(CString default_dir, CString section, CString key, CString str);//读写注册表
LPBYTE CString_To_LPBYTE(CString str);//读写注册表
void Syt_LockCtrlPos(CWnd* pm_parent,CWnd* pm_ctrl, int x, int y);
void CreateItemByNameReg(CString default_dir, CString section, CString key, CString strPos);//创建注册表
void DelItemByNameReg(CString default_dir, CString section);//删除注册表项
void EnumItemByNameReg(CString default_dir, CComboBox &customName);

函数实现

CString GetStrByName(CString file, CString section, CString key)
{
TCHAR szFileName[MAX_PATH];
CString strFileName;
CString strDirectory;
::GetModuleFileName(NULL,szFileName,MAX_PATH);
strFileName=szFileName;
int nIndex=strFileName.ReverseFind('\\');
strDirectory=strFileName.Left(nIndex);
// AfxMessageBox(strDirectory);


CString strFile;
strFile=strDirectory+"\\"+file;
CString str;
::GetPrivateProfileString(section,key,"",str.GetBuffer(1024),1024,strFile);
str.ReleaseBuffer();
// AfxMessageBox(str);
return  str;
}
CString GetSectionByName(CString file,CString section)
{
TCHAR szFileName[MAX_PATH];
CString strFileName;
CString strDirectory;
::GetModuleFileName(NULL,szFileName,MAX_PATH);
strFileName=szFileName;
int nIndex=strFileName.ReverseFind('\\');
strDirectory=strFileName.Left(nIndex);
// AfxMessageBox(strDirectory);


CString strFile;
strFile=strDirectory+"\\"+file;
CString str;
::GetPrivateProfileSection(section,str.GetBuffer(1024),1024,strFile);
str.ReleaseBuffer();
return  str;
}


int GetIntByName(CString file, CString section, CString key)
{
TCHAR szFileName[MAX_PATH];
CString strFileName;
CString strDirectory;
::GetModuleFileName(NULL,szFileName,MAX_PATH);
strFileName=szFileName;
int nIndex=strFileName.ReverseFind('\\');
strDirectory=strFileName.Left(nIndex);
// MessageBox(strDirectory);


CString strFile;
strFile=strDirectory+"\\"+file;


int value;
value=::GetPrivateProfileInt(section,key,-1,strFile);


// ::GetPrivateProfileString(section,key,"",str.GetBuffer(1024),1024,strFile);
// str.ReleaseBuffer();
// MessageBox(value);
CString tt;
tt.Format("%d",value);
// AfxMessageBox(tt);
return  value;
}


BOOL WriteStrByName(CString file, CString section, CString key, CString str)
{
TCHAR szFileName[MAX_PATH];
CString strFileName;
CString strDirectory;
::GetModuleFileName(NULL,szFileName,MAX_PATH);
strFileName=szFileName;
int nIndex=strFileName.ReverseFind('\\');
strDirectory=strFileName.Left(nIndex);
// MessageBox(strDirectory);


CString strFile;
strFile=strDirectory+"\\"+file;


return  WritePrivateProfileString(section,key,str,strFile);


}


BOOL WriteSectionByName(CString file,CString section,CString str)
{
TCHAR szFileName[MAX_PATH];
CString strFileName;
CString strDirectory;
::GetModuleFileName(NULL,szFileName,MAX_PATH);
strFileName=szFileName;
int nIndex=strFileName.ReverseFind('\\');
strDirectory=strFileName.Left(nIndex);
// MessageBox(strDirectory);


CString strFile;
strFile=strDirectory+"\\"+file;
return  WritePrivateProfileSection(section,str,strFile);


}


CString GetOsVersion()
{
CString osName;
OSVERSIONINFOEX osvi;
ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
GetVersionEx ((OSVERSIONINFO *)&osvi);


if(osvi.dwMajorVersion==5&&osvi.dwMinorVersion==0)
osName="Windows 2000";
if(osvi.dwMajorVersion==5&&osvi.dwMinorVersion==1)
osName="Windows XP";
if(osvi.dwMajorVersion==5&&osvi.dwMinorVersion==2)
osName="Windows Server 2003";
if(osvi.dwMajorVersion==6&&osvi.dwMinorVersion==0)
osName="Windows Vista";
if(osvi.dwMajorVersion==6&&osvi.dwMinorVersion==1)
osName="Windows 7";
return osName;
}


int GetIntByNameReg(CString default_dir, CString section, CString key)
{
default_dir+=section;
HKEY hKey;
RegOpenKey(HKEY_LOCAL_MACHINE,default_dir,&hKey);
/* RegOpenKeyEx(
HKEY_LOCAL_MACHINE,
default_dir,
0,
KEY_ALL_ACCESS,
&hKey
);*/
DWORD dwType;
DWORD dwValue=1000;//release 版要赋值,并且够大
CString strVersion;
RegQueryValueEx(hKey,key,0,&dwType,(LPBYTE)strVersion.GetBuffer(100),&dwValue);
strVersion.ReleaseBuffer();
RegCloseKey(hKey);


return atoi(strVersion);


}


CString GetStrByNameReg(CString default_dir, CString section, CString key)
{
default_dir+=section;
HKEY hKey;
RegOpenKey(HKEY_LOCAL_MACHINE,default_dir,&hKey);
DWORD dwType;
DWORD dwValue=10000;//release 版要赋值,并且够大
CString strVersion;
RegQueryValueEx(hKey,key,0,&dwType,(LPBYTE)strVersion.GetBuffer(1000),&dwValue);
strVersion.ReleaseBuffer();
// if(key=="CPLVer")
// AfxMessageBox("f");


return strVersion;
}


bool WriteStrByNameReg(CString default_dir, CString section, CString key, CString str)
{
default_dir+=section;
HKEY hKey;
RegOpenKey(HKEY_LOCAL_MACHINE,default_dir,&hKey);


//设置有关的数据
//CString_To_LPBYTE,请参考下面的函数
LPBYTE owner_Set=CString_To_LPBYTE(str);//定义用户姓名 owner_Set
DWORD type_1=REG_SZ;//定义数据类型
DWORD cbData_1=str.GetLength()+1;//定义数据长度


long ret1=::RegSetValueEx(hKey,key,NULL,type_1,owner_Set,cbData_1);
if(ret1!=ERROR_SUCCESS)
{
// AfxMessageBox("错误:无法设置有关的注册表信息");


return false;
}
return true;


}
LPBYTE CString_To_LPBYTE(CString str)
{
LPBYTE lpb=new BYTE[str.GetLength()+1];
for(int i=0;i<str.GetLength();i++)
lpb[i]=str[i];
lpb[str.GetLength()]=0;
return lpb;
}


void Syt_LockCtrlPos(CWnd* pm_parent,CWnd* pm_ctrl, int x, int y)
{
CRect rect_bk;
pm_parent->GetWindowRect(&rect_bk);


CRect rect_ctrl;
pm_ctrl->GetWindowRect(&rect_ctrl);


CRect rect_dst;


rect_dst.left=rect_bk.left+x;
rect_dst.top=rect_bk.top+y;
rect_dst.right=rect_dst.left+rect_ctrl.right-rect_ctrl.left;
rect_dst.bottom=rect_dst.top+rect_ctrl.bottom-rect_ctrl.top;


pm_ctrl->MoveWindow(rect_dst);


}


void CreateItemByNameReg(CString default_dir, CString section, CString key, CString strPos)
{
default_dir += section;


HKEY hKey;


RegCreateKey(HKEY_LOCAL_MACHINE,default_dir,&hKey);


RegSetValueEx(hKey,key,0,REG_SZ,CString_To_LPBYTE(strPos),strlen(strPos));


RegCloseKey(hKey);
}


void DelItemByNameReg(CString default_dir, CString section)
{
default_dir += section;


RegDeleteKey(HKEY_LOCAL_MACHINE ,default_dir);
}


void EnumItemByNameReg(CString default_dir, CComboBox &customName)
{
HKEY hKey;


RegOpenKey(HKEY_LOCAL_MACHINE,default_dir,&hKey);


CHAR achKey[MAX_PATH]; 


DWORD cbMaxSubKey = MAX_PATH;


DWORD i = 0;


for (int i = customName.GetCount() - 1; i >= 0; i--)
customName.DeleteString(i);


i = 0;


while(RegEnumKeyEx(hKey,i,achKey,&cbMaxSubKey,NULL,NULL,NULL,NULL)==ERROR_SUCCESS)
{
// AfxMessageBox(achKey); //显示子项名称


customName.AddString(achKey);           


ZeroMemory(achKey,MAX_PATH); 


cbMaxSubKey=MAX_PATH;


i++;
}


RegCloseKey(hKey);


customName.Invalidate();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值