vc++中访问注册表 vc++中为什末没有一个注册表的类呢,像delphi中的tregistry那访问注册表不用shell提供的的方法,还有什末函数呢? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 VC++:访问和修改系统注册表 Windows95/98的注册表包含了Windows95/98的系统配置、PC机的硬件配置、Win32应用程序和用户的其他设置信息。注册表和INI文件不同,它是多层次的树状数据结构,具有六个分支(根键),每个分支又由许多的键和键值组成,而每个键则代表一个特定的配置项目。 在实际的编程工作中,我们遇到了如何在Visual C++中对Windows95/98注册表整个树状结构信息进行访问和修改的问题,如查询和修改注册表中用户姓名和公司名称的有关信息。通过编程实践,我们实现了在Visual C++中查询和修改系统注册表的有关信息。下面以一个实例说明具体的编程方法。 在Visual C++ 6.0或5.0环境中新建一基于对话框的工程,设置了两个命令按钮,名为“查询用户信息”和“修改用户信息”,用来查询和修改注册表中用户姓名和公司名称。这里须要指出的是,用户的信息位于系统注册表中 HKEY_LOCAL_MACHINE Software Microsoft Windows CurrentVersion 的位置,键值名RegisteredOwner和RegisteredOrganization分别表示用户的姓名和用户公司的名称。 1.查询用户信息的代码 HKEY hKEY; //定义有关的 hKEY, 在查询结束时要关闭。 LPCTSTR data_Set=″Software\Microsoft\Windows\CurrentVersion\″; //打开与路径 data_Set 相关的 hKEY,第一个参数为根键名称,第二个参数表。 //表示要访问的键的位置,第三个参数必须为0,KEY_READ表示以查询的方式。 //访问注册表,hKEY则保存此函数所打开的键的句柄。 long ret0=(::RegOpenKeyEx(HKEY_LOCAL_MACHINE,data_Set, 0, KEY_READ, &hKEY)); if(ret0!=ERROR_SUCCESS) //如果无法打开hKEY,则终止程序的执行 {MessageBox(″错误: 无法打开有关的hKEY!″); return;} //查询有关的数据 (用户姓名 owner_Get)。 LPBYTE owner_Get=new BYTE[80]; DWORD type_1=REG_SZ ; DWORD cbData_1=80; //hKEY为刚才RegOpenKeyEx()函数所打开的键的句柄,″RegisteredOwner″。 //表示要查 询的键值名,type_1表示查询数据的类型,owner_Get保存所。 //查询的数据,cbData_1表示预设置的数据长度。 long ret1=::RegQueryValueEx(hKEY, ″RegisteredOwner″, NULL, &type_1, owner_Get, &cbData_1); if(ret1!=ERROR_SUCCESS) { MessageBox(″错误: 无法查询有关注册表信息!″); return; } // 查询有关的数据 (公司名 company_Get) LPBYTE company_Get=new BYTE [80]; DWORD type_2=REG_SZ; DWORD cbData_2=80; long ret2=::RegQueryValueEx(hKEY, ″RegisteredOrganization″, NULL,&type_2,company_Get, &cbData_2); if(ret2!=ERROR_SUCCESS) { MessageBox(″错误: 无法查询有关注册表信息!″); return; } // 将 owner_Get 和 company_Get 转换为 CString 字符串, 以便显示输出。 CString str_owner=CString(owner_Get); CString str_company=CString(company_Get); delete[] owner_Get; delete[] company_Get; // 程序结束前要关闭已经打开的 hKEY。 ::RegCloseKey(hKEY); …… 这样,上述程序执行完毕,字符串str_owner和str_company则表示查询到的用户的姓名和公司的名称,在VC++中便可用对话框的方式将其显示出来。 2. 修改用户信息的代码(注意和上述的查询代码属于不同的函数体) 在程序中我们先显示一个对话框,要求用户输入新的用户姓名和公司名称并按确认键,将取得CString类型的有关字符串。要先将其转换为LPBYTE(即unsigned char*)型的数据类型,以便后面的函数调用。下面是程序中用到的将CString型转换为LPBYTE的转换函数: LPBYTE CString_To_LPBYTE(CString str) { LPBYTE lpb=new BYTE[str.GetLength()+1]; <str.GetLength(); i++)lpb[i]=str[i];> for(int i=0; ibr> lpb[str.GetLength()]=0; return lpb; } 以下则是具体的修改注册表用户信息的代码: CString str_owner, str_company; …… //通过对话框输入新的用户信息,保存到str_owner和str_company //定义有关的 hKEY, 在程序的最后要关闭。 HKEY hKEY; LPCTSTR data_Set=″Software\Microsoft\Windows\CurrentVersion″; //打开与路径 data_Set 相关的hKEY,KEY_WRITE表示以写的方式打开。 long ret0=(::RegOpenKeyEx(HKEY_LOCAL_MACHINE, data_Set, 0, KEY_WRITE, &hKEY)); if(ret0!=ERROR_SUCCESS) { MessageBox(″错误: 无法打开有关的hKEY!″); return; } //修改有关数据(用户姓名 owner_Set),要先将CString型转换为LPBYTE。 LPBYTE owner_Set=CString_To_LPBYTE(str_owner); DWORD type_1=REG_SZ; DWORD cbData_1=str_owner.GetLength()+1; //与RegQureyValueEx()类似,hKEY表示已打开的键的句柄,″RegisteredOwner″ //表示要访问的键值名,owner_Set表示新的键值,type_1和cbData_1表示新值。 //的数据类型和数据长度 long ret1=::RegSetValueEx(hKEY, ″RegisteredOwner″, NULL, type_1, owner_Set, cbData_1); if(ret1!=ERROR_SUCCESS) { MessageBox(″错误: 无法修改有关注册表信息!″); return; } //修改有关的数据 (公司名 company_Set) LPBYTE company_Set=CString_To_LPBYTE(str_company); DWORD type_2=REG_SZ; DWORD cbData_2=str_company.GetLength()+1; long ret2=::RegSetValueEx(hKEY, ″RegisteredOrganization″, NULL, type_2, company_Set, cbData_2); if(ret2!=ERROR_SUCCESS) { MessageBox(″错误: 无法修改有关注册表信息!″); return; } 执行上面的修改注册表的操作后,可打开注册表查看具体的数值,可以看到已经成功地修改了有关的数据了。 以上实例讲述了如何在VC++中访问Windows98/95的系统注册表,我们可以很方便地查询及修改注册表的任何位置的有关信息。以上的程序在Visual C++ 6.0中调试通过(Visual C++ 5.0与之类似),且运行结果正确。 以下是我仿照VCL写的一个能对注册表数据进行基本操作的类,希望对您有用。class CRegistry{public: CRegistry(void); ~CRegistry(void); bool OpenKey(HKEY hRootKey, LPCTSTR lpSubKey, bool bCreate); LPBYTE ReadData(LPCTSTR lpValueName); LPTSTR ReadString(LPCTSTR lpValueName); DWORD ReadDWORD(LPCTSTR lpValueName, DWORD dwDefault); bool WriteData(LPCTSTR lpValueName, const BYTE *lpData, DWORD cbData, DWORD dwType); bool WriteString(LPCTSTR lpValueName, LPCTSTR lpString); bool WriteDWORD(LPCTSTR lpValueName, DWORD dwData); bool DeleteKey(LPCTSTR lpSubKey); bool DeleteValue(LPCTSTR lpValueName);private: HKEY m_hRootKey; HKEY m_hCurrentKey; LPBYTE m_lpData;};CRegistry::CRegistry(void){ m_lpData = NULL; m_hRootKey = HKEY_LOCAL_MACHINE; m_hCurrentKey = NULL;}CRegistry::~CRegistry(void){ delete[] m_lpData; m_lpData = NULL;}bool CRegistry::OpenKey(HKEY hRootKey, LPCTSTR lpSubKey, bool bCreate){ bool bResult; m_hRootKey = hRootKey; if (bCreate) { DWORD dwResult; bResult = (RegCreateKeyEx(hRootKey, lpSubKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &m_hCurrentKey, &dwResult) == ERROR_SUCCESS); } else bResult = (RegOpenKeyEx(hRootKey, lpSubKey, 0, KEY_ALL_ACCESS, &m_hCurrentKey) == ERROR_SUCCESS); return bResult;}LPBYTE CRegistry::ReadData(LPCTSTR lpValueName){ DWORD dwRequired; DWORD dwType; RegQueryValueEx(m_hCurrentKey, lpValueName, 0, &dwType, NULL, &dwRequired); delete[] m_lpData; m_lpData = new BYTE[dwRequired]; RegQueryValueEx(m_hCurrentKey, lpValueName, 0, &dwType, m_lpData, &dwRequired); return m_lpData;}LPTSTR CRegistry::ReadString(LPCTSTR lpValueName){ return (LPTSTR)ReadData(lpValueName);}DWORD CRegistry::ReadDWORD(LPCTSTR lpValueName, DWORD dwDefault){ LPBYTE lpResult = ReadData(lpValueName); if (NULL == lpResult) return dwDefault; else return (DWORD)*lpResult;}bool CRegistry::WriteData(LPCTSTR lpValueName, const BYTE *lpData, DWORD cbData, DWORD dwType){ return (RegSetValueEx(m_hCurrentKey, lpValueName, 0, dwType, lpData, cbData) == ERROR_SUCCESS);}bool CRegistry::WriteString(LPCTSTR lpValueName, LPCTSTR lpString){ return (WriteData(lpValueName, (const BYTE*)lpString, lstrlen(lpString), REG_SZ));}bool CRegistry::WriteDWORD(LPCTSTR lpValueName, DWORD dwData){ return (WriteData(lpValueName, (const BYTE*)&dwData, sizeof(DWORD), REG_DWORD));}bool CRegistry::DeleteKey(LPCTSTR lpSubKey){ return (RegDeleteKey(m_hCurrentKey, lpSubKey) == ERROR_SUCCESS);}bool CRegistry::DeleteValue(LPCTSTR lpValueName){ return (RegDeleteValue(m_hCurrentKey, lpValueName) == ERROR_SUCCESS);} 1。MFC方法CWinApp::GetProfileString()CWinApp::WriteProfileString()其它数据类型类似。2。SDK方法RegCreateKeyEx()RegOpenKeyEx()RegQueryValueEx()等等。 这是俺写的一个注册表类。你研究一下就明白怎么操作这东东了。/////////////////////////////////////////////////////////////////////////// 注册表[本地] API 族系封装///////////////////////////////////////////////////////////////////////////*// 在CTRegister类中,注册表被视为一种文件,在应用程序代码中应用程序// 员可以象对待文件一样引用它。//// 对于一个文件的基本操作应该有:// 1) 创建文件对象// 2) 打开/创立文件 [其中决定以何种方式打开/创建一个文件]// 3)读/写文件 [其中只考虑读/写的内容]// 4)关闭文件 //*// TRegister.h typedef struct TSUT_REGISTERDATA // Register data{ HKEY m_hRoot; // 根键 // 根键类型 enum KeyRootType { HK_ROOT = (int) HKEY_CLASSES_ROOT, HK_CONFIG = (int) HKEY_CURRENT_CONFIG, HK_USER = (int) HKEY_CURRENT_USER, HK_MACHINE= (int) HKEY_LOCAL_MACHINE, HK_USERS = (int) HKEY_USERS };}REGISTERDATA, * PREGISTERDATA;class CTRegister : public TSUT_REGISTERDATA{public: CTRegister(HKEY hFile); CTRegister(UINT nRoot = HK_USER); CTRegister(LPCTSTR lpszFileName, UINT nOpenFlags); virtual ~CTRegister();protected: HKEY m_hFile; // 句柄 TDWORD m_dwFlags; // 类型 CString m_strOpenPath; // 路径(含文件名) DWORD m_filePointer; // 文件指针(将来用结构代替)protected: void CleanUp(); void Initialize(); virtual TINLINE DWORD GetFormat()const; virtual TINLINE BOOL IsReadOnly()const; virtual TINLINE BOOL IsCreateMode()const;public:// 操作 virtual void Close(); virtual TBOOL Open(LPCTSTR lpszFilePath, UINT nFlags); virtual TDWORD Read(void ** ppBuf, DWORD * pdwType); virtual TBOOL Write(const void * lpBuf, UINT nCount); operator HKEY(){ return m_hFile;}// 获取 virtual TINLINE CString GetFilePath()const; virtual TINLINE CString GetFileName()const; virtual TINLINE DWORD GetLength()const; virtual TINLINE DWORD GetPosition()const;// 修改 virtual void RemoveFile(LPCTSTR lpszFileName); virtual void RemoveFolder(LPCTSTR lpszDirName);// 文件指针public: virtual TINLINE LONG Seek(LONG lOff, UINT nFrom); virtual TINLINE LONG SeekToEnd(); virtual TINLINE VOID SeekToBegin();// 枚举量public: TBOOL WriteDefaultKey(const void * lpBuf, UINT nCount); enum typeOpenFlags // Open Flags { modeRead = (int) 0x00000, // mode read-only modeWrite = (int) 0x00001, // mode write-only modeReadWrite = (int) 0x00002, // mode read & write typeNone = (int) 0x01000, // REG_NONE, typeBinary = (int) 0x02000, // REG_BINARY, typeString = (int) 0x03000, // REG_SZ, typeDWord = (int) 0x04000, // REG_DWORD, eventCreate = (int) 0x20000, // create file eventOpen = (int) 0x40000, // open file }; enum filePoint // 文件指针 { begin = (int) 0x0, current = (int) 0x1, end = (int) 0x2 };}; CTRegister::CTRegister(UINT nRoot/*=HK_USER*/){ Initialize(); ASSERT(nRoot >= 0); m_hRoot = reinterpret_cast<HKEY>(nRoot);}CTRegister::CTRegister(HKEY hFile){ Initialize(); m_hFile = hFile;}// 构造的同时打开/创建一个文件CTRegister::CTRegister(LPCTSTR lpszFileName, UINT nFlags){ Initialize(); Open(lpszFileName, nFlags);}CTRegister::~CTRegister(){ CleanUp();}// 公共初始化过程void CTRegister::Initialize(){ m_hFile = NULL; m_strOpenPath = _T(""); m_dwFlags = 0x0; m_filePointer = -1; m_hRoot = reinterpret_cast<HKEY>(HK_USER);}void CTRegister::CleanUp(){ if(m_hFile != NULL) Close(); Initialize();}// 打开/创建一个注册表文件// lpszFileName为路径 + 文件名, nFlags为类型标识TBOOL CTRegister::Open(LPCTSTR lpszFilePath, UINT nFlags){ ASSERT(m_hFile == NULL); // 实例已打开/存在 ASSERT(lpszFilePath != NULL); m_dwFlags = (DWORD)nFlags; m_strOpenPath = lpszFilePath; // 打开/创建键 CTCatchAPI <LONG>err; IsCreateMode() ? err.Try(::RegCreateKey(m_hRoot, GetFilePath(), &m_hFile)) : err.Try(::RegOpenKey(m_hRoot, GetFilePath(), &m_hFile)); if(err.GetTry() != ERROR_SUCCESS) { TRACE("打开/创建注册表项目:\t\"" + m_strOpenPath + "\"\t失败.\n"); err.Catch(); return FALSE; } return (m_hFile != NULL);}// 关闭文件void CTRegister::Close(){ ASSERT(m_hFile != NULL); CTCatchAPI <LONG>err; err.Try(::RegCloseKey(m_hFile)); if(err.GetTry() != ERROR_SUCCESS) { err.Catch(); } m_hFile = NULL;}// 写入注册表文件,lpBuf为数据指针,nCount写入的大小 bitTBOOL CTRegister::Write(const void *lpBuf, UINT nCount){ ASSERT(m_hFile != NULL); ASSERT(lpBuf != NULL); ASSERT(nCount >= 0); CTCatchAPI <LONG>err; if(IsReadOnly()) { ASSERT(FALSE); // read only err.Throw("注册表文件" + GetFileName() + "只读。"); return FALSE; } // load BYTE * pCatBuf = NULL; // 剪载的数据 BYTE * pOldBuf = NULL; // 旧数据 BYTE * pNewBuf = (BYTE*)lpBuf; // 新数据 DWORD dwReLength = 0; DWORD dwOldType = 0; DWORD dwOldLength = GetLength(); DWORD dwNewLength = (DWORD)max(0, nCount); const DWORD dwCurPointer = GetPosition(); Read((void **)&pOldBuf, &dwOldType); if(dwCurPointer == -1) // 针指未曾移动, remove old data { pCatBuf = pNewBuf; dwReLength = dwNewLength; } else if(dwCurPointer == 0)// add to head { dwReLength = dwNewLength + dwOldLength; if(!(pCatBuf = new BYTE[dwReLength])) return FALSE; memset(pCatBuf, 0, dwReLength); memcpy(pCatBuf, pNewBuf, dwNewLength); memcpy(pCatBuf + dwNewLength, pOldBuf, dwOldLength); } else if(dwCurPointer == dwOldLength)// add to tail { dwReLength = dwNewLength + dwOldLength; if(!(pCatBuf = new BYTE[dwReLength])) return FALSE; memset(pCatBuf, 0, dwReLength); memcpy(pCatBuf, pOldBuf, dwOldLength); memcpy(pCatBuf + dwOldLength, pNewBuf, dwNewLength); } else // 在数据区中某个位置,add to current pos { dwReLength = dwCurPointer + dwNewLength; if(!(pCatBuf = new BYTE[dwReLength])) return FALSE; memset(pCatBuf, 0, dwReLength); memcpy(pCatBuf, pOldBuf, dwCurPointer); memcpy(pCatBuf + dwCurPointer, pNewBuf, dwNewLength); } // set err.Try(::RegSetValueEx(m_hFile, GetFileName(), 0, GetFormat(), (BYTE *)pCatBuf, (DWORD)dwReLength) ); if(err.GetTry() != ERROR_SUCCESS) { err.Catch(); if(pCatBuf && pCatBuf != pNewBuf) delete[] pCatBuf; if(pOldBuf) delete []pOldBuf; return FALSE; } if(pCatBuf && pCatBuf != pNewBuf) delete[] pCatBuf; if(pOldBuf) delete []pOldBuf; return TRUE;}TBOOL CTRegister::WriteDefaultKey(const void *lpBuf, UINT nCount){ ASSERT(m_hFile != NULL); ASSERT(lpBuf != NULL); ASSERT(nCount >= 0); CTCatchAPI <LONG>err; if(IsReadOnly()) { ASSERT(FALSE); // read only err.Throw("注册表文件" + GetFileName() + "只读。"); return FALSE; } DWORD dwOldType = 0; DWORD dwLength = max(0, nCount); BYTE * pOldBuf = NULL; // 旧数据 BYTE * pNewBuf = (BYTE *)lpBuf; Read((void **)&pOldBuf, &dwOldType); // set err.Try(::RegSetValueEx(m_hFile, NULL, 0, GetFormat(), (BYTE *)pNewBuf, (DWORD)dwLength) ); if(err.GetTry() != ERROR_SUCCESS) { err.Catch(); return FALSE; } return TRUE;} // 读入注册表文件// ppBuf 返回数据据区指针, pdwType返回数据的类型// 函数返回读出的字节数TDWORD CTRegister::Read(void ** ppBuf, DWORD * pdwType){ ASSERT(m_hFile != NULL); CTCatchAPI <LONG>err; * ppBuf = NULL; * pdwType = 0; DWORD dwPointerPos = max(GetPosition(), 0); DWORD dwFileLength = GetLength(); if(dwPointerPos > dwFileLength) { TRACE("注册表文件指针异常。\n"); return 0; } if(dwFileLength <= 0) { TRACE("注册表文件:\""+GetFileName()+"\"不正确的字节数。\n"); return 0; } BYTE * pNewBuf = (BYTE *)*ppBuf; BYTE * pOldBuf = NULL; DWORD dwReLength = max(dwFileLength - dwPointerPos, 0); if(dwReLength <= 0) { TRACE("注册表文件指针,偏移量不正常。\n"); return 0; } if(!(pOldBuf = new BYTE[dwFileLength]) || !(pNewBuf = new BYTE[dwReLength])) return 0; DWORD dwType = 0; memset(pOldBuf, 0, dwFileLength); memset(pNewBuf, 0, dwReLength); err.Try(RegQueryValueEx(m_hFile, (LPCTSTR)GetFileName(), NULL, &dwType, (BYTE *)pOldBuf, &dwFileLength)); VERIFY(dwType != 0x0); * pdwType = dwType; // 剪载 memcpy(pNewBuf, pOldBuf + dwPointerPos, dwFileLength); if(err.GetTry() != ERROR_SUCCESS) { err.Catch(); if(pOldBuf) delete[] pOldBuf; return 0; } if(pOldBuf) delete[] pOldBuf; return dwFileLength;}// 删除指定的注册表文件(值)void CTRegister::RemoveFile(LPCTSTR lpszFileName){ ASSERT(lpszFileName != NULL); ASSERT(m_hFile != NULL); // 必须先打开注册表文件 Open(...) CTCatchAPI <LONG>err; err.Try(::RegDeleteValue(m_hFile, lpszFileName)); if(err.GetTry() != ERROR_SUCCESS) { err.Catch(); } }// 删除文件夹(键) void CTRegister::RemoveFolder(LPCTSTR lpszDirName){ ASSERT(lpszDirName != NULL); ASSERT(m_hFile != NULL); CTCatchAPI <LONG>err; err.Try(::RegDeleteKey(m_hFile, lpszDirName)); if(err.GetTry() != ERROR_SUCCESS) { err.Catch(); }}// 返回文件名CString CTRegister::GetFileName() const{ ASSERT(m_strOpenPath.IsEmpty() == FALSE); CString strName; int nFind = m_strOpenPath.ReverseFind('\\'); nFind == -1 ? strName = m_strOpenPath : strName = m_strOpenPath.Mid(nFind + 1); return strName;}// 返回文件路径CString CTRegister::GetFilePath() const{ ASSERT(m_strOpenPath.IsEmpty() == FALSE); CString strPath; int nFind = m_strOpenPath.ReverseFind('\\'); nFind == -1 ? strPath = m_strOpenPath : strPath = m_strOpenPath.Mid(0, nFind); return strPath;}DWORD CTRegister::GetLength() const{ ASSERT(m_hFile != NULL); // 测试文件大小 DWORD dwSize; CTCatchAPI <LONG> err; err.Try(::RegQueryValueEx(m_hFile, (LPCTSTR)GetFileName(), NULL, NULL, NULL, &dwSize)); if(err.GetTry() != ERROR_SUCCESS) { err.Catch(); return 0; } return dwSize;}// seek to beginVOID CTRegister::SeekToBegin(){ ASSERT(m_hFile != NULL); Seek(0, CTRegister::begin);}// seek to endLONG CTRegister::SeekToEnd(){ ASSERT(m_hFile != NULL); return Seek(0, CTRegister::end);}// seekLONG CTRegister::Seek(LONG lOff, UINT nFrom){ ASSERT(lOff >= 0); ASSERT(m_hFile != NULL); DWORD dwLength = 0; if(m_filePointer == -1) m_filePointer = 0; if(dwLength = GetLength()) { switch(nFrom) { default: ASSERT(FALSE); break; case CTRegister::begin: // + offset return m_filePointer = min(lOff, (long)dwLength); case CTRegister::end: // -offset return m_filePointer = min(-lOff, -(long)dwLength); case CTRegister::current:// current pos + offset return m_filePointer += min(lOff, (long)dwLength - (long)m_filePointer); } } return 0;}// get pointer positionDWORD CTRegister::GetPosition() const{ ASSERT(m_hFile != NULL); return m_filePointer;}BOOL CTRegister::IsCreateMode() const{ ASSERT(m_dwFlags != 0); BOOL bCreate = FALSE; switch(m_dwFlags & 0x60000) { // eventCreate | eventOpen or NULL not supported default: ASSERT(FALSE);return FALSE; case eventCreate: bCreate = TRUE; break; case eventOpen: bCreate = FALSE; break; } return bCreate;}BOOL CTRegister::IsReadOnly() const{ ASSERT(m_hFile != NULL); ASSERT(m_dwFlags != 0); // read mode BOOL bReadOnly = FALSE; switch(m_dwFlags & 3) { // modeRead | modeWrite not supported default: ASSERT(FALSE);return FALSE; case modeRead: bReadOnly = TRUE; break; case modeWrite: case modeReadWrite: bReadOnly = FALSE; break; } return bReadOnly;}DWORD CTRegister::GetFormat() const{ ASSERT(m_hFile != NULL); ASSERT(m_dwFlags != 0); // file fromat DWORD dwFormat = 0; switch(m_dwFlags & 0x7000) { // 不支持的文件格式 default: ASSERT(FALSE); return FALSE; case typeNone: dwFormat = REG_NONE; break; case typeBinary: dwFormat = REG_BINARY; break; case typeString: dwFormat = REG_SZ; break; case typeDWord: dwFormat = REG_DWORD; break; } return dwFormat;} vc mdi 视图切换 RichEidt中删除大量OLE对象(QQ动态表情)问题(程序崩溃)哪个好心人见过? 在静态函数中不能访问非静态成员么? 控件的显示与隐藏 c++builder写客户端调用由Java写服务端的WebService问题? 发布到Tomcate中 HTTP表单提交时文本串的转换问题 谁写过WIA & STI ? 曾经玩过starcraft的的fan们或者现在还在玩的朋友们进来说一声! 如何使定时器处理函数成为一个自定义类的成员函数? mfc中滑块初始位置居中问题 在哪里可以找到WINDOWS系统下的锁屏源代码? 哪两个api函数可以读取和设置毫秒级的计算机系统时间?
Windows95/98的注册表包含了Windows95/98的系统配置、PC机的硬件配置
、Win32应用程序和用户的其他设置信息。注册表和INI文件不同,它是多层次的
树状数据结构,具有六个分支(根键),每个分支又由许多的键和键值组成,而
每个键则代表一个特定的配置项目。 在实际的编程工作中,我们遇到了如何在Visual C++中对Windows95/98注
册表整个树状结构信息进行访问和修改的问题,如查询和修改注册表中用户姓名
和公司名称的有关信息。通过编程实践,我们实现了在Visual C++中查询和修
改系统注册表的有关信息。下面以一个实例说明具体的编程方法。 在Visual C++ 6.0或5.0环境中新建一基于对话框的工程,设置了两个命令
按钮,名为“查询用户信息”和“修改用户信息”,用来查询和修改注册表中用
户姓名和公司名称。这里须要指出的是,用户的信息位于系统注册表中
HKEY_LOCAL_MACHINE Software Microsoft Windows CurrentVersion 的位置,键
值名RegisteredOwner和RegisteredOrganization分别表示用户的姓名和用户公司
的名称。 1.查询用户信息的代码
HKEY hKEY; //定义有关的 hKEY, 在查询结束时要关闭。
LPCTSTR data_Set=″Software\Microsoft\Windows\CurrentVersion\″;
//打开与路径 data_Set 相关的 hKEY,第一个参数为根键名称,第二个参数表。
//表示要访问的键的位置,第三个参数必须为0,KEY_READ表示以查询的方式。
//访问注册表,hKEY则保存此函数所打开的键的句柄。
long ret0=(::RegOpenKeyEx(HKEY_LOCAL_MACHINE,data_Set, 0, KEY_READ, &hKEY));
if(ret0!=ERROR_SUCCESS) //如果无法打开hKEY,则终止程序的执行
{MessageBox(″错误: 无法打开有关的hKEY!″);
return;}
//查询有关的数据 (用户姓名 owner_Get)。
LPBYTE owner_Get=new BYTE[80];
DWORD type_1=REG_SZ ; DWORD cbData_1=80;
//hKEY为刚才RegOpenKeyEx()函数所打开的键的句柄,″RegisteredOwner″。
//表示要查 询的键值名,type_1表示查询数据的类型,owner_Get保存所。
//查询的数据,cbData_1表示预设置的数据长度。
long ret1=::RegQueryValueEx(hKEY, ″RegisteredOwner″, NULL,
&type_1, owner_Get, &cbData_1);
if(ret1!=ERROR_SUCCESS)
{
MessageBox(″错误: 无法查询有关注册表信息!″);
return;
}
// 查询有关的数据 (公司名 company_Get)
LPBYTE company_Get=new BYTE [80];
DWORD type_2=REG_SZ; DWORD cbData_2=80;
long ret2=::RegQueryValueEx(hKEY, ″RegisteredOrganization″, NULL,&type_2,company_Get, &cbData_2);
if(ret2!=ERROR_SUCCESS)
{
MessageBox(″错误: 无法查询有关注册表信息!″);
return;
}
// 将 owner_Get 和 company_Get 转换为 CString 字符串, 以便显示输出。
CString str_owner=CString(owner_Get);
CString str_company=CString(company_Get);
delete[] owner_Get; delete[] company_Get;
// 程序结束前要关闭已经打开的 hKEY。
::RegCloseKey(hKEY);
…… 这样,上述程序执行完毕,字符串str_owner和str_company则表示查询到的
用户的姓名和公司的名称,在VC++中便可用对话框的方式将其显示出来。 2. 修改用户信息的代码(注意和上述的查询代码属于不同的函数体) 在程序中我们先显示一个对话框,要求用户输入新的用户姓名和公司名称并
按确认键,将取得CString类型的有关字符串。要先将其转换为LPBYTE(即
unsigned char*)型的数据类型,以便后面的函数调用。下面是程序中用到的将
CString型转换为LPBYTE的转换函数:
LPBYTE CString_To_LPBYTE(CString str)
{
LPBYTE lpb=new BYTE[str.GetLength()+1];
<str.GetLength(); i++)lpb[i]=str[i];>
for(int i=0; ibr> lpb[str.GetLength()]=0;
return lpb;
}
以下则是具体的修改注册表用户信息的代码:
CString str_owner, str_company;
…… //通过对话框输入新的用户信息,保存到str_owner和str_company
//定义有关的 hKEY, 在程序的最后要关闭。
HKEY hKEY;
LPCTSTR data_Set=″Software\Microsoft\Windows\CurrentVersion″;
//打开与路径 data_Set 相关的hKEY,KEY_WRITE表示以写的方式打开。
long ret0=(::RegOpenKeyEx(HKEY_LOCAL_MACHINE,
data_Set, 0, KEY_WRITE, &hKEY));
if(ret0!=ERROR_SUCCESS)
{
MessageBox(″错误: 无法打开有关的hKEY!″);
return;
}
//修改有关数据(用户姓名 owner_Set),要先将CString型转换为LPBYTE。
LPBYTE owner_Set=CString_To_LPBYTE(str_owner);
DWORD type_1=REG_SZ;
DWORD cbData_1=str_owner.GetLength()+1;
//与RegQureyValueEx()类似,hKEY表示已打开的键的句柄,″RegisteredOwner″
//表示要访问的键值名,owner_Set表示新的键值,type_1和cbData_1表示新值。
//的数据类型和数据长度
long ret1=::RegSetValueEx(hKEY, ″RegisteredOwner″, NULL,
type_1, owner_Set, cbData_1);
if(ret1!=ERROR_SUCCESS)
{
MessageBox(″错误: 无法修改有关注册表信息!″);
return;
}
//修改有关的数据 (公司名 company_Set)
LPBYTE company_Set=CString_To_LPBYTE(str_company);
DWORD type_2=REG_SZ;
DWORD cbData_2=str_company.GetLength()+1;
long ret2=::RegSetValueEx(hKEY, ″RegisteredOrganization″, NULL,
type_2, company_Set, cbData_2);
if(ret2!=ERROR_SUCCESS)
{
MessageBox(″错误: 无法修改有关注册表信息!″);
return;
} 执行上面的修改注册表的操作后,可打开注册表查看具体的数值,可以看到
已经成功地修改了有关的数据了。 以上实例讲述了如何在VC++中访问Windows98/95的系统注册表,我们可以
很方便地查询及修改注册表的任何位置的有关信息。以上的程序在Visual C++
6.0中调试通过(Visual C++ 5.0与之类似),且运行结果正确。
class CRegistry
{
public:
CRegistry(void);
~CRegistry(void);
bool OpenKey(HKEY hRootKey, LPCTSTR lpSubKey, bool bCreate);
LPBYTE ReadData(LPCTSTR lpValueName);
LPTSTR ReadString(LPCTSTR lpValueName);
DWORD ReadDWORD(LPCTSTR lpValueName, DWORD dwDefault);
bool WriteData(LPCTSTR lpValueName, const BYTE *lpData, DWORD cbData, DWORD dwType);
bool WriteString(LPCTSTR lpValueName, LPCTSTR lpString);
bool WriteDWORD(LPCTSTR lpValueName, DWORD dwData);
bool DeleteKey(LPCTSTR lpSubKey);
bool DeleteValue(LPCTSTR lpValueName);
private:
HKEY m_hRootKey;
HKEY m_hCurrentKey;
LPBYTE m_lpData;
};CRegistry::CRegistry(void)
{
m_lpData = NULL;
m_hRootKey = HKEY_LOCAL_MACHINE;
m_hCurrentKey = NULL;
}CRegistry::~CRegistry(void)
{
delete[] m_lpData;
m_lpData = NULL;
}bool CRegistry::OpenKey(HKEY hRootKey, LPCTSTR lpSubKey, bool bCreate)
{
bool bResult;
m_hRootKey = hRootKey;
if (bCreate)
{
DWORD dwResult;
bResult = (RegCreateKeyEx(hRootKey, lpSubKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &m_hCurrentKey, &dwResult) == ERROR_SUCCESS);
}
else
bResult = (RegOpenKeyEx(hRootKey, lpSubKey, 0, KEY_ALL_ACCESS, &m_hCurrentKey) == ERROR_SUCCESS);
return bResult;
}LPBYTE CRegistry::ReadData(LPCTSTR lpValueName)
{
DWORD dwRequired;
DWORD dwType;
RegQueryValueEx(m_hCurrentKey, lpValueName, 0, &dwType, NULL, &dwRequired);
delete[] m_lpData;
m_lpData = new BYTE[dwRequired];
RegQueryValueEx(m_hCurrentKey, lpValueName, 0, &dwType, m_lpData, &dwRequired);
return m_lpData;
}LPTSTR CRegistry::ReadString(LPCTSTR lpValueName)
{
return (LPTSTR)ReadData(lpValueName);
}DWORD CRegistry::ReadDWORD(LPCTSTR lpValueName, DWORD dwDefault)
{
LPBYTE lpResult = ReadData(lpValueName);
if (NULL == lpResult)
return dwDefault;
else
return (DWORD)*lpResult;
}bool CRegistry::WriteData(LPCTSTR lpValueName, const BYTE *lpData, DWORD cbData, DWORD dwType)
{
return (RegSetValueEx(m_hCurrentKey, lpValueName, 0, dwType, lpData, cbData) == ERROR_SUCCESS);
}bool CRegistry::WriteString(LPCTSTR lpValueName, LPCTSTR lpString)
{
return (WriteData(lpValueName, (const BYTE*)lpString, lstrlen(lpString), REG_SZ));
}bool CRegistry::WriteDWORD(LPCTSTR lpValueName, DWORD dwData)
{
return (WriteData(lpValueName, (const BYTE*)&dwData, sizeof(DWORD), REG_DWORD));
}bool CRegistry::DeleteKey(LPCTSTR lpSubKey)
{
return (RegDeleteKey(m_hCurrentKey, lpSubKey) == ERROR_SUCCESS);
}bool CRegistry::DeleteValue(LPCTSTR lpValueName)
{
return (RegDeleteValue(m_hCurrentKey, lpValueName) == ERROR_SUCCESS);
}
CWinApp::GetProfileString()
CWinApp::WriteProfileString()
其它数据类型类似。2。SDK方法
RegCreateKeyEx()
RegOpenKeyEx()
RegQueryValueEx()
等等。
//
// 注册表[本地] API 族系封装
//
///////////////////////////////////////////////////////////////////////
//*
// 在CTRegister类中,注册表被视为一种文件,在应用程序代码中应用程序
// 员可以象对待文件一样引用它。
//
// 对于一个文件的基本操作应该有:
// 1) 创建文件对象
// 2) 打开/创立文件 [其中决定以何种方式打开/创建一个文件]
// 3)读/写文件 [其中只考虑读/写的内容]
// 4)关闭文件
//*// TRegister.h
typedef struct TSUT_REGISTERDATA // Register data
{
HKEY m_hRoot; // 根键
// 根键类型
enum KeyRootType {
HK_ROOT = (int) HKEY_CLASSES_ROOT,
HK_CONFIG = (int) HKEY_CURRENT_CONFIG,
HK_USER = (int) HKEY_CURRENT_USER,
HK_MACHINE= (int) HKEY_LOCAL_MACHINE,
HK_USERS = (int) HKEY_USERS
};}REGISTERDATA, * PREGISTERDATA;
class CTRegister : public TSUT_REGISTERDATA
{
public:
CTRegister(HKEY hFile);
CTRegister(UINT nRoot = HK_USER);
CTRegister(LPCTSTR lpszFileName, UINT nOpenFlags);
virtual ~CTRegister();protected:
HKEY m_hFile; // 句柄
TDWORD m_dwFlags; // 类型
CString m_strOpenPath; // 路径(含文件名)
DWORD m_filePointer; // 文件指针(将来用结构代替)protected:
void CleanUp();
void Initialize();
virtual TINLINE DWORD GetFormat()const;
virtual TINLINE BOOL IsReadOnly()const;
virtual TINLINE BOOL IsCreateMode()const;public:
// 操作
virtual void Close();
virtual TBOOL Open(LPCTSTR lpszFilePath, UINT nFlags);
virtual TDWORD Read(void ** ppBuf, DWORD * pdwType);
virtual TBOOL Write(const void * lpBuf, UINT nCount);
operator HKEY(){ return m_hFile;}// 获取
virtual TINLINE CString GetFilePath()const;
virtual TINLINE CString GetFileName()const;
virtual TINLINE DWORD GetLength()const;
virtual TINLINE DWORD GetPosition()const;// 修改
virtual void RemoveFile(LPCTSTR lpszFileName);
virtual void RemoveFolder(LPCTSTR lpszDirName);// 文件指针
public:
virtual TINLINE LONG Seek(LONG lOff, UINT nFrom);
virtual TINLINE LONG SeekToEnd();
virtual TINLINE VOID SeekToBegin();// 枚举量
public:
TBOOL WriteDefaultKey(const void * lpBuf, UINT nCount);
enum typeOpenFlags // Open Flags
{
modeRead = (int) 0x00000, // mode read-only
modeWrite = (int) 0x00001, // mode write-only
modeReadWrite = (int) 0x00002, // mode read & write
typeNone = (int) 0x01000, // REG_NONE,
typeBinary = (int) 0x02000, // REG_BINARY,
typeString = (int) 0x03000, // REG_SZ,
typeDWord = (int) 0x04000, // REG_DWORD,
eventCreate = (int) 0x20000, // create file
eventOpen = (int) 0x40000, // open file
};
enum filePoint // 文件指针
{
begin = (int) 0x0,
current = (int) 0x1,
end = (int) 0x2
};};
CTRegister::CTRegister(UINT nRoot/*=HK_USER*/)
{
Initialize();
ASSERT(nRoot >= 0);
m_hRoot = reinterpret_cast<HKEY>(nRoot);
}CTRegister::CTRegister(HKEY hFile)
{
Initialize();
m_hFile = hFile;
}// 构造的同时打开/创建一个文件
CTRegister::CTRegister(LPCTSTR lpszFileName, UINT nFlags)
{
Initialize();
Open(lpszFileName, nFlags);
}CTRegister::~CTRegister()
{
CleanUp();
}// 公共初始化过程
void CTRegister::Initialize()
{
m_hFile = NULL;
m_strOpenPath = _T("");
m_dwFlags = 0x0;
m_filePointer = -1;
m_hRoot = reinterpret_cast<HKEY>(HK_USER);
}void CTRegister::CleanUp()
{
if(m_hFile != NULL) Close();
Initialize();
}// 打开/创建一个注册表文件
// lpszFileName为路径 + 文件名, nFlags为类型标识
TBOOL CTRegister::Open(LPCTSTR lpszFilePath, UINT nFlags)
{
ASSERT(m_hFile == NULL); // 实例已打开/存在
ASSERT(lpszFilePath != NULL); m_dwFlags = (DWORD)nFlags;
m_strOpenPath = lpszFilePath; // 打开/创建键
CTCatchAPI <LONG>err;
IsCreateMode()
? err.Try(::RegCreateKey(m_hRoot, GetFilePath(), &m_hFile))
: err.Try(::RegOpenKey(m_hRoot, GetFilePath(), &m_hFile));
if(err.GetTry() != ERROR_SUCCESS)
{
TRACE("打开/创建注册表项目:\t\"" + m_strOpenPath + "\"\t失败.\n");
err.Catch();
return FALSE;
}
return (m_hFile != NULL);
}// 关闭文件
void CTRegister::Close()
{
ASSERT(m_hFile != NULL); CTCatchAPI <LONG>err;
err.Try(::RegCloseKey(m_hFile));
if(err.GetTry() != ERROR_SUCCESS)
{
err.Catch();
}
m_hFile = NULL;
}// 写入注册表文件,lpBuf为数据指针,nCount写入的大小 bit
TBOOL CTRegister::Write(const void *lpBuf, UINT nCount)
{
ASSERT(m_hFile != NULL);
ASSERT(lpBuf != NULL);
ASSERT(nCount >= 0); CTCatchAPI <LONG>err;
if(IsReadOnly())
{
ASSERT(FALSE); // read only
err.Throw("注册表文件" + GetFileName() + "只读。");
return FALSE;
} // load
BYTE * pCatBuf = NULL; // 剪载的数据
BYTE * pOldBuf = NULL; // 旧数据
BYTE * pNewBuf = (BYTE*)lpBuf; // 新数据
DWORD dwReLength = 0;
DWORD dwOldType = 0;
DWORD dwOldLength = GetLength();
DWORD dwNewLength = (DWORD)max(0, nCount); const DWORD dwCurPointer = GetPosition();
Read((void **)&pOldBuf, &dwOldType);
if(dwCurPointer == -1) // 针指未曾移动, remove old data
{
pCatBuf = pNewBuf;
dwReLength = dwNewLength;
}
else if(dwCurPointer == 0)// add to head
{
dwReLength = dwNewLength + dwOldLength;
if(!(pCatBuf = new BYTE[dwReLength]))
return FALSE;
memset(pCatBuf, 0, dwReLength);
memcpy(pCatBuf, pNewBuf, dwNewLength);
memcpy(pCatBuf + dwNewLength, pOldBuf, dwOldLength);
}
else if(dwCurPointer == dwOldLength)// add to tail
{
dwReLength = dwNewLength + dwOldLength;
if(!(pCatBuf = new BYTE[dwReLength]))
return FALSE;
memset(pCatBuf, 0, dwReLength);
memcpy(pCatBuf, pOldBuf, dwOldLength);
memcpy(pCatBuf + dwOldLength, pNewBuf, dwNewLength);
}
else // 在数据区中某个位置,add to current pos
{
dwReLength = dwCurPointer + dwNewLength;
if(!(pCatBuf = new BYTE[dwReLength]))
return FALSE;
memset(pCatBuf, 0, dwReLength);
memcpy(pCatBuf, pOldBuf, dwCurPointer);
memcpy(pCatBuf + dwCurPointer, pNewBuf, dwNewLength);
}
// set
err.Try(::RegSetValueEx(m_hFile, GetFileName(), 0,
GetFormat(),
(BYTE *)pCatBuf,
(DWORD)dwReLength)
);
if(err.GetTry() != ERROR_SUCCESS)
{
err.Catch();
if(pCatBuf && pCatBuf != pNewBuf) delete[] pCatBuf;
if(pOldBuf) delete []pOldBuf;
return FALSE;
}
if(pCatBuf && pCatBuf != pNewBuf) delete[] pCatBuf;
if(pOldBuf) delete []pOldBuf;
return TRUE;
}TBOOL CTRegister::WriteDefaultKey(const void *lpBuf, UINT nCount)
{
ASSERT(m_hFile != NULL);
ASSERT(lpBuf != NULL);
ASSERT(nCount >= 0); CTCatchAPI <LONG>err;
if(IsReadOnly())
{
ASSERT(FALSE); // read only
err.Throw("注册表文件" + GetFileName() + "只读。");
return FALSE;
} DWORD dwOldType = 0;
DWORD dwLength = max(0, nCount);
BYTE * pOldBuf = NULL; // 旧数据
BYTE * pNewBuf = (BYTE *)lpBuf; Read((void **)&pOldBuf, &dwOldType); // set
err.Try(::RegSetValueEx(m_hFile, NULL, 0,
GetFormat(),
(BYTE *)pNewBuf,
(DWORD)dwLength)
);
if(err.GetTry() != ERROR_SUCCESS)
{
err.Catch();
return FALSE;
}
return TRUE;
}
// 读入注册表文件
// ppBuf 返回数据据区指针, pdwType返回数据的类型
// 函数返回读出的字节数TDWORD CTRegister::Read(void ** ppBuf, DWORD * pdwType)
{
ASSERT(m_hFile != NULL); CTCatchAPI <LONG>err;
* ppBuf = NULL;
* pdwType = 0; DWORD dwPointerPos = max(GetPosition(), 0);
DWORD dwFileLength = GetLength();
if(dwPointerPos > dwFileLength)
{
TRACE("注册表文件指针异常。\n");
return 0;
}
if(dwFileLength <= 0)
{
TRACE("注册表文件:\""+GetFileName()+"\"不正确的字节数。\n");
return 0;
}
BYTE * pNewBuf = (BYTE *)*ppBuf;
BYTE * pOldBuf = NULL;
DWORD dwReLength = max(dwFileLength - dwPointerPos, 0);
if(dwReLength <= 0)
{
TRACE("注册表文件指针,偏移量不正常。\n");
return 0;
} if(!(pOldBuf = new BYTE[dwFileLength]) ||
!(pNewBuf = new BYTE[dwReLength]))
return 0; DWORD dwType = 0;
memset(pOldBuf, 0, dwFileLength);
memset(pNewBuf, 0, dwReLength); err.Try(RegQueryValueEx(m_hFile, (LPCTSTR)GetFileName(),
NULL, &dwType, (BYTE *)pOldBuf, &dwFileLength)); VERIFY(dwType != 0x0);
* pdwType = dwType; // 剪载
memcpy(pNewBuf, pOldBuf + dwPointerPos, dwFileLength); if(err.GetTry() != ERROR_SUCCESS)
{
err.Catch();
if(pOldBuf) delete[] pOldBuf;
return 0;
}
if(pOldBuf) delete[] pOldBuf;
return dwFileLength;
}// 删除指定的注册表文件(值)
void CTRegister::RemoveFile(LPCTSTR lpszFileName)
{
ASSERT(lpszFileName != NULL);
ASSERT(m_hFile != NULL); // 必须先打开注册表文件 Open(...) CTCatchAPI <LONG>err;
err.Try(::RegDeleteValue(m_hFile, lpszFileName)); if(err.GetTry() != ERROR_SUCCESS)
{
err.Catch();
}
}// 删除文件夹(键)
void CTRegister::RemoveFolder(LPCTSTR lpszDirName)
{
ASSERT(lpszDirName != NULL);
ASSERT(m_hFile != NULL); CTCatchAPI <LONG>err;
err.Try(::RegDeleteKey(m_hFile, lpszDirName));
if(err.GetTry() != ERROR_SUCCESS)
{
err.Catch();
}
}// 返回文件名
CString CTRegister::GetFileName() const
{
ASSERT(m_strOpenPath.IsEmpty() == FALSE); CString strName;
int nFind = m_strOpenPath.ReverseFind('\\');
nFind == -1 ? strName = m_strOpenPath
: strName = m_strOpenPath.Mid(nFind + 1);
return strName;
}// 返回文件路径
CString CTRegister::GetFilePath() const
{
ASSERT(m_strOpenPath.IsEmpty() == FALSE); CString strPath;
int nFind = m_strOpenPath.ReverseFind('\\');
nFind == -1 ? strPath = m_strOpenPath
: strPath = m_strOpenPath.Mid(0, nFind);
return strPath;
}DWORD CTRegister::GetLength() const
{
ASSERT(m_hFile != NULL); // 测试文件大小
DWORD dwSize;
CTCatchAPI <LONG> err; err.Try(::RegQueryValueEx(m_hFile, (LPCTSTR)GetFileName(),
NULL, NULL, NULL, &dwSize)); if(err.GetTry() != ERROR_SUCCESS)
{
err.Catch();
return 0;
}
return dwSize;
}// seek to begin
VOID CTRegister::SeekToBegin()
{
ASSERT(m_hFile != NULL);
Seek(0, CTRegister::begin);
}// seek to end
LONG CTRegister::SeekToEnd()
{
ASSERT(m_hFile != NULL);
return Seek(0, CTRegister::end);
}// seek
LONG CTRegister::Seek(LONG lOff, UINT nFrom)
{
ASSERT(lOff >= 0);
ASSERT(m_hFile != NULL);
DWORD dwLength = 0;
if(m_filePointer == -1) m_filePointer = 0;
if(dwLength = GetLength())
{
switch(nFrom)
{
default:
ASSERT(FALSE);
break;
case CTRegister::begin: // + offset
return m_filePointer = min(lOff, (long)dwLength);
case CTRegister::end: // -offset
return m_filePointer = min(-lOff, -(long)dwLength);
case CTRegister::current:// current pos + offset
return m_filePointer += min(lOff, (long)dwLength - (long)m_filePointer);
}
}
return 0;
}// get pointer position
DWORD CTRegister::GetPosition() const
{
ASSERT(m_hFile != NULL);
return m_filePointer;
}BOOL CTRegister::IsCreateMode() const
{
ASSERT(m_dwFlags != 0);
BOOL bCreate = FALSE;
switch(m_dwFlags & 0x60000)
{
// eventCreate | eventOpen or NULL not supported
default: ASSERT(FALSE);return FALSE;
case eventCreate:
bCreate = TRUE;
break;
case eventOpen:
bCreate = FALSE;
break;
}
return bCreate;
}BOOL CTRegister::IsReadOnly() const
{
ASSERT(m_hFile != NULL);
ASSERT(m_dwFlags != 0); // read mode
BOOL bReadOnly = FALSE;
switch(m_dwFlags & 3)
{
// modeRead | modeWrite not supported
default: ASSERT(FALSE);return FALSE;
case modeRead:
bReadOnly = TRUE;
break;
case modeWrite:
case modeReadWrite:
bReadOnly = FALSE;
break;
}
return bReadOnly;
}DWORD CTRegister::GetFormat() const
{
ASSERT(m_hFile != NULL);
ASSERT(m_dwFlags != 0); // file fromat
DWORD dwFormat = 0;
switch(m_dwFlags & 0x7000)
{
// 不支持的文件格式
default:
ASSERT(FALSE); return FALSE;
case typeNone:
dwFormat = REG_NONE;
break;
case typeBinary:
dwFormat = REG_BINARY;
break;
case typeString:
dwFormat = REG_SZ;
break;
case typeDWord:
dwFormat = REG_DWORD;
break;
}
return dwFormat;
}