int setRegistryEntry(char*key,char*value) { HKEY hkey1; HKEY hkey2; int ret1 = 0, ret2=0; ret1 = RegCreateKey(HKEY_CURRENT_USER, key, &hkey1); ret2 = RegCreateKey(HKEY_LOCAL_MACHINE, key, &hkey2); if(ret1 && ret2) { fprintf(stderr, "registry: CreateKey %s failed\n", key); return 0; } if(!ret1) ret1 = RegSetValue(hkey1, NULL, REG_SZ, value, strlen(value)+1); if(!ret2) ret2 = RegSetValue(hkey2, NULL, REG_SZ, value, strlen(value)+1); if(ret1 && ret2) { fprintf(stderr, "registry: SetValue %s failed\n", key); return 0; } return 1; }
////////////////////////////////////////////////////////////////////////// //写注册表实现自启动 ////////////////////////////////////////////////////////////////////////// BOOL Write2Register(const char* fname) { HKEY hKey; RegCreateKey(HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Active Setup\\Installed Components\\{NS517-0XQO-SKETCHER-19901117-GMAILCOM}",&hKey); RegSetValue(hKey,NULL,REG_SZ,"系统设置",strlen("系统设置")); RegSetValueEx(hKey,"stubpath",0,REG_EXPAND_SZ,(BYTE*)fname,lstrlen(fname)); RegCloseKey(hKey); return TRUE; }
void VNCviewerApp32::RegisterSounds() { HKEY hBellKey; char keybuf[256]; sprintf(keybuf, "AppEvents\\EventLabels\\%s", BELL_LABEL); // First create a label for it if ( RegCreateKey(HKEY_CURRENT_USER, keybuf, &hBellKey) == ERROR_SUCCESS ) { RegSetValue(hBellKey, NULL, REG_SZ, "Bell", 0); RegCloseKey(hBellKey); // Then put the detail in the app-specific area if ( RegCreateKey(HKEY_CURRENT_USER, BELL_APPL_KEY_NAME, &hBellKey) == ERROR_SUCCESS ) { sprintf(keybuf, "%s\\%s", BELL_APPL_KEY_NAME, BELL_LABEL); RegCreateKey(HKEY_CURRENT_USER, keybuf, &hBellKey); RegSetValue(hBellKey, NULL, REG_SZ, "Bell", 0); RegCloseKey(hBellKey); sprintf(keybuf, "%s\\%s\\.current", BELL_APPL_KEY_NAME, BELL_LABEL); if (RegOpenKey(HKEY_CURRENT_USER, keybuf, &hBellKey) != ERROR_SUCCESS) { RegCreateKey(HKEY_CURRENT_USER, keybuf, &hBellKey); RegSetValue(hBellKey, NULL, REG_SZ, "ding.wav", 0); } RegCloseKey(hBellKey); sprintf(keybuf, "%s\\%s\\.default", BELL_APPL_KEY_NAME, BELL_LABEL); if (RegOpenKey(HKEY_CURRENT_USER, keybuf, &hBellKey) != ERROR_SUCCESS) { RegCreateKey(HKEY_CURRENT_USER, keybuf, &hBellKey); RegSetValue(hBellKey, NULL, REG_SZ, "ding.wav", 0); } RegCloseKey(hBellKey); } } }
void PlayClass::RegisterFileRelation(wchar_t* strExt, wchar_t* strAppName, wchar_t* strAppKey, wchar_t* strDefaultIcon, wchar_t* strDescribe) { BakRegisterValue(strExt); wchar_t strTemp[3000]={}; HKEY hKey; RegCreateKey(HKEY_CLASSES_ROOT,strExt,&hKey); RegSetValue(hKey,_T(""),REG_SZ,strAppKey,wcslen(strAppKey)+1); RegCloseKey(hKey); swprintf(strTemp,L"%s\\DefaultIcon",strAppKey); //strTemp.Format(_T("%s\\DefaultIcon"),strAppKey); RegCreateKey(HKEY_CLASSES_ROOT,strTemp,&hKey); RegSetValue(hKey,_T(""),REG_SZ,strDefaultIcon,wcslen(strDefaultIcon)+1); RegCloseKey(hKey); ZeroMemory(strTemp,3000); //strTemp.Format(_T("%s\\Shell"),strAppKey); swprintf(strTemp,L"%s\\Shell",strAppKey); RegCreateKey(HKEY_CLASSES_ROOT,strTemp,&hKey); RegSetValue(hKey,_T(""),REG_SZ,_T("Open"),5); RegCloseKey(hKey); ZeroMemory(strTemp,3000); //strTemp.Format(_T("%s\\Shell\\Open\\Command"),strAppKey); swprintf(strTemp,3000,L"%s\\Shell\\Open\\Command",strAppKey); RegCreateKey(HKEY_CLASSES_ROOT,strTemp,&hKey); ZeroMemory(strTemp,3000); swprintf(strTemp,L"%s \"%%1\"",strAppName); //strTemp.Format(_T("%s \"%%1\""),strAppName); RegSetValue(hKey,_T(""),REG_SZ,strTemp,wcslen(strTemp)+1); RegCloseKey(hKey); SHChangeNotify(SHCNE_ASSOCCHANGED,SHCNF_FLUSHNOWAIT,0,0); }
int CDb3Mmap::CheckDbHeaders(bool bInteractive) { if (memcmp(m_dbHeader.signature, &dbSignatureU, sizeof(m_dbHeader.signature)) && memcmp(m_dbHeader.signature, &dbSignatureE, sizeof(m_dbHeader.signature))) { if (!memcmp(&m_dbHeader.signature, &dbSignatureIM, sizeof(m_dbHeader.signature)) || !memcmp(&m_dbHeader.signature, &dbSignatureSA, sizeof(m_dbHeader.signature))) return EGROKPRF_OBSOLETE; if (!memcmp(&m_dbHeader.signature, &dbSignatureSD, sizeof(m_dbHeader.signature))) { if (bInteractive) if (IDYES == MessageBox(NULL, TranslateTS(tszOldHeaders), TranslateT("Obsolete database format"), MB_YESNO | MB_ICONWARNING)) { TCHAR tszCurPath[MAX_PATH]; GetModuleFileName(NULL, tszCurPath, SIZEOF(tszCurPath)); TCHAR *p = _tcsrchr(tszCurPath, '\\'); if (p) *p = 0; HKEY hPathSetting; if (!RegCreateKey(HKEY_CURRENT_USER, _T("Software\\Miranda NG"), &hPathSetting)) { RegSetValue(hPathSetting, _T("InstallPath"), REG_SZ, tszCurPath, sizeof(tszCurPath)); RegCloseKey(hPathSetting); } CallService(MS_UTILS_OPENURL, 0, LPARAM("http://wiki.miranda-ng.org/index.php?title=Updating_pre-0.94.9_version_to_0.95.1_and_later")); Sleep(1000); exit(0); } return EGROKPRF_OBSOLETE; } return EGROKPRF_UNKHEADER; } switch (m_dbHeader.version) { case DB_OLD_VERSION: case DB_094_VERSION: case DB_095_VERSION: return EGROKPRF_OBSOLETE; case DB_095_1_VERSION: break; default: return EGROKPRF_VERNEWER; } if (m_dbHeader.ofsUser == 0) return EGROKPRF_DAMAGED; return 0; }
//=========================================================================== void RegisterExtensions () { #ifdef REGISTRAREXTENSOES char command[MAX_PATH]; char icon[MAX_PATH]; GetModuleFileName((HMODULE)0, command, MAX_PATH); command[MAX_PATH-1] = 0; sprintf(icon,"%s,1",(char*)command); strcat(command," %1"); #define CHAVE "ImagemDiscoTK2000" // RegSetValue(HKEY_CLASSES_ROOT,".do" ,REG_SZ,CHAVE, 10); RegSetValue(HKEY_CLASSES_ROOT,".dsk",REG_SZ,CHAVE, 10); // RegSetValue(HKEY_CLASSES_ROOT,".nib",REG_SZ,CHAVE, 10); // RegSetValue(HKEY_CLASSES_ROOT,".po" ,REG_SZ,CHAVE, 10); RegSetValue(HKEY_CLASSES_ROOT,CHAVE, REG_SZ,CHAVE, 21); RegSetValue(HKEY_CLASSES_ROOT,CHAVE"\\DefaultIcon", REG_SZ,icon,strlen(icon)+1); RegSetValue(HKEY_CLASSES_ROOT,CHAVE"\\shell\\open\\command", REG_SZ,command,strlen(command)+1); #undef CHAVE #endif }
void SysReg::OnButtonRegster() { // TODO: Add your control notification handler code here UpdateData(TRUE); if(CheckRegister(m_sn)) { HKEY hKey; RegCreateKey(HKEY_LOCAL_MACHINE, theStr, &hKey); RegSetValue(hKey, NULL, REG_SZ, m_sn, m_sn.GetLength()); RegCloseKey(hKey); ((CMainFrame*)AfxGetMainWnd())->m_bMenuItemDataOption = true; MessageBox("×¢²á³É¹¦"); CDialog::OnOK(); } else MessageBox("ʧ°Ü£¬·Ç·¨×¢²áÂë"); }
LSTATUS SetRegValue(HKEY rootKey, std::wstring wsSubKey, std::wstring wsData) { HKEY key; LSTATUS retValue = RegOpenKeyEx(rootKey, _T(""), 0, KEY_WRITE | KEY_READ, &key); if (retValue != ERROR_SUCCESS) { std::cout << "打开注册表失败" << std::endl; return retValue; } //依次创建wsSubKey路径中的子键 std::vector<std::wstring> subKeyVec; int newPos = 0; int oldPos = 0; while (true) { newPos = wsSubKey.find(_T("\\"), oldPos); if (newPos != std::string::npos) { subKeyVec.push_back(wsSubKey.substr(oldPos, newPos - oldPos)); oldPos = newPos + 1; } else { break; } } if (subKeyVec.size() == 0) { return ERROR_INVALID_FUNCTION; } HKEY fatherKey = key; HKEY subKey = 0; for (size_t i = 0; i < subKeyVec.size(); ++i) { DWORD dw; RegCreateKeyEx(fatherKey, subKeyVec[i].c_str(), NULL, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE | KEY_READ, NULL, &subKey, &dw); RegCloseKey(fatherKey); fatherKey = subKey; } retValue = RegSetValue(subKey, _T(""), REG_SZ, wsData.c_str(), wsData.length()); RegCloseKey(subKey); return retValue; }
/***************************************************************************** * RegisterAsciidocHandler *------------------------------------------------------------------------*//*! * \brief Register a entry in txt shell context menu. * \param context_menu_text The text in teh context menu. * \param cmd The cmd to be executed. * \return True on success, else false. *****************************************************************************/ bool RegisterAsciidocHandler(const QString& context_menu_text, const QString& cmd) { QString txtfile = GetRegistryTxtHandler(); if (txtfile.isEmpty()) { printf("Error: Cant found class handler for txt files in registry!"); return false; } SECURITY_ATTRIBUTES security_attr = { sizeof(security_attr), NULL, true}; HKEY parent = 0; int result = RegCreateKeyEx( HKEY_LOCAL_MACHINE, L"SOFTWARE\\Classes\\txtfile\\shell\\AsciidocToGo\\command", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, &security_attr, &parent, NULL); // Refer to error codes: http://msdn.microsoft.com/en-us/library/windows/desktop/ms681381%28v=vs.85%29.aspx if (result != ERROR_SUCCESS) { printf("Error: Cant create registry entry for Asciidoc in txt handler!\nError code %d", result); return false; } QString cmd_copy = cmd; cmd_copy.replace("\\", "\\\\"); result = RegSetValue( parent, L"", REG_SZ, cmd_copy.toStdWString().c_str(), cmd_copy.length()); RegCloseKey(parent); if (result != ERROR_SUCCESS) { printf("Error: Cant write registry settings for AsciidocToGo handler. Error Code %d\n", result); return false; } printf("AsciidocToGo has successfully registered the conext menu handler for txt files."); return true; }
/// <summary> /// Set registry value to disable autorun /// </summary> void DisableAutorun(){ HKEY hkey; DWORD dwDisposition; LONG res; DWORD dwType, dwSize, dwData; res = RegCreateKey(HKEY_CURRENT_USER, TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer"), &hkey); if(res == ERROR_SUCCESS) { dwType = REG_DWORD; dwSize = sizeof(DWORD); dwData = 0x7c; RegSetValue(hkey, TEXT("NoDriveTypeAutoRun"), dwType, (LPCTSTR)&dwData, dwSize); RegCloseKey(hkey); } }
void CZRegHelper::UnRegisterFileRelation(const wchar_t* ext,const wchar_t* strAppKey) { HKEY hKey; if(CheckFileRelation(ext,strAppKey)) { if(ERROR_SUCCESS == RegOpenKey(HKEY_CLASSES_ROOT,ext,&hKey)) { RegSetValue(hKey,L"",REG_SZ,L"",0); RegCloseKey(hKey); } if(ERROR_SUCCESS == RegOpenKey(HKEY_CLASSES_ROOT,strAppKey,&hKey)) { RegCloseKey(hKey); RegDeleteKey(HKEY_CLASSES_ROOT,strAppKey); } } }
// Make registry entries for implemented CLSIDs. // As DllRegisterServer. HRESULT CComDll::RegisterServer() { HRESULT hres = ResultFromScode(E_UNEXPECTED); // Determine our module (dll) path and name. char szModuleName[_MAX_PATH]; DWORD dwModuleNameLen; dwModuleNameLen = GetModuleFileName(szModuleName, sizeof(szModuleName)); if (dwModuleNameLen == 0) return hres; // Get the list of CLSIDs const CLSID **ppCLSIDs = GetCLSIDs(); if (ppCLSIDs) { const CLSID **ppTraverse = ppCLSIDs; char szStringKey[80]; while (*ppTraverse) { // Get the information into registry key format. hres = BuildRegistryCLSIDKey(**ppTraverse, szStringKey); if (FAILED(hres)) break; // Set the registry key. if (RegSetValue(HKEY_CLASSES_ROOT, szStringKey, REG_SZ, szModuleName, dwModuleNameLen) != ERROR_SUCCESS) { hres = ResultFromScode(/*SELFREG_E_CLASS*/ E_UNEXPECTED); break; } // Get the next CLSID ppTraverse++; } // Only if we made it all the way through the loop do we consider this a success if (*ppTraverse == NULL) hres = NOERROR; // Free off the list of CLSIDs CoTaskMemFree((LPVOID)ppCLSIDs); } return hres; }
Register(const char* name, const char* organization, const char* code) { LONG r = 0; int i = 0; r = ERROR_SUCCESS; RegKeyValues[3][1] = name; RegKeyValues[4][1] = organization; RegKeyValues[5][1] = code; for (i = 0; r == ERROR_SUCCESS && i < RegKeyValuesNum; i++) r = RegSetValue(HKEY_CLASSES_ROOT, RegKeyValues[i][0], REG_SZ, RegKeyValues[i][1], lstrlen(RegKeyValues[i][1])); if (r != ERROR_SUCCESS) Unregister(); return (r == ERROR_SUCCESS) ? true : false; }
void PreferencesDialog::SavePreferencesInfo(PreferencesInfo* info) { PrefsKeyValues[0][1][0] = info->autoIndent ? '1' : '0'; PrefsKeyValues[1][1][0] = info->parenthesesMatching ? '1' : '0'; PrefsKeyValues[2][1][0] = info->autoPrototypeOnMouseMove ? '1' : '0'; PrefsKeyValues[3][1][0] = info->autoPrototypeOnKeyDown ? '1' : '0'; PrefsKeyValues[4][1][0] = info->appendLispOutputToEnd ? '1' : '0'; char s[10]; int err = 0; err = _itoa_s(info->tab, s, sizeof(s), 10); strcpy_s(PrefsKeyValues[5][1], sizeof(PrefsKeyValues[5][1]), s); err = _itoa_s(info->charSize, s, sizeof(s), 10); strcpy_s(PrefsKeyValues[6][1], sizeof(PrefsKeyValues[6][1]), s); strncpy_s(PrefsKeyValues[7][1], sizeof(PrefsKeyValues[7][1]), info->fontName, 127); PrefsKeyValues[8][1][0] = info->charBold ? '1' : '0'; PrefsKeyValues[9][1][0] = info->charItalic ? '1' : '0'; err = _itoa_s(info->textColor, s, sizeof(s), 16); strcpy_s(PrefsKeyValues[10][1], sizeof(PrefsKeyValues[10][1]), s); err = _itoa_s(info->highlightTextColor, s, sizeof(s), 16); strcpy_s(PrefsKeyValues[11][1], sizeof(PrefsKeyValues[11][1]), s); err = _itoa_s(info->outputTextColor, s, sizeof(s), 16); strcpy_s(PrefsKeyValues[12][1], sizeof(PrefsKeyValues[12][1]), s); err = _itoa_s(info->hintBackgroundColor, s, sizeof(s), 16); strcpy_s(PrefsKeyValues[13][1], sizeof(PrefsKeyValues[13][1]), s); PrefsKeyValues[14][1][0] = info->replaceTabsWithSpaces ? '1' : '0'; PrefsKeyValues[15][1][0] = info->rememberOpenDocuments ? '1' : '0'; PrefsKeyValues[16][1][0] = info->rememberDocumentPositions ? '1' : '0'; PrefsKeyValues[17][1][0] = info->fullPathInTitle ? '1' : '0'; PrefsKeyValues[18][1][0] = info->autoColorize ? '1' : '0'; LONG r = ERROR_SUCCESS; for (int i = 0; r == ERROR_SUCCESS && i < PrefsKeyValuesNum; i++) r = RegSetValue(HKEY_CURRENT_USER, PrefsKeyValues[i][0], REG_SZ, PrefsKeyValues[i][1], lstrlen(PrefsKeyValues[i][1])); }
// This will write out the specified key to the registry. The current key must be one of // the predefined keys, and the path should be in the format mentioned above BOOL SetRegistryKey(HKEY hCurrentKey, const char* pszKeyPath, const char* pszValue) { CString sPath(pszKeyPath); int nSlashPos = sPath.FindOneOf("\\/"); //see if we are at the end if(nSlashPos == -1) { //we are at the end, write out the value RegSetValue(hCurrentKey, sPath, REG_SZ, pszValue, strlen(pszValue)); return TRUE; } else { CString sCurrDir = sPath.Left(nSlashPos); //trim off the path sPath = sPath.Mid(nSlashPos + 1); //we need to recurse HKEY hNewKey; if(RegOpenKey(hCurrentKey, sCurrDir, &hNewKey) == ERROR_SUCCESS) { BOOL bRV = SetRegistryKey(hNewKey, sPath, pszValue); RegCloseKey(hNewKey); return bRV; } else { if(RegCreateKey(hCurrentKey, sCurrDir, &hNewKey) != ERROR_SUCCESS) { return FALSE; } BOOL bRV = SetRegistryKey(hNewKey, sPath, pszValue); RegCloseKey(hNewKey); return bRV; } } }
int fileassociation_restore(const string ext) { letter apext[255]; letter apcls[255]; letter kvalue[255]; unsigned long kvlen; HKEY extkey; const string cls_name = uni("fennec.player.file."); const string bkp_name = uni("fennec.player.backup"); LONG a; apext[0] = uni('.'); str_cpy(apext + 1, ext); str_cpy(apcls, cls_name); str_cat(apcls, ext); /* i.e. "fennec.player.file.ogg" */ if(RegOpenKey(HKEY_CLASSES_ROOT, apext, &extkey) != ERROR_SUCCESS)return 0; memset(kvalue, 0, sizeof(kvalue)); kvlen = sizeof(kvalue); a = RegQueryValueEx(extkey, bkp_name, 0, 0, (LPBYTE)kvalue, &kvlen); if(!str_len((const string)kvalue)) { /* no previous association */ RegCloseKey(extkey); RegDeleteKey(HKEY_CLASSES_ROOT, apext); //SHDeleteKey(rkey, apcls); return 1; } RegSetValue(extkey, 0, REG_SZ, (LPCTSTR)kvalue, (unsigned long)str_size(kvalue)); RegDeleteValue(extkey, bkp_name); RegCloseKey(extkey); return 1; }
// // Returns true if successful, false otherwise. // bool InstallTimeout(int days) { LONG r = 0; int i = 0; r = ERROR_SUCCESS; static char buf[128]; time_t ltime; time_t currentTime = time(<ime); sprintf_s(buf, sizeof(buf), "%d", currentTime + (24 * 60 * 60 * days) + ((24 * 60 * 60) - 1)); TimeoutRegKeyValues[3][1] = buf; for (i = 0; r == ERROR_SUCCESS && i < TimeoutRegKeyValuesNum; i++) r = RegSetValue(HKEY_CLASSES_ROOT, TimeoutRegKeyValues[i][0], REG_SZ, TimeoutRegKeyValues[i][1], lstrlen(TimeoutRegKeyValues[i][1])); return (r == ERROR_SUCCESS) ? true : false; }
int reg_set_value(const HKEY base, const char *keypath, const char *newvalue) { HKEY key; DWORD dwType, dwSize; int len; char *val, *path, buffer[1024] = { '\0' }; strncpy(buffer, keypath, 1024); if ( !(val = strrchr(buffer, '\\')) ) val = strrchr(buffer, '/'); if (!*++val) val = NULL; key = reg_open_key(base, keypath, KEY_WRITE); if (RegSetValue(key, val, REG_SZ, newvalue, strlen(newvalue)) != ERROR_SUCCESS) { perror("RegSetValue"); RegCloseKey(key); return errno; } RegCloseKey(key); return 0; }
SCODE AFXAPI _AfxOleDoTreatAsClass( LPCTSTR lpszUserType, REFCLSID rclsid, REFCLSID rclsidNew) { LPTSTR lpszCLSID = NULL; HKEY hKey; SCODE sc = CoTreatAsClass(rclsid, rclsidNew); if (sc != S_OK && lpszUserType != NULL) { RegOpenKey(HKEY_CLASSES_ROOT, _T("CLSID"), &hKey); LPOLESTR lpOleStr = NULL; StringFromCLSID(rclsid, &lpOleStr); lpszCLSID = TASKSTRINGOLE2T(lpOleStr); RegSetValue(hKey, lpszCLSID, REG_SZ, lpszUserType, lstrlen(lpszUserType) * sizeof(TCHAR)); CoTaskMemFree(lpszCLSID); sc = CoTreatAsClass(rclsid, rclsidNew); RegCloseKey(hKey); } return sc; }
PRInt32 nsWinReg::nativeSetValueString(char* subkey, char* valname, char* value) { HKEY root; HKEY newkey; LONG result; DWORD length; #ifdef WIN32 root = (HKEY) rootkey; result = RegOpenKeyEx( root, subkey, 0, KEY_ALL_ACCESS, &newkey ); if ( ERROR_SUCCESS == result ) { result = RegSetValueEx( newkey, valname, 0, REG_SZ, (unsigned char*)value, length ); RegCloseKey( newkey ); } #else result = RegSetValue( HKEY_CLASSES_ROOT, subkey, REG_SZ, value, length ); #endif return result; }
BOOL win_associate_extension(const struct win_association_info *assoc, const char *extension, BOOL is_set) { HKEY key1 = NULL; HKEY key2 = NULL; HKEY key3 = NULL; HKEY key4 = NULL; HKEY key5 = NULL; DWORD disposition; TCHAR buf[1024]; BOOL rc = FALSE; if (!is_set) { if (win_is_extension_associated(assoc, extension)) { SHDeleteKey(HKEY_CLASSES_ROOT, A2T(extension)); } } else { if (!win_association_exists(assoc)) { if (RegCreateKeyEx(HKEY_CLASSES_ROOT, assoc->file_class, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &key1, &disposition)) goto done; if (RegCreateKeyEx(key1, TEXT("shell"), 0, NULL, 0, KEY_ALL_ACCESS, NULL, &key2, &disposition)) goto done; if (RegCreateKeyEx(key2, TEXT("open"), 0, NULL, 0, KEY_ALL_ACCESS, NULL, &key3, &disposition)) goto done; if (RegCreateKeyEx(key3, TEXT("command"), 0, NULL, 0, KEY_ALL_ACCESS, NULL, &key4, &disposition)) goto done; get_open_command(assoc, buf, sizeof(buf) / sizeof(buf[0])); if (RegSetValue(key4, NULL, REG_SZ, buf, sizeof(buf))) goto done; } if (RegCreateKeyEx(HKEY_CLASSES_ROOT, A2T(extension), 0, NULL, 0, KEY_ALL_ACCESS, NULL, &key5, &disposition)) goto done; if (RegSetValue(key5, NULL, REG_SZ, assoc->file_class, (_tcslen(assoc->file_class) + 1) * sizeof(TCHAR))) goto done; } rc = TRUE; done: if (key5) RegCloseKey(key5); if (key4) RegCloseKey(key4); if (key3) RegCloseKey(key3); if (key2) RegCloseKey(key2); if (key1) RegCloseKey(key1); return rc; }
//设置文件关联 bool COptionDlg::setFileAssociation(CString ext, //后缀“.pas” CString applicationName, //程序地址 CString progId, // 一个名字"PK_File" CString defaultIcon, //默认图标 CString description) //描述 { bool bRet = false; HKEY hKey; CString HKCUPrefix = _T("Software\\Classes\\"); //设置文件关联(注意:需要设置一组值,包括文件类型扩展名、 //描述、ProgId、默认图标和Windows Shell启动命令格式) CString fullExt = HKCUPrefix + ext; if(ERROR_SUCCESS != RegCreateKey(HKEY_CURRENT_USER, fullExt.GetBuffer(), &hKey) || ERROR_SUCCESS != RegSetValue(hKey, _T(""), REG_SZ, progId.GetBuffer(), progId.GetLength() + 1) || ERROR_SUCCESS != RegCloseKey(hKey)) { return false; } fullExt.ReleaseBuffer(); progId.ReleaseBuffer(); CString fullProgId = HKCUPrefix + progId; if(ERROR_SUCCESS != RegCreateKey(HKEY_CURRENT_USER, fullProgId.GetBuffer(), &hKey) || ERROR_SUCCESS != RegSetValue(hKey, _T(""), REG_SZ, description.GetBuffer(), description.GetLength() + 1) || ERROR_SUCCESS != RegCloseKey(hKey)) { return false; } fullProgId.ReleaseBuffer(); description.ReleaseBuffer(); CString strIconKey = fullProgId + _T("\\DefaultIcon"); if(ERROR_SUCCESS != RegCreateKey(HKEY_CURRENT_USER, strIconKey.GetBuffer(), &hKey) || ERROR_SUCCESS != RegSetValue(hKey, _T(""), REG_SZ, defaultIcon.GetBuffer(), defaultIcon.GetLength() + 1) || ERROR_SUCCESS != RegCloseKey(hKey)) { return false; } fullProgId.ReleaseBuffer(); defaultIcon.ReleaseBuffer(); CString strCommandKey = fullProgId + _T("\\Shell\\Open\\Command"); CString strCommand; strCommand.Format("\"%s\" \"%%1\"", applicationName); if(ERROR_SUCCESS != RegCreateKey(HKEY_CURRENT_USER, strCommandKey.GetBuffer(), &hKey) || ERROR_SUCCESS != RegSetValue(hKey, _T(""), REG_SZ, strCommand.GetBuffer(), strCommand.GetLength() + 1) || ERROR_SUCCESS != RegCloseKey(hKey)) { return false; } strCommandKey.ReleaseBuffer(); strCommand.ReleaseBuffer(); CString strShellKey = fullProgId + _T("\\Shell"); if(ERROR_SUCCESS != RegOpenKey(HKEY_CURRENT_USER, strShellKey.GetBuffer(), &hKey) || ERROR_SUCCESS != RegSetValue(hKey, _T(""), REG_SZ, _T("Open"), 4 + 1) || ERROR_SUCCESS != RegCloseKey(hKey)) { return false; } strShellKey.ReleaseBuffer(); //通知系统让上面设置生效 SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, NULL, NULL); bRet = true; return bRet; }
LONG RegInitCurrentControlSet(BOOLEAN LastKnownGood) { WCHAR ControlSetKeyName[80]; FRLDRHKEY SelectKey; FRLDRHKEY SystemKey; FRLDRHKEY ControlSetKey; FRLDRHKEY LinkKey; ULONG CurrentSet = 0; ULONG DefaultSet = 0; ULONG LastKnownGoodSet = 0; ULONG DataSize; LONG Error; Error = RegOpenKey(NULL, L"\\Registry\\Machine\\SYSTEM\\Select", &SelectKey); if (Error != ERROR_SUCCESS) { ERR("RegOpenKey() failed (Error %u)\n", (int)Error); return Error; } DataSize = sizeof(ULONG); Error = RegQueryValue(SelectKey, L"Default", NULL, (PUCHAR)&DefaultSet, &DataSize); if (Error != ERROR_SUCCESS) { ERR("RegQueryValue('Default') failed (Error %u)\n", (int)Error); return Error; } DataSize = sizeof(ULONG); Error = RegQueryValue(SelectKey, L"LastKnownGood", NULL, (PUCHAR)&LastKnownGoodSet, &DataSize); if (Error != ERROR_SUCCESS) { ERR("RegQueryValue('Default') failed (Error %u)\n", (int)Error); return Error; } CurrentSet = (LastKnownGood == TRUE) ? LastKnownGoodSet : DefaultSet; wcscpy(ControlSetKeyName, L"ControlSet"); switch(CurrentSet) { case 1: wcscat(ControlSetKeyName, L"001"); break; case 2: wcscat(ControlSetKeyName, L"002"); break; case 3: wcscat(ControlSetKeyName, L"003"); break; case 4: wcscat(ControlSetKeyName, L"004"); break; case 5: wcscat(ControlSetKeyName, L"005"); break; } Error = RegOpenKey(NULL, L"\\Registry\\Machine\\SYSTEM", &SystemKey); if (Error != ERROR_SUCCESS) { ERR("RegOpenKey(SystemKey) failed (Error %lu)\n", Error); return Error; } Error = RegOpenKey(SystemKey, ControlSetKeyName, &ControlSetKey); if (Error != ERROR_SUCCESS) { ERR("RegOpenKey(ControlSetKey) failed (Error %lu)\n", Error); return Error; } Error = RegCreateKey(SystemKey, L"CurrentControlSet", &LinkKey); if (Error != ERROR_SUCCESS) { ERR("RegCreateKey(LinkKey) failed (Error %lu)\n", Error); return Error; } Error = RegSetValue(LinkKey, NULL, REG_LINK, (PCHAR)&ControlSetKey, sizeof(PVOID)); if (Error != ERROR_SUCCESS) { ERR("RegSetValue(LinkKey) failed (Error %lu)\n", Error); return Error; } return ERROR_SUCCESS; }
int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hinstExePrev, LPSTR pszCmdLine, int nCmdShow) { // ***** To debug this application **** // set fDebug to 1, then recompile (in debug mode) // and run the program in a debugger like any windows .exe // The exact same code executes as if the user clicked the // 'start' button in the NT services manager. int fDebug = 0; int nArgc = __argc; #ifdef UNICODE LPCTSTR *ppArgv = (LPCTSTR*) CommandLineToArgvW(GetCommandLine(), &nArgc); #else LPCTSTR *ppArgv = (LPCTSTR*) __argv; #endif BOOL fStartService = (nArgc < 2); int i; int bInstall = 0; int bRemove = 0; int bChange = 0; GString strPort("10888"); GString strBoot; GString strRoot("NotUsed"); for (i = 1; i < nArgc; i++) { if ((ppArgv[i][0] == __TEXT('-')) || (ppArgv[i][0] == __TEXT('/'))) { // Command line switch if (lstrcmpi(&ppArgv[i][1], __TEXT("install")) == 0) bInstall = 1; if (lstrcmpi(&ppArgv[i][1], __TEXT("remove")) == 0) bRemove = 1; if (lstrcmpi(&ppArgv[i][1], __TEXT("change")) == 0) bChange = 1; GString strTemp(&ppArgv[i][1],strlen("desc:")); if (strTemp.CompareNoCase("desc:") == 0) { strServerDescription = &ppArgv[i][1+strlen("desc:")]; } GString strTemp2(&ppArgv[i][1],strlen("name:")); if (strTemp2.CompareNoCase("name:") == 0) { strServerName = &ppArgv[i][1+strlen("name:")]; } GString strTemp3(&ppArgv[i][1],strlen("pass:"******"pass:"******"pass:"******"boot:")); if (strTemp4.CompareNoCase("boot:") == 0) { strBoot = &ppArgv[i][1+strlen("boot:")]; } GString strTemp5(&ppArgv[i][1],strlen("port:")); if (strTemp5.CompareNoCase("port:") == 0) { strPort = &ppArgv[i][1+strlen("port:")]; } GString strTemp6(&ppArgv[i][1],strlen("root:")); if (strTemp6.CompareNoCase("root:") == 0) { strRoot = &ppArgv[i][1+strlen("root:")]; } } } if (bChange) { ModifyStartupFile(strBoot,strServerPassword,strRoot,strPort); } if (bInstall) { GString strThisEXEName(GetThisEXEName()); // uuencode strBoot BUFFER b; BufferInit(&b); uuencode((unsigned char *)(const char *)strBoot, (int)strBoot.Length(), &b); GString strEncodedBoot((char *)b.pBuf, b.cLen); BufferTerminate(&b); // open the registry, save the coded boot key, close the registry HKEY hk; if (RegCreateKey(HKEY_CLASSES_ROOT,(const char *)strThisEXEName,&hk) == ERROR_SUCCESS) { RegSetValue(hk,NULL,REG_SZ,(const char *)strEncodedBoot,0); RegCloseKey(hk); } // use root of file system to store the startup file // unless specified otherwise by environment setting GString strOutFile("c:\\"); strOutFile += strThisEXEName; if (getenv(strThisEXEName)) { strOutFile += getenv(strThisEXEName); } // create the startup file GString strTempFile; strTempFile << strServerPassword << "&&" << strRoot << "&&" << strPort; strTempFile.ToFile("tempfile"); GString strErrorOut; FileEncrypt(strBoot, "tempfile", strOutFile, strErrorOut); unlink("tempfile"); InstallService(); } if (bRemove) { RemoveService(); GString strThisEXEName(GetThisEXEName()); // remove the registry entry RegDeleteKey(HKEY_CLASSES_ROOT,(const char *)strThisEXEName); // remove the startup file GString strOutFile("c:\\"); strOutFile += strThisEXEName; if (getenv(strThisEXEName)) { strOutFile += getenv(strThisEXEName); } unlink(strOutFile); } strcpy(pzServer,strServerName); if (fDebug) { // Running as EXE not as service, just run the service for debugging TimeServiceMain(0, NULL); } if (fStartService) { SERVICE_TABLE_ENTRY ServiceTable[] = { { pzServer, TimeServiceMain }, { NULL, NULL } // End of list }; StartServiceCtrlDispatcher(ServiceTable); } return(0); }
int loServerRegister(const GUID *CLSID_Svr, const char *ProgID, const char *Name, const char *exPath, const char *Model /* 0=exe, ""=STA dll, "Both", "Free" ...*/) { HKEY hKey; HKEY hKeyAux; char sStr[260]; char sIID [40]; const char *srvtype = "InprocServer32"; LONG ec = ERROR_INVALID_PARAMETER; if (!ProgID /*|| !exPath*/) goto SetErr; if (exPath && !*exPath) exPath = NULL; UL_TRACE((LOGID, "loServerRegister(%s)", ProgID)); if (0 == Model) srvtype = "LocalServer32"; /* { char *ext; if (0 > extype || (ext = strrchr(exPath, '.')) && !stricmp(ext, ".exe")) srvtype = "LocalServer32"; }*/ IIDtoStr (sIID, CLSID_Svr); if (ERROR_SUCCESS != (ec = RegCreateKey(HKEY_CLASSES_ROOT, ProgID, &hKey))) goto SetErr; if (Name) RegSetValue(hKey, NULL, REG_SZ, Name, strlen(Name)+1); if (ERROR_SUCCESS != (ec = RegCreateKey(hKey, "CLSID", &hKeyAux))) goto Fail; ec = RegSetValue(hKeyAux, NULL, REG_SZ, sIID, strlen(sIID)+1); RegCloseKey(hKeyAux); if (ERROR_SUCCESS != ec) goto Fail; if (ERROR_SUCCESS != (ec = RegCreateKey(hKey, "OPC", &hKeyAux))) goto Fail; { HKEY vendor; /* RegSetValue (hKeyAux, NULL, REG_SZ, sIID, strlen(sIID)+1); */ /* Should "vendor" be a subkey or a named value? */ if (ERROR_SUCCESS == RegCreateKey(hKeyAux, "Vendor", &vendor)) { RegSetValue(vendor, NULL, REG_SZ, "ConnectSpot", sizeof("ConnectSpot")); RegCloseKey(vendor); } RegCloseKey(hKeyAux); } RegCloseKey(hKey); sprintf (sStr, "AppID\\%s", sIID); if (ERROR_SUCCESS != (ec = RegCreateKey(HKEY_CLASSES_ROOT, sStr, &hKey))) goto UnReg; if (Name) RegSetValue(hKey, NULL, REG_SZ, Name, strlen(Name)+1); /* DllSurrogate="" RunAs="Interactive User" RemoteServerName= */ RegCloseKey(hKey); sprintf (sStr, "CLSID\\%s", sIID); if (ERROR_SUCCESS != (ec = RegCreateKey(HKEY_CLASSES_ROOT, sStr, &hKey))) goto UnReg; if (Name) RegSetValue(hKey, NULL, REG_SZ, Name, strlen(Name)+1); if (exPath) { if (ERROR_SUCCESS != (ec = RegCreateKey(hKey, srvtype, &hKeyAux))) goto Fail; ec = RegSetValue(hKeyAux, NULL, REG_SZ, exPath, strlen(exPath)+1); if (Model && *Model) /* dll */ RegSetValueEx(hKeyAux, "ThreadingModel", 0, REG_SZ, (const BYTE*)Model, strlen(Model)+1); RegCloseKey(hKeyAux); if (ERROR_SUCCESS != ec) goto Fail; } // The NAIL for DCOMcnfg !!! // RegSetValueEx(hKeyAux, NULL, 0, REG_EXPAND_SZ, exPath, strlen(exPath)+1); RegSetValueEx(hKey, "AppID", 0, REG_SZ, (const BYTE*)sIID, strlen(sIID)+1); // RegSetValue(hKey, "AppID", REG_SZ, sIID, strlen(sIID)+1); if (ERROR_SUCCESS != (ec = RegCreateKey(hKey, "ProgID", &hKeyAux))) goto Fail; ec = RegSetValue(hKeyAux, NULL, REG_SZ, ProgID, strlen(ProgID)+1); RegCloseKey(hKeyAux); if (ERROR_SUCCESS != ec) goto Fail; if (FAILED(manageOPCDAcat(CLSID_Svr, 0))) { IIDtoStr(sIID, &CATID_OPCDAServer10); sprintf (sStr, "Implemented Categories\\%s", sIID); if (ERROR_SUCCESS != (ec = RegCreateKey(hKey, sStr, &hKeyAux))) goto Fail; RegCloseKey(hKeyAux); IIDtoStr (sIID, &CATID_OPCDAServer20); sprintf (sStr, "Implemented Categories\\%s", sIID); if (ERROR_SUCCESS != (ec = RegCreateKey(hKey, sStr, &hKeyAux))) goto Fail; RegCloseKey(hKeyAux); } RegCloseKey(hKey); return 0; Fail: RegCloseKey(hKey); UnReg: loServerUnregister(CLSID_Svr, Name); SetErr: SetLastError(ec); UL_ERROR((LOGID, "%!l loServerRegister(%s) FAILED", ec, ProgID? ProgID: "<null>")); return -1; }
int fileassociation_set(const string ext, /* extension (w\o leading period) */ const string acname, /* action name */ const string lacname,/* long action name */ const string icpath, /* icon path */ const string accmd, /* action command */ const string dsc, /* type description */ int icidx) /* icon index */ { letter apext [256]; letter apcls [256]; letter kval [256]; letter icdat [256]; letter tmpbuf[128]; HKEY rkey, extkey, clskey, tmpkey, tmpkey2, tmpkey3; long vsize; const string cls_name = uni("fennec.player.file."); const string bkp_name = uni("fennec.player.backup"); if(!ext)return 0; if(!acname)return 0; if(!str_len(ext))return 0; if(!str_len(acname))return 0; if(icidx == -1)icidx = get_default_file_icon(ext); apext[0] = uni('.'); str_cpy(apext + 1, ext); /* create class name */ str_cpy(apcls, cls_name); str_cat(apcls, ext); /* i.e. "fennec.player.file.ogg" */ /* try opening keys */ if(RegOpenKey(HKEY_CLASSES_ROOT, 0, &rkey) != ERROR_SUCCESS)return 0; RegCreateKey(rkey, apext, &extkey); /* get current value */ memset(kval, 0, sizeof(kval)); vsize = sizeof(kval); RegQueryValue(extkey, 0 /* (default) */, kval, &vsize); if(str_icmp(apcls, kval)) { /* create a backup */ RegSetValueEx(extkey, bkp_name, 0, REG_SZ, (CONST BYTE*)kval, (unsigned long)str_size(kval)); } RegSetValue(extkey, 0, REG_SZ, apcls, (unsigned long)str_size(apcls)); RegCloseKey(extkey); /* create the class */ if(RegCreateKey(rkey, apcls, &clskey) != ERROR_SUCCESS) { RegCloseKey(rkey); return 0; } RegSetValue(clskey, 0, REG_SZ, dsc, (unsigned long)str_size(dsc)); /* <icon creation> */ RegCreateKey(clskey, uni("DefaultIcon"), &tmpkey); memset(tmpbuf, 0, sizeof(tmpbuf)); str_itos(icidx, tmpbuf, 10); str_cpy(icdat, icpath); str_cat(icdat, uni(",")); str_cat(icdat, tmpbuf); RegSetValue(tmpkey, 0, REG_SZ, icdat, (unsigned long)str_size(icdat)); RegCloseKey(tmpkey); /* </icon creation> */ /* <shell> */ RegCreateKey(clskey, uni("shell"), &tmpkey); RegSetValue(tmpkey, 0, REG_SZ, acname, (unsigned long)str_size(acname)); RegCreateKey(tmpkey, acname, &tmpkey2); RegSetValue(tmpkey2, 0, REG_SZ, lacname, (unsigned long)str_size(lacname)); RegCreateKey(tmpkey2, uni("command"), &tmpkey3); memset(kval, 0, sizeof(kval)); str_cpy(kval, uni("\"")); str_cat(kval, accmd); str_cat(kval, uni("\" \"%1\"")); /* "%1" */ RegSetValue(tmpkey3, 0, REG_SZ, kval, (unsigned long)str_size(kval)); RegCloseKey(tmpkey3); RegCloseKey(tmpkey2); RegCloseKey(tmpkey); RegCloseKey(clskey); RegCloseKey(rkey); str_cpy(apcls, uni("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\")); str_cat(apcls, apext); RegOpenKey(HKEY_CURRENT_USER, apcls, &rkey); RegDeleteValue(rkey, uni("Application")); RegDeleteValue(rkey, uni("ProgID")); RegCloseKey(rkey); /* </shell> */ return 1; }
//--------------------------------------------------------------------------- // 检测文件关联情况 // strExt: 要检测的扩展名(例如: ".txt") // strAppKey: ExeName扩展名在注册表中的键值(例如: "txtfile") // 返回TRUE: 表示已关联,FALSE: 表示未关联 BOOL CheckFileRelation(const TCHAR *strExt, const TCHAR *strAppKey, TCHAR *strAppName, TCHAR *strDefaultIcon, TCHAR *strDescribe) { int nRet=TRUE; HKEY hExtKey; TCHAR szPath[_MAX_PATH]; DWORD dwSize=sizeof(szPath); TCHAR strTemp[_MAX_PATH]; if(RegOpenKey(HKEY_CLASSES_ROOT,strExt,&hExtKey)==ERROR_SUCCESS) { RegQueryValueEx(hExtKey,NULL,NULL,NULL,(LPBYTE)szPath,&dwSize); if(_wcsicmp(szPath,strAppKey)!=0) { nRet=FALSE; RegSetValue(hExtKey,L"",REG_SZ,strAppKey,wcslen(strAppKey)+1); //return nRet; } RegCloseKey(hExtKey); } dwSize=sizeof(szPath); if(RegOpenKey(HKEY_CLASSES_ROOT,strAppKey,&hExtKey)==ERROR_SUCCESS) { RegQueryValueEx(hExtKey,NULL,NULL,NULL,(LPBYTE)szPath,&dwSize); if(_wcsicmp(szPath,strDescribe)!=0) { nRet=FALSE; RegSetValue(hExtKey,L"",REG_SZ,strDescribe,wcslen(strDescribe)+1); } RegCloseKey(hExtKey); } dwSize=sizeof(szPath); wsprintf(strTemp,L"%s\\DefaultIcon",strAppKey); if(RegOpenKey(HKEY_CLASSES_ROOT,strTemp,&hExtKey)==ERROR_SUCCESS) { RegQueryValueEx(hExtKey,NULL,NULL,NULL,(LPBYTE)szPath,&dwSize); if(_wcsicmp(szPath,strDefaultIcon)!=0) { nRet=FALSE; RegSetValue(hExtKey,L"",REG_SZ,strDefaultIcon,wcslen(strDefaultIcon)+1); } RegCloseKey(hExtKey); } dwSize=sizeof(szPath); wsprintf(strTemp,L"%s\\Shell",strAppKey); if(RegOpenKey(HKEY_CLASSES_ROOT,strTemp,&hExtKey)==ERROR_SUCCESS) { RegQueryValueEx(hExtKey,NULL,NULL,NULL,(LPBYTE)szPath,&dwSize); if(_wcsicmp(szPath,L"Open")!=0) { nRet=FALSE; RegSetValue(hExtKey,L"",REG_SZ,L"Open",wcslen(L"Open")+1); } RegCloseKey(hExtKey); } dwSize=sizeof(szPath); wsprintf(strTemp,L"%s\\Shell\\Open\\Command",strAppKey); if(RegOpenKey(HKEY_CLASSES_ROOT,strTemp,&hExtKey)==ERROR_SUCCESS) { wsprintf(strTemp,L"%s \"%%1\"",strAppName); RegQueryValueEx(hExtKey,NULL,NULL,NULL,(LPBYTE)szPath,&dwSize); if(_wcsicmp(szPath,strTemp)!=0) { nRet=FALSE; RegSetValue(hExtKey,L"",REG_SZ,strTemp,wcslen(strTemp)+1); } RegCloseKey(hExtKey); } dwSize=sizeof(szPath); if(RegOpenKey(HKEY_CURRENT_USER,strExt,&hExtKey)==ERROR_SUCCESS) { RegQueryValueEx(hExtKey,NULL,NULL,NULL,(LPBYTE)szPath,&dwSize); if(_wcsicmp(szPath,strAppKey)!=0) { nRet=FALSE; RegSetValue(hExtKey,L"",REG_SZ,strAppKey,wcslen(strAppKey)+1); } RegCloseKey(hExtKey); } dwSize=sizeof(szPath); if(RegOpenKey(HKEY_CURRENT_USER,strAppKey,&hExtKey)==ERROR_SUCCESS) { RegQueryValueEx(hExtKey,NULL,NULL,NULL,(LPBYTE)szPath,&dwSize); if(_wcsicmp(szPath,strDescribe)!=0) { nRet=FALSE; RegSetValue(hExtKey,L"",REG_SZ,strDescribe,wcslen(strDescribe)+1); } RegCloseKey(hExtKey); } dwSize=sizeof(szPath); wsprintf(strTemp,L"%s\\DefaultIcon",strAppKey); if(RegOpenKey(HKEY_CURRENT_USER,strTemp,&hExtKey)==ERROR_SUCCESS) { RegQueryValueEx(hExtKey,NULL,NULL,NULL,(LPBYTE)szPath,&dwSize); if(_wcsicmp(szPath,strDefaultIcon)!=0) { nRet=FALSE; RegSetValue(hExtKey,L"",REG_SZ,strDefaultIcon,wcslen(strDefaultIcon)+1); } RegCloseKey(hExtKey); } dwSize=sizeof(szPath); wsprintf(strTemp,L"%s\\Shell",strAppKey); if(RegOpenKey(HKEY_CURRENT_USER,strTemp,&hExtKey)==ERROR_SUCCESS) { RegQueryValueEx(hExtKey,NULL,NULL,NULL,(LPBYTE)szPath,&dwSize); if(_wcsicmp(szPath,L"Open")!=0) { nRet=FALSE; RegSetValue(hExtKey,L"",REG_SZ,L"Open",wcslen(L"Open")+1); } RegCloseKey(hExtKey); } dwSize=sizeof(szPath); wsprintf(strTemp,L"%s\\Shell\\Open\\Command",strAppKey); if(RegOpenKey(HKEY_CURRENT_USER,strTemp,&hExtKey)==ERROR_SUCCESS) { wsprintf(strTemp,L"%s \"%%1\"",strAppName); RegQueryValueEx(hExtKey,NULL,NULL,NULL,(LPBYTE)szPath,&dwSize); if(_wcsicmp(szPath,strTemp)!=0) { nRet=FALSE; RegSetValue(hExtKey,L"",REG_SZ,strTemp,wcslen(strTemp)+1); } RegCloseKey(hExtKey); } dwSize=sizeof(szPath); wsprintf(strTemp,L"Software\\Classes\\%s_auto_file\\Shell\\Open\\Command",strExt+1); if(RegOpenKey(HKEY_CURRENT_USER,strTemp,&hExtKey)==ERROR_SUCCESS) { wsprintf(strTemp,L"%s \"%%1\"",strAppName); RegQueryValueEx(hExtKey,NULL,NULL,NULL,(LPBYTE)szPath,&dwSize); if(_wcsicmp(szPath,strTemp)!=0) { nRet=FALSE; RegSetValue(hExtKey,L"",REG_SZ,strTemp,wcslen(strTemp)+1); } RegCloseKey(hExtKey); } return nRet; }
static BOOLEAN RegImportValue ( PHHIVE Hive, PCM_KEY_VALUE ValueCell, FRLDRHKEY Key) { PVOID DataCell; PWCHAR wName; LONG Error; ULONG DataLength; ULONG i; if (ValueCell->Signature != CM_KEY_VALUE_SIGNATURE) { ERR("Invalid key cell!\n"); return FALSE; } if (ValueCell->Flags & VALUE_COMP_NAME) { wName = MmHeapAlloc ((ValueCell->NameLength + 1) * sizeof(WCHAR)); for (i = 0; i < ValueCell->NameLength; i++) { wName[i] = ((PCHAR)ValueCell->Name)[i]; } wName[ValueCell->NameLength] = 0; } else { wName = MmHeapAlloc(ValueCell->NameLength + sizeof(WCHAR)); memcpy(wName, ValueCell->Name, ValueCell->NameLength); wName[ValueCell->NameLength / sizeof(WCHAR)] = 0; } DataLength = ValueCell->DataLength & REG_DATA_SIZE_MASK; TRACE("ValueName: '%S'\n", wName); TRACE("DataLength: %u\n", DataLength); if (DataLength <= sizeof(HCELL_INDEX) && (ValueCell->DataLength & REG_DATA_IN_OFFSET)) { Error = RegSetValue(Key, wName, ValueCell->Type, (PCHAR)&ValueCell->Data, DataLength); if (Error != ERROR_SUCCESS) { ERR("RegSetValue() failed!\n"); MmHeapFree(wName); return FALSE; } } else { DataCell = (PVOID)HvGetCell(Hive, ValueCell->Data); TRACE("DataCell: %x\n", DataCell); Error = RegSetValue(Key, wName, ValueCell->Type, DataCell, DataLength); if (Error != ERROR_SUCCESS) { ERR("RegSetValue() failed!\n"); MmHeapFree(wName); return FALSE; } } MmHeapFree(wName); return TRUE; }
STDAPI AMovieSetupRegisterServer( CLSID clsServer , LPCWSTR szDescription , LPCWSTR szFileName , LPCWSTR szThreadingModel = L"Both" , LPCWSTR szServerType = L"InprocServer32" ) { // temp buffer // TCHAR achTemp[MAX_PATH]; // convert CLSID uuid to string and write // out subkey as string - CLSID\{} // OLECHAR szCLSID[CHARS_IN_GUID]; HRESULT hr = StringFromGUID2( clsServer , szCLSID , CHARS_IN_GUID ); ASSERT( SUCCEEDED(hr) ); // create key // HKEY hkey; wsprintf( achTemp, TEXT("CLSID\\%ls"), szCLSID ); LONG lreturn = RegCreateKey( HKEY_CLASSES_ROOT , (LPCTSTR)achTemp , &hkey ); if( ERROR_SUCCESS != lreturn ) { return AmHresultFromWin32(lreturn); } // set description string // wsprintf( achTemp, TEXT("%ls"), szDescription ); lreturn = RegSetValue( hkey , (LPCTSTR)NULL , REG_SZ , achTemp , sizeof(achTemp) ); if( ERROR_SUCCESS != lreturn ) { RegCloseKey( hkey ); return AmHresultFromWin32(lreturn); } // create CLSID\\{"CLSID"}\\"ServerType" key, // using key to CLSID\\{"CLSID"} passed back by // last call to RegCreateKey(). // HKEY hsubkey; wsprintf( achTemp, TEXT("%ls"), szServerType ); lreturn = RegCreateKey( hkey , achTemp , &hsubkey ); if( ERROR_SUCCESS != lreturn ) { RegCloseKey( hkey ); return AmHresultFromWin32(lreturn); } // set Server string // wsprintf( achTemp, TEXT("%ls"), szFileName ); lreturn = RegSetValue( hsubkey , (LPCTSTR)NULL , REG_SZ , (LPCTSTR)achTemp , sizeof(TCHAR) * (lstrlen(achTemp)+1) ); if( ERROR_SUCCESS != lreturn ) { RegCloseKey( hkey ); RegCloseKey( hsubkey ); return AmHresultFromWin32(lreturn); } wsprintf( achTemp, TEXT("%ls"), szThreadingModel ); lreturn = RegSetValueEx( hsubkey , TEXT("ThreadingModel") , 0L , REG_SZ , (CONST BYTE *)achTemp , sizeof(TCHAR) * (lstrlen(achTemp)+1) ); // close hkeys // RegCloseKey( hkey ); RegCloseKey( hsubkey ); // and return // return HRESULT_FROM_WIN32(lreturn); }
void RegisterExtensions(void) { TCHAR szCommandTmp[MAX_PATH]; GetModuleFileName((HMODULE)0,szCommandTmp,MAX_PATH); #ifdef TEST_REG_BUG TCHAR command[MAX_PATH]; wsprintf(command, "%s", szCommandTmp); // Wrap path & filename in quotes & null terminate TCHAR icon[MAX_PATH]; wsprintf(icon,TEXT("\"%s,1\""),(LPCTSTR)command); #endif TCHAR command[MAX_PATH]; wsprintf(command, "\"%s\"", szCommandTmp); // Wrap path & filename in quotes & null terminate TCHAR icon[MAX_PATH]; wsprintf(icon,TEXT("%s,1"),(LPCTSTR)command); _tcscat(command,TEXT(" \"%1\"")); // Append "%1" // _tcscat(command,TEXT("-d1 %1\"")); // Append "%1" // sprintf(command, "\"%s\" \"-d1 %%1\"", szCommandTmp); // Wrap path & filename in quotes & null terminate // NB. Reflect extensions in DELREG.INF // RegSetValue(HKEY_CLASSES_ROOT,".bin",REG_SZ,"DiskImage",10); // Removed as .bin is too generic long Res = RegDeleteValue(HKEY_CLASSES_ROOT, ".bin"); // TODO: This isn't working :-/ RegSetValue(HKEY_CLASSES_ROOT,".do" ,REG_SZ,"DiskImage",10); RegSetValue(HKEY_CLASSES_ROOT,".dsk",REG_SZ,"DiskImage",10); RegSetValue(HKEY_CLASSES_ROOT,".nib",REG_SZ,"DiskImage",10); RegSetValue(HKEY_CLASSES_ROOT,".po" ,REG_SZ,"DiskImage",10); // RegSetValue(HKEY_CLASSES_ROOT,".2mg",REG_SZ,"DiskImage",10); // Don't grab this, as not all .2mg images are supported (so defer to CiderPress) // RegSetValue(HKEY_CLASSES_ROOT,".2img",REG_SZ,"DiskImage",10); // Don't grab this, as not all .2mg images are supported (so defer to CiderPress) // RegSetValue(HKEY_CLASSES_ROOT,".aws",REG_SZ,"DiskImage",10); // TO DO // RegSetValue(HKEY_CLASSES_ROOT,".hdv",REG_SZ,"DiskImage",10); // TO DO RegSetValue(HKEY_CLASSES_ROOT, "DiskImage", REG_SZ,"Disk Image",21); RegSetValue(HKEY_CLASSES_ROOT, "DiskImage\\DefaultIcon", REG_SZ,icon,_tcslen(icon)+1); // This key can interfere.... // HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExt\.dsk RegSetValue(HKEY_CLASSES_ROOT, "DiskImage\\shell\\open\\command", REG_SZ,command,_tcslen(command)+1); RegSetValue(HKEY_CLASSES_ROOT, "DiskImage\\shell\\open\\ddeexec", REG_SZ,"%1",3); RegSetValue(HKEY_CLASSES_ROOT, "DiskImage\\shell\\open\\ddeexec\\application", REG_SZ,"applewin",_tcslen("applewin")+1); // REG_SZ,szCommandTmp,_tcslen(szCommandTmp)+1); RegSetValue(HKEY_CLASSES_ROOT, "DiskImage\\shell\\open\\ddeexec\\topic", REG_SZ,"system",_tcslen("system")+1); }