请教高手:求自启动VC程序注释:

发布时间:2024-05-06 17:22 发布:上海旅游网

问题描述:

帮看看这段操作注册表的程序:
String path=Application->ExeName;
connectedcenter=0;
printerstatus=0;
TRegistry *Registry=new TRegistry;
Registry->RootKey=HKEY_LOCAL_MACHINE;
Registry->OpenKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run",TRUE);
try
{
if(Registry->ReadString("Alarm Client")!=path)
Registry->WriteString("Alarm Client",path);
}
catch(...)
{
}
请高手写一下每句话的注释吧!(我想写一段程序自启动的代码,求助高人给的代码,可是我卡不懂)

问题解答:

这段代码就是在注册表里面添加信息,c++ builder语言
VC里面有专门的操作注册表的API函数
-------------------------------------------------
String path=Application->ExeName; //程序路径,如"c:\\apps\\alarmclient.exe"
connectedcenter=0;
printerstatus=0;
TRegistry *Registry=new TRegistry; //注册表项
Registry->RootKey=HKEY_LOCAL_MACHINE; //注册表根键
Registry->OpenKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run",TRUE); //打开准备添加
try
{
if(Registry->ReadString("Alarm Client")!=path) //判断是否已经存在,或者路径是否相同
Registry->WriteString("Alarm Client",path); //不存在或路经不同,写入新的
}
catch(...) //异常处理
{
}

----------------------------------------------------------------------------------------------------------------------
CString str = "hello reg";
DWORD regd = 100;
BYTE bValues[] = { 0x11, 0x12, 0x55 };

BYTE Vals[100];
DWORD lenIt = 100;
HKEY hk;

// 打开注册表如果没有则创建, 返回注册表句柄于hk
if ( ::RegCreateKey( HKEY_LOCAL_MACHINE, "SOFTWARE\\Test\\", &hk ) == ERROR_SUCCESS )
{
AfxMessageBox( "打开注册表" );
}

// 保存参数到注册表
try
{
/*
RegSetValueEx( 根键句柄, 值项名称, 保留参数补0即可, 数据类型, 所设置的数据, 数据长度 )
用来设置注册表键特定的值,如果数据不存在则创建它
*/
/* 保存字符值到注册表 */
if ( ::RegSetValueEx( hk, "Server", 0, REG_SZ, (LPBYTE)(LPCSTR)str, str.GetLength( ) +1 ) == ERROR_SUCCESS )
{
AfxMessageBox( "字符值参数保存成功" );
}

/* 保存双字节到注册表 */
if ( ::RegSetValueEx( hk, "part", 0, REG_DWORD, (BYTE*)®d, sizeof( regd ) ) == ERROR_SUCCESS )
{
AfxMessageBox( "双字节参数保存成功" );
}

/* 保存二进制到注册表 */
if ( ::RegSetValueEx( hk, "id", 0, REG_BINARY, bValues, 3) == ERROR_SUCCESS )
{
AfxMessageBox( "二进制参数保存成功" );
}

/*
RegQueryValueEx( 根键句柄, 值项名称, 保留参数补0即可, 返回值的数据类型(可为NULL), 返回
读取的数据, 返回读取数据的长度 )
用来返回注册表键的特定名称的值
/*
/* 获得字符值 */
if ( ::RegQueryValueEx( hk, "Server", 0, NULL, (BYTE*)Vals, &lenIt ) == ERROR_SUCCESS )
{
AfxMessageBox( (CString)Vals );
}

/* 获得双字节值 */
if ( ::RegQueryValueEx( hk, "part", 0, NULL, (BYTE*)Vals, &lenIt ) == ERROR_SUCCESS )
{
AfxMessageBox( (CString)Vals );
}

/* 获得二进制值 */
if ( ::RegQueryValueEx( hk, "id", 0, NULL, (BYTE*)Vals, &lenIt ) == ERROR_SUCCESS )
{
AfxMessageBox( (CString)Vals );
}

// 关闭注册表
::RegCloseKey( hk );

热点新闻