2010-02-23 10 views
2

Hey, j'essaie simplement de fusionner un fichier .reg dans mon registre en utilisant un programme C++ très basique.Utilisation de C++ pour fusionner des fichiers .reg

Le code est le suivant:

#include <iostream> 
#include <iomanip> 
#include <string> 
#include <fstream> 
#include <stdlib.h> 
#include <cstdlib> 
#include <stdio.h> 
#include <windows.h> 

using namespace std; 

int main() 
{ 
    string file = "regedit.exe new.reg"; 
    const char* ctv = file.c_str(); 

    system(ctv); 

    system("PAUSE"); 

    return 0; 
} 

J'ai aussi essayé d'utiliser ces commandes système:

ShellExecute(GetDesktopWindow(), "open", "new.reg", NULL, NULL, SW_SHOWNORMAL); 
system("reg import new.reg"); 
system("regedit/s new.reg"); 
system("new.reg"); 

mais ils travaillent pas mieux. La chose très intéressante est que si je vais à Démarrer, Exécuter, et tapez "regedit.exe new.reg" Le registre va mettre à jour; juste pas quand je cours le programme .exe. Des pensées?

+0

double: http://stackoverflow.com/questions/2300524/using-c- to-import-registry-files – dmeister

+0

Cela nécessite des autorisations élevées dans n'importe quelle version moderne de Windows. Ajoutez le manifeste requis à votre programme. –

Répondre

0

Découvrez programmatically merge .reg file into win32 registry et en plus import .reg files using win32 or other libraries.

Voir aussi http://msdn.microsoft.com/en-us/library/ms724889%28VS.85%29.aspx qui pourrait aider.

RegLoadKey Fonction

Crée une sous-clé sous HKEY_USERS ou HKEY_LOCAL_MACHINE et charge les données de la ruche de registre spécifiée dans cette sous-clé.

applications qui sauvegarde et la restauration état du système, y compris les fichiers système et les ruches de registre doit utiliser le service de cliché instantané du volume au lieu de les fonctions de registre.

Juste Pour votre information, voici le morceau de code du projet open-source liée qui gère les importations:

void CMainFrame::ImportRegistryFiles(CString csFileName) 
{ 
    CStdioFileEx sfRegFile; 
    TRY 
    { 
     sfRegFile.Open(LPCTSTR(csFileName),CFile::modeRead | CFile::typeText); 
    } 
    CATCH(CFileException, e) 
    { 
     CString csError = _T(""); 
     csError.Format(_T("File could not be opened: %s"), e->m_cause); 
     MessageBox(csError,_T("Import Error"), MB_OK|MB_ICONERROR); 
     return; 
    } 
    END_CATCH 

    CNtRegistry ntReg; 
    ntReg.InitNtRegistry(); 

    // 
    DWORD dwRegType = 0; 
    int nDataStarts = 0; 

    CTokenEx tok; 
    UCHAR ucData[8192]; 

    CString csValueName = _T(""); 
    CString csFullKey = _T(""); 
    CString csData = _T(""); 

    BOOL bNextLine = FALSE; 
    BOOL bKeyFound = FALSE; 
    BOOL bHiddenKey = FALSE; 

    CString csLine = _T(""); 
    while (sfRegFile.ReadString(csLine)) { 
     // 
     if (csLine.Left(3) == _T("[HK")) { 
      // 
      csLine.TrimLeft("["); 
      csLine.TrimRight("]"); 

      CStringArray csaKeyPath; 
      CString csFullPath = GetRegistryPath(csLine,csaKeyPath); 

      if (csFullPath.Right(1) == "*") { 
       // User wants to create a "Hidden" Key ... 
       bHiddenKey = TRUE; 
      } 

      if (!ntReg.KeyExists(csFullPath)) { 
       // 
       csFullKey = csaKeyPath.GetAt(0); 
       for (int n=1; n<csaKeyPath.GetSize(); n++) { 
        // 
        csFullKey += _T("\\"); 
        csFullKey += csaKeyPath.GetAt(n); 
        if (n == (csaKeyPath.GetSize()-1) && csFullKey.Right(1) == "*") { 
         CString csTmp = csFullKey; 
         csFullKey = csTmp.Left(csTmp.GetLength()-1); 
         if (!ntReg.CreateHiddenKey(csFullKey)) { 
          // 
          sfRegFile.Close(); 
          return; 
         } 
        } 
        else if (!ntReg.SetKey(csFullKey,TRUE,TRUE)) { 
         // 
         sfRegFile.Close(); 
         return; 
        } 
        theApp.m_clsTV->TraverseTree(csFullKey); 
       } 
      } 
      else { 
       // 
       if (!ntReg.SetKey(csFullPath,TRUE,TRUE)) { 
        // 
        sfRegFile.Close(); 
        return; 
       } 
       theApp.m_clsTV->TraverseTree(csFullKey); 
      } 
      bKeyFound = TRUE; 
      nDataStarts = 0; 
      dwRegType = REG_NONE; 
      csData = _T(""); 
     } 
     else if ((csLine.Left(2) == _T("@=") || 
       csLine.Left(1) == _T("=") || 
       csLine.Left(1) == _T("\"")) && 
       bKeyFound) { 
      // 
      memset(ucData,0,8192); 
      dwRegType = BreakdownLineInfo(csLine, csValueName, nDataStarts); 

#if _MSC_VER >= 1400 
      csLine.Trim();  // _VC80_ 
#else 
      csLine.TrimLeft(); 
      csLine.TrimRight(); 
#endif 
      csData = csLine.Mid(nDataStarts); 
      if (csLine.Right(1) == _T("\\")) { 
       bNextLine = TRUE; 
       csData.TrimRight(_T("\\")); 
      } 
      else { 
       // SetValue in Registry 
       bNextLine = FALSE; 
#if _MSC_VER >= 1400 
       csData.Trim();  // _VC80_ 
#else 
       csData.TrimLeft(); 
       csData.TrimRight(); 
#endif 
       csData.TrimRight(_T("\"")); 

       BOOL bError = FALSE; 
       switch (dwRegType) { 
        case REG_SZ: 
         bError = ntReg.WriteString(csFullKey,csValueName,csData); 
         break; 

        case REG_EXPAND_SZ: 
         // 
         { 
          CString csNewData = _T(""); 
          CString csTmpData = _T(""); 
          int nCtr = 0; 
          tok.Split(csData,_T(",")); 
          for (int n=0; n<tok.m_csaAddIt.GetSize(); n++) { 
           int nDec = theApp.Hex2Dec(tok.m_csaAddIt.GetAt(n)); 
           if (nDec != 0) { 
            csNewData.Format(_T("%c"),nDec); 
            csNewData += csTmpData; 
           } 
          } 
          bError = ntReg.WriteExpandString(csFullKey,csValueName,csNewData); 
         } 
         break; 

        case REG_DWORD: 
        case REG_DWORD_BIG_ENDIAN: 
         { 
          DWORD dwData = theApp.Hex2Dec(csData); 
          bError = ntReg.WriteDword(csFullKey,csValueName,dwData); 
         } 
         break; 

        case REG_MULTI_SZ: 
         // 
         { 
          CStringArray csaData; 
          CString csNewData = _T(""); 
          CString csTmpData = _T(""); 
          int nCtr = 0; 
          tok.Split(csData,_T(",")); 
          for (int n=0; n<tok.m_csaAddIt.GetSize(); n++) { 
           int nDec = theApp.Hex2Dec(tok.m_csaAddIt.GetAt(n)); 
           if (nDec != 0) { 
            csNewData.Format(_T("%c"),nDec); 
            csNewData += csTmpData; 
           } 
           else { 
            if ((n+1) < tok.m_csaAddIt.GetSize()) { 
             int nDec2 = theApp.Hex2Dec(tok.m_csaAddIt.GetAt(n+1)); 
             if (nDec2 == 0 && csNewData != _T("")) { 
              csaData.Add(csNewData); 
              csNewData = _T(""); 
             } 
            } 
           } 
          } 
          bError = ntReg.WriteMultiString(csFullKey,csValueName,csaData); 
         } 
         break; 

        case REG_BINARY: 
        case REG_LINK: 
        case REG_RESOURCE_LIST: 
        case REG_FULL_RESOURCE_DESCRIPTOR: 
        case REG_RESOURCE_REQUIREMENTS_LIST: 
        case REG_QWORD: 
         // 
         { 
          int nCtr = 0; 
          tok.Split(csData,_T(",")); 
          for (int n=0; n<tok.m_csaAddIt.GetSize(); n++) { 
           int nDec = theApp.Hex2Dec(tok.m_csaAddIt.GetAt(n)); 
           ucData[nCtr++] = nDec; 
          } 
          UINT uiLength = (UINT)nCtr+1; 
          bError = ntReg.WriteValue(csFullKey, csValueName, ucData, (ULONG)uiLength, dwRegType); 
         } 
         break; 
       } 
      } 
     } 
     else { 
      // 
      memset(ucData,0,8192); 
      if (bNextLine) { 
       // 
#if _MSC_VER >= 1400 
       csLine.Trim();  // _VC80_ 
#else 
       csLine.TrimLeft(); 
       csLine.TrimRight(); 
#endif 
       if (csLine.Right(1) != _T("\\")) { 
        // 
        bNextLine = FALSE; 
#if _MSC_VER >= 1400 
        csData.Trim();  // _VC80_ 
#else 
        csData.TrimLeft(); 
        csData.TrimRight(); 
#endif 
        csData.TrimRight(_T("\"")); 

        BOOL bError = FALSE; 
        csData += csLine; 

        // SetValue in Registry 
        switch (dwRegType) { 
         case REG_SZ: 
          bError = ntReg.WriteString(csFullKey,csValueName,csData); 
          break; 

         case REG_EXPAND_SZ: 
          // 
          { 
           CString csNewData = _T(""); 
           CString csTmpData = _T(""); 
           int nCtr = 0; 
           tok.Split(csData,_T(",")); 
           for (int n=0; n<tok.m_csaAddIt.GetSize(); n++) { 
            int nDec = theApp.Hex2Dec(tok.m_csaAddIt.GetAt(n)); 
            if (nDec != 0) { 
             csTmpData.Format(_T("%c"),nDec); 
             csNewData += csTmpData; 
            } 
           } 
           bError = ntReg.WriteExpandString(csFullKey,csValueName,csNewData); 
          } 
          break; 

         case REG_DWORD: 
         case REG_DWORD_BIG_ENDIAN: 
          { 
           DWORD dwData = theApp.Hex2Dec(csData); 
           bError = ntReg.WriteDword(csFullKey,csValueName,dwData); 
          } 
          break; 

         case REG_MULTI_SZ: 
          // 
          { 
           CStringArray csaData; 
           CString csNewData = _T(""); 
           CString csTmpData = _T(""); 
           int nCtr = 0; 
           tok.Split(csData,_T(",")); 
           for (int n=0; n<tok.m_csaAddIt.GetSize(); n++) { 
            int nDec = theApp.Hex2Dec(tok.m_csaAddIt.GetAt(n)); 
            if (nDec != 0) { 
             csTmpData.Format(_T("%c"),nDec); 
             csNewData += csTmpData; 
            } 
            else { 
             if ((n+1) < tok.m_csaAddIt.GetSize()) { 
              int nDec2 = theApp.Hex2Dec(tok.m_csaAddIt.GetAt(n+1)); 
              if (nDec2 == 0 && csNewData != _T("")) { 
               csaData.Add(csNewData); 
               csNewData = _T(""); 
              } 
             } 
            } 
           } 
           bError = ntReg.WriteMultiString(csFullKey,csValueName,csaData); 
          } 
          break; 

         case REG_BINARY: 
         case REG_LINK: 
         case REG_RESOURCE_LIST: 
         case REG_FULL_RESOURCE_DESCRIPTOR: 
         case REG_RESOURCE_REQUIREMENTS_LIST: 
         case REG_QWORD: 
          // 
          { 
           int nCtr = 0; 
           tok.Split(csData,_T(",")); 
           for (int n=0; n<tok.m_csaAddIt.GetSize(); n++) { 
            int nDec = theApp.Hex2Dec(tok.m_csaAddIt.GetAt(n)); 
            ucData[nCtr++] = nDec; 
           } 
           UINT uiLength = (UINT)nCtr+1; 
           bError = ntReg.WriteValue(csFullKey, csValueName, ucData, (ULONG)uiLength, dwRegType); 
          } 
          break; 
        } 
       } 
       else { 
        csData += csLine; 
#if _MSC_VER >= 1400 
        csLine.Trim();  // _VC80_ 
#else 
        csLine.TrimLeft(); 
        csLine.TrimRight(); 
#endif 
        csData.TrimRight(_T("\\")); 
       } 
      } 
      else { 
       bKeyFound = FALSE; 
      } 
     } 
    } 
} 
+0

Merci pour les liens, mais aucun d'eux n'a vraiment donné beaucoup d'aide; les premières fonctions API de détails, en mentionnant 'regedit.exe "mytest.reg"', que j'ai déjà testé, et la seconde seulement vraiment fourni des suggestions pour googling .. que j'ai fait. –

+0

Avez-vous regardé le lien API MSDNAA? Ce serait bien s'ils offraient un exemple concret là aussi. Ce que je pourrais suggérer est de trouver un éditeur de reg open-source et d'en extraire le code. http://www.codeproject.com/KB/applications/NtRegEdit.aspx –

+0

J'ai trouvé un moyen beaucoup plus simple de le faire via VB. Merci pour votre aide, cependant. –