写注册表,把协议和程序路径写入注册表
void CTestDlg::RegIEProtocol(CString strProName,CString strPath)
{
HKEY hSubKey;
DWORD lpdw;
RegCreateKeyEx(HKEY_CLASSES_ROOT,strProName.GetBuffer(0),0,NULL,REG_OPTION_NON_VOLATILE,KEY_WRITE,NULL,&hSubKey,&lpdw);//REG_OPTION_NON_VOLATILE:保存,重启还在;REG_OPTION_VOLATILE:不保存,重启不在。
CString strPname = strProName + "Protocol";
RegSetValueEx(hSubKey,"",0,REG_SZ,(LPBYTE)strPname.GetBuffer(0),strPname.GetLength());
RegSetValueEx(hSubKey,"URL Protocol",0,REG_SZ,(LPBYTE)strPath.GetBuffer(0),strPath.GetLength());
HKEY iconKey;
CString strIconStr = strPath + ",1";
RegCreateKeyEx(hSubKey,"DefaultIcon",0,NULL,REG_OPTION_NON_VOLATILE,KEY_WRITE,NULL,&iconKey,&lpdw);
RegSetValueEx(iconKey,"",0,REG_SZ,(LPBYTE)strIconStr.GetBuffer(0),strIconStr.GetLength());
RegCloseKey(iconKey);
HKEY shellKey;
RegCreateKeyEx(hSubKey,"shell",0,NULL,REG_OPTION_NON_VOLATILE,KEY_WRITE,NULL,&shellKey,&lpdw);
HKEY openKey;
RegCreateKeyEx(shellKey,"open",0,NULL,REG_OPTION_NON_VOLATILE,KEY_WRITE,NULL,&openKey,&lpdw);
HKEY commandKey;
RegCreateKeyEx(openKey,"command",0,NULL,REG_OPTION_NON_VOLATILE,KEY_WRITE,NULL,&commandKey,&lpdw);
CString str = "\"" + strPath + "\" \"%1\"";
RegSetValueEx(commandKey,"",0,REG_SZ,(LPBYTE)str.GetBuffer(0),str.GetLength());
RegCloseKey(hSubKey);
}
调用:
RegIEProtocol("test","D:\\work\\temp\\activctest\\ocxApp\\TSTCON32.EXE");
IE地址栏中输入:test://
即可启动TSTCON32.EXE