RegWrap.cpp

Go to the documentation of this file.
00001 //===========================================================================
00002 //Copyright (C) 2003, 2004, 2005 Zentaro Kavanagh
00003 //
00004 //Redistribution and use in source and binary forms, with or without
00005 //modification, are permitted provided that the following conditions
00006 //are met:
00007 //
00008 //- Redistributions of source code must retain the above copyright
00009 //  notice, this list of conditions and the following disclaimer.
00010 //
00011 //- Redistributions in binary form must reproduce the above copyright
00012 //  notice, this list of conditions and the following disclaimer in the
00013 //  documentation and/or other materials provided with the distribution.
00014 //
00015 //- Neither the name of Zentaro Kavanagh nor the names of contributors 
00016 //  may be used to endorse or promote products derived from this software 
00017 //  without specific prior written permission.
00018 //
00019 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00020 //``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00021 //LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00022 //PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE ORGANISATION OR
00023 //CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00024 //EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00025 //PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00026 //PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00027 //LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00028 //NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00029 //SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030 //===========================================================================
00031 #include "stdafx.h"
00032 #include "regwrap.h"
00033 
00034 RegWrap::RegWrap(void)
00035 {
00036 }
00037 
00038 RegWrap::~RegWrap(void)
00039 {
00040 }
00041 
00042 LONG RegWrap::addKeyVal(HKEY inHive, string inKeyName, string inValueName, string inValue) {
00043         //Open or create keyname
00044         //Add a value called ValueName with value inValue.
00045 
00046         //LONG RegCreateKeyEx(
00047         //      HKEY hKey,
00048         //      LPCTSTR lpSubKey,
00049         //      DWORD Reserved,
00050         //      LPTSTR lpClass,
00051         //      DWORD dwOptions,
00052         //      REGSAM samDesired,
00053         //      LPSECURITY_ATTRIBUTES lpSecurityAttributes,
00054         //      PHKEY phkResult,
00055         //      LPDWORD lpdwDisposition
00056         //);
00057 
00058 
00059         //LONG RegSetValueEx(
00060         //      HKEY hKey,
00061         //      LPCTSTR lpValueName,
00062         //      DWORD Reserved,
00063         //      DWORD dwType,
00064         //      const BYTE* lpData,
00065         //      DWORD cbData
00066         //);
00067 
00068 
00069         //fstream debugLog;
00070         //debugLog.open("G:\\reg.log", ios_base::out);
00071         //debugLog <<"Key = "<<inKeyName<<endl<<"ValueName = "<<inValueName<<endl<<"Value = "<<inValue<<endl;
00072         HKEY locKey;
00073         DWORD locDisp;
00074         LONG retVal = RegCreateKeyEx(   inHive,
00075                                                                         inKeyName.c_str(),
00076                                                                         NULL,
00077                                                                         NULL,
00078                                                                         REG_OPTION_NON_VOLATILE,
00079                                                                         KEY_ALL_ACCESS,
00080                                                                         NULL,
00081                                                                         &locKey,
00082                                                                         &locDisp);
00083 
00084         if (retVal != ERROR_SUCCESS) {
00085                 //debugLog<<"Create Failed"<<endl;
00086                 return retVal;
00087         }
00088 
00089         retVal = RegSetValueEx(         locKey,
00090                                                                 inValueName.c_str(),
00091                                                                 NULL,
00092                                                                 REG_SZ,
00093                                                                 (const BYTE*)inValue.c_str(),
00094                                                                 (DWORD)(inValue.length()+1));
00095 
00096         if (retVal != ERROR_SUCCESS) {
00097                 //debugLog<<"Set Value Failed"<<endl;
00098                 return retVal;
00099         }
00100         
00101         RegCloseKey(locKey);
00102 
00103         //debugLog.close();
00104         return retVal;
00105 
00106 }
00107 
00108 bool RegWrap::deleteKeyRecurse(HKEY inHive, string inKeyName, string inSubKeyToDelete) {
00109         HKEY locKey;
00110         LONG retVal;
00111 
00112         retVal = RegOpenKeyEx(  inHive,
00113                                                         inKeyName.c_str(),
00114                                                         NULL,
00115                                                         KEY_ALL_ACCESS,
00116                                                         &locKey);
00117 
00118         if (retVal != ERROR_SUCCESS) {
00119                 //debugLog<<"Key not found"<<endl;
00120                 return false;
00121         }
00122 
00123         retVal = SHDeleteKeyA(locKey, inSubKeyToDelete.c_str());
00124         RegCloseKey(locKey);
00125         return true;
00126 
00127 }
00128 
00129 
00130 
00131 bool RegWrap::removeKeyVal(HKEY inHive, string inKeyName, string inValueName) {
00132         //LONG RegDeleteValue(
00133         //      HKEY hKey,
00134         //      LPCTSTR lpValueName
00135         //);
00136 
00137         HKEY locKey;
00138         LONG retVal;
00139 
00140         retVal = RegOpenKeyEx(  inHive,
00141                                                         inKeyName.c_str(),
00142                                                         NULL,
00143                                                         KEY_ALL_ACCESS,
00144                                                         &locKey);
00145 
00146         if (retVal != ERROR_SUCCESS) {
00147                 //debugLog<<"Key not found"<<endl;
00148                 return false;
00149         }
00150 
00151         retVal = RegDeleteValue(locKey, inValueName.c_str());
00152         RegCloseKey(locKey);
00153         if (retVal != ERROR_SUCCESS) {
00154                 return false;
00155         } else {
00156                 return true;
00157         }
00158 }
00159 
00160 bool RegWrap::valueExists(HKEY inHive, string inKeyName, string inValueName) {
00161 
00162         //LONG RegQueryValueEx(
00163         //      HKEY hKey,
00164         //      LPCTSTR lpValueName,
00165         //      LPDWORD lpReserved,
00166         //      LPDWORD lpType,
00167         //      LPBYTE lpData,
00168         //      LPDWORD lpcbData
00169         //);
00170 
00171         //LONG RegOpenKeyEx(
00172         //      HKEY hKey,
00173         //      LPCTSTR lpSubKey,
00174         //      DWORD ulOptions,
00175         //      REGSAM samDesired,
00176         //      PHKEY phkResult
00177         //);
00178 
00179         //fstream debugLog;
00180         //HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MediaPlayer\Player\Extensions\Descriptions
00181         //debugLog.open("G:\\val.log", ios_base::out);
00182         HKEY locKey;
00183         LONG retVal;
00184         //debugLog<<"Querying : Key = "<<inKeyName<<endl<<"Value = "<<inValueName<<endl;
00185 
00186         retVal = RegOpenKeyEx(  inHive,
00187                                                         inKeyName.c_str(),
00188                                                         NULL,
00189                                                         KEY_ALL_ACCESS,
00190                                                         &locKey);
00191 
00192         if (retVal != ERROR_SUCCESS) {
00193                 //debugLog<<"Key not found"<<endl;
00194                 return false;
00195         }
00196 
00197         retVal = RegQueryValueEx(       locKey,
00198                                                                 inValueName.c_str(),
00199                                                                 NULL,
00200                                                                 NULL,
00201                                                                 NULL,
00202                                                                 NULL);
00203 
00204         RegCloseKey(locKey);
00205         if (retVal != ERROR_SUCCESS) {
00206                 //debugLog<<"Value not found"<<endl;
00207                 return false;
00208         } else {
00209                 //debugLog<<"Value found"<<endl;
00210                 return true;
00211         }
00212 
00213 }
00214 
00215 string RegWrap::findNextEmptyMediaPlayerDesc() {
00216         char locNum[6];
00217         string foundNum = "";
00218         for (long i = 1; i < 24; i++) {
00219                 itoa(i, (char*)&locNum, 10);
00220                 if (!RegWrap::valueExists(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\MediaPlayer\\Player\\Extensions\\Descriptions", (char*)&locNum)) {
00221                         foundNum = (char*)&locNum;
00222                         break;
00223                 }
00224 
00225         }
00226         return foundNum;
00227 }
00228 
00229 bool RegWrap::removeMediaDesc() {
00230         HKEY locKey;
00231         LONG retVal;
00232 
00233         retVal = RegOpenKeyEx(  HKEY_LOCAL_MACHINE,
00234                                                         "SOFTWARE\\illiminable\\oggcodecs",
00235                                                         NULL,
00236                                                         KEY_ALL_ACCESS,
00237                                                         &locKey);
00238 
00239         if (retVal != ERROR_SUCCESS) {
00240                 //debugLog<<"Key not found"<<endl;
00241                 return false;
00242         }
00243 
00244         DWORD locBuffSize = 16;
00245         char locBuff[16];
00246 
00247         retVal = RegQueryValueEx(       locKey,
00248                                                                 "MediaDescNum",
00249                                                                 NULL,
00250                                                                 NULL,
00251                                                                 (BYTE*)&locBuff,
00252                                                                 &locBuffSize);
00253 
00254         RegCloseKey(locKey);
00255         if (retVal != ERROR_SUCCESS) {
00256                 //debugLog<<"Value not found"<<endl;
00257                 return false;
00258         } else {
00259                 RegWrap::removeKeyVal(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\MediaPlayer\\Player\\Extensions\\Descriptions", locBuff);
00260                 RegWrap::removeKeyVal(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\MediaPlayer\\Player\\Extensions\\MUIDescriptions", locBuff);
00261                 RegWrap::removeKeyVal(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\MediaPlayer\\Player\\Extensions\\Types", locBuff);
00262                 RegWrap::removeKeyVal(HKEY_LOCAL_MACHINE, "SOFTWARE\\illiminable\\oggcodecs", "MediaDescNum");
00263                 //debugLog<<"Value found"<<endl;
00264                 return true;
00265                 
00266         }
00267 
00268 
00269 }
00270 bool RegWrap::addMediaPlayerDesc(string inDesc, string inExts) {
00271         if (!RegWrap::valueExists(HKEY_LOCAL_MACHINE, "SOFTWARE\\illiminable\\oggcodecs", "MediaDescNum")) {
00272                 string locDescNum = "";
00273                 string locFull = inDesc+" ("+inExts+")";
00274                 locDescNum = RegWrap::findNextEmptyMediaPlayerDesc();
00275                 if (locDescNum == "") {
00276                         return false;
00277                 }
00278                 RegWrap::addKeyVal(HKEY_LOCAL_MACHINE, "SOFTWARE\\illiminable\\oggcodecs", "MediaDescNum", locDescNum.c_str());
00279                 RegWrap::addKeyVal(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\MediaPlayer\\Player\\Extensions\\Descriptions", locDescNum, locFull.c_str());
00280                 RegWrap::addKeyVal(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\MediaPlayer\\Player\\Extensions\\MUIDescriptions", locDescNum, inDesc.c_str());
00281                 RegWrap::addKeyVal(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\MediaPlayer\\Player\\Extensions\\Types", locDescNum, inExts.c_str());
00282                 return true;
00283         }
00284 
00285 }

Generated on Thu Feb 16 23:48:13 2006 for oggdsf by  doxygen 1.3.9