使用 C++合并 .reg 文件

发布于 2024-08-22 10:33:19 字数 779 浏览 7 评论 0原文

嘿,我只是尝试使用一个非常基本的 C++ 程序将 .reg 文件合并到我的注册表中。

代码如下:

#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;
}

我也尝试过使用这些系统命令:

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

但它们的工作效果并没有更好。 非常有趣的是,如果我转到“开始”、“运行”,然后输入“regedit.exe new.reg” 注册表将会更新;只是当我运行 .exe 程序时没有。 有什么想法吗?

Hey, I am simply trying to merge a .reg file into my registry using a very basic c++ program.

The code is as follows:

#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;
}

I've also tried using these system commands:

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

but they work no better.
The very interesting thing is that if I go to Start, Run, and type in "regedit.exe new.reg"
The registry WILL update; just not when I run the .exe program.
Any thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故事还在继续 2024-08-29 10:33:19

查看以编程方式将 .reg 文件合并到 win32 注册表中,另外使用 win32 或其他库导入 .reg 文件

请参阅 http://msdn.microsoft.com/en -us/library/ms724889%28VS.85%29.aspx 以及这可能会有所帮助。

RegLoadKey函数

在 HKEY_USERS 下创建子项或
HKEY_LOCAL_MACHINE 并加载数据
从指定的注册表配置单元进入
该子项。

备份或恢复的应用程序
系统状态包括系统文件
并且注册表配置单元应该使用
卷影复制服务代替
注册表功能。

仅供参考,以下是链接的开源项目中处理导入的代码片段:

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;
            }
        }
    }
}

Check out programmatically merge .reg file into win32 registry and additionally import .reg files using win32 or other libraries.

See http://msdn.microsoft.com/en-us/library/ms724889%28VS.85%29.aspx as well which might help.

RegLoadKey Function

Creates a subkey under HKEY_USERS or
HKEY_LOCAL_MACHINE and loads the data
from the specified registry hive into
that subkey.

Applications that back up or restore
system state including system files
and registry hives should use the
Volume Shadow Copy Service instead of
the registry functions.

Just FYI, here is the piece of code from the linked open-source project which handles imports:

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;
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文