正文

ReadRegistry与WriteRegistry读写注册表2010-05-24 16:41:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/miaowei/51191.html

分享到:

在安装一些软件的时候,我们会看到一些提示,如 “正在写入注册表”等。在win 32时代,系统启动或者应用程序启动时,会有一个.ini文件会事先被加载。然后系统或者应用程序才能正常运行。但是,现在,我们一般都只需要把启动信息写入注册表,这样就可以很方便的进行读写。 本文,我不打算介绍利用注册表读写一套函数来将如何读写注册表信息。而是想要讲讲ReadRegistry 和 WriteRegistry 这两个函数在VC/MFC编程中如何将一些系统配置信息写入注册表,并在需要的时候读出。 在MFC 程序中,生成Project   的时候会在   InitInstance   中有一句         SetRegistryKey(_T("Local   AppWizard-Generated   Applications"));        其中   "Local   AppWizard-Generated   Applications"   是注册表中相对于         HKEY_CURRENT_USE\Software    的路径,然后当你利用ReadRegistry 和 WriteRegistry 这两个函数对注册表读写都是在该路径下进行操作。比如我们可以把 一些系统全局配置信息写入注册表,然后在需要的时候读出,这样就避免每次都 要进行文件配置。 如下例: void CRealTimeDicDlg::ReadRegistry( void ){ DWORD nVersion; nVersion = theApp.GetProfileInt( _T(""), _T("Version"), 0 ); if ( GetVersionMJ(nVersion) < REGISTRY_VERSION_MJ )  return;  m_nRecogFPS = theApp.GetProfileInt( _T(""), _T("RecogFrameRate"), DEFAULT_RECOG_FPS ); m_nFifoDepth = theApp.GetProfileInt( _T(""), _T("FifoDepth"), RCG_DEFAULT_FIFO_DEPTH ); m_bIsLFReading = theApp.GetProfileInt( _T(""), _T("LFReadingFlag"), DEFAULT_LF_READING_FLAG ); m_nRecogMode = theApp.GetProfileInt( _T(""), _T("RecogMode"), DEFAULT_RECOG_MODE ); m_nGuideSize = theApp.GetProfileInt( _T(""), _T("GuideSize"), DEFAULT_GUIDE_SIZE ); m_bDispRcgRect = theApp.GetProfileInt( _T(""), _T("DispRecogRect"), DEFAULT_DISP_RECOG_RECT_FLAG ); m_nFormatIdx = theApp.GetProfileInt( _T(""), _T("FormatIdx"), CAMERA_FORMAT_NONE ); m_strDevice = theApp.GetProfileString( _T(""), _T("CaptureDevice"), DEFAULT_DEVICE_STRING ); m_nCaptureFPS = theApp.GetProfileInt( _T(""), _T("CaptureFrameRate"), DEFAULT_CAPTURE_FPS ); m_nFocus = theApp.GetProfileInt( _T(""), _T("Focus"), DEFAULT_FOCUS ); m_bAutoFocus = theApp.GetProfileInt( _T(""), _T("AutoFocus"), DEFAULT_AUTO_FOCUS ); m_nENG2JPN_WebDicIdx = theApp.GetProfileInt( _T(""), _T("ENG2JPN_WEBDIC"), DEFAULT_ENG2JPN_WEBDIC); m_nJPNLANG_WebDicIdx = theApp.GetProfileInt( _T(""), _T("JPNLANG_WEBDIC"), DEFAULT_JPNLANG_WEBDIC);} void CRealTimeDicDlg::WriteRegistry( void ){ theApp.WriteProfileInt( _T(""), _T("Version"), REGISTRY_VERSION ); theApp.WriteProfileInt( _T(""), _T("RecogFrameRate"), m_nRecogFPS ); theApp.WriteProfileInt( _T(""), _T("FifoDepth"), m_nFifoDepth ); theApp.WriteProfileInt( _T(""), _T("LFReadingFlag"), m_bIsLFReading ); theApp.WriteProfileInt( _T(""), _T("RecogMode"), m_nRecogMode ); theApp.WriteProfileInt( _T(""), _T("GuideSize"), m_nGuideSize ); theApp.WriteProfileInt( _T(""), _T("DispRecogRect"), m_bDispRcgRect ); theApp.WriteProfileInt( _T(""), _T("FormatIdx"), m_nFormatIdx ); theApp.WriteProfileString( _T(""), _T("CaptureDevice"), m_strDevice ); theApp.WriteProfileInt( _T(""), _T("CaptureFrameRate"), m_nCaptureFPS ); theApp.WriteProfileInt( _T(""), _T("Focus"), m_nFocus ); theApp.WriteProfileInt( _T(""), _T("AutoFocus"), m_bAutoFocus ); theApp.WriteProfileInt(_T(""), _T("ENG2JPN_WEBDIC"), m_nENG2JPN_WebDicIdx); theApp.WriteProfileInt(_T(""), _T("JPNLANG_WEBDIC"), m_nJPNLANG_WebDicIdx); }  此外,另外两个函数可以用来些 .ini文件。这样每次启动应用程序可以现读入 .ini启动文件。 一般是写到.ini文件里的   1.   读 .ini 文件:     ret   =   GetPrivateProfileString(SECTION_ZIXIANG1,   strEntry,   "",   temp,   1000,    iniFileName);         SECTION_ZIXIANG1     这个参数为SECTION你知道吧     strEntry                     这个参数为SECTION下里的东东     temp                             这个参数为临时存放读出来的东东     1000                             为长度     iniFileName               为文件名   2. 写 .ini 文件    ret  = SetPrivateProfileString(SECTION_ZIXIANG1, strEngry, "", temp, 1000, iniFileName);   SECTION_ZIXIANG1     这个参数为SECTION你知道吧     strEntry                     这个参数为SECTION下里的东东     temp                             这个参数为临时存放读出来的东东     1000                             为长度     iniFileName               为文件名    

阅读(4615) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册