随笔-27  评论-6  文章-0  trackbacks-0
利用Win32API函数操作注册表:

MFC对话框工程:SoftReg,将应用软件的使用次数写入注册表。
在OnInitDialog函数中添加:
/************************************************************************/
    
/* RegOpenKey(HKEY hKey,LPCTSRT lpSubKey,PHKEY phkResult)
        打开注册表中指定的项或子项,hKey:根键,lpSubKey:将要打开的子项名称,
        phkResult:返回打开的子项。如果成功,返回ERROR_SUCCESS
    
*/

    
/************************************************************************/
    HKEY Key;    
    CString sKeyPath;
    sKeyPath 
= "Software\\jerry";
    
if (RegOpenKey(HKEY_CURRENT_USER,sKeyPath,&Key)!=0
        
&& RegOpenKey(HKEY_CURRENT_USER,sKeyPath,&Key)!=ERROR_SUCCESS)
    
{
        ::RegCreateKey(HKEY_CURRENT_USER,sKeyPath,
&Key);
        ::RegSetValueEx(Key,
"TryTime",0,REG_SZ,(unsigned char*)"5",2);
        ::RegCloseKey(Key);
        MessageBox(
"You can try this 5 times!","tip",MB_OK|MB_ICONEXCLAMATION);
    }
 
    
else
    
{
        CString sKeyTime;
        
int nTryTime;
        LPBYTE Data
=new BYTE[80];
        DWORD TYPE
=REG_SZ;
        DWORD cbData
=80;
        ::RegQueryValueEx(Key,
"TryTime",0,&TYPE,Data,&cbData);
        sKeyTime.Format(
"%s",Data);
        nTryTime
=atoi(sKeyTime);
        
if (nTryTime <= 1)
        
{
            MessageBox(
"Please regist!","tip",MB_OK|MB_ICONSTOP);
            
return FALSE;
        }

        nTryTime
--;
        sKeyTime.Format(
"%d",nTryTime);
        ::RegSetValueEx(Key,
"TryTime",0,REG_SZ,(unsigned char*)sKeyTime.GetBuffer(sKeyTime.GetLength()),2);
        ::RegCloseKey(Key);
        MessageBox(
"You can try this "+sKeyTime+" times!"," tip",MB_OK|MB_ICONEXCLAMATION);
    }
posted on 2010-06-01 17:03 CrazyNerd 阅读(392) 评论(0)  编辑 收藏 引用 所属分类: Windows编程

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理