即使GetLastError返回全0,我也看不到注册表项

时间:2013-05-30 05:59:41

标签: c++ windows api registry regedit

我尝试制作文件dropper并在启动时启动,但之后它正确启动但是当我使用regedit导航到所述密钥时我看不到它?怎么了?所有错误代码都返回0 .....

#include <iostream>
#include <windows.h>
#include <Shlwapi.h>
using namespace std;
string RegistryKeyName = "testdropper.exe";

int main()
{
    std::string filename ="\\";
    char system[MAX_PATH];
    char pathtofile[MAX_PATH];
    memset(system, 0, MAX_PATH);
    memset(pathtofile, 0, MAX_PATH);


    //GET MODULE HANDLE OF CALLING PROGRAM I.E SERVER.EXE'S HANDLE
    HMODULE GetModH = GetModuleHandle(NULL);

cout << GetLastError();
    //GET PATH OF exe
    GetModuleFileName(GetModH,pathtofile,sizeof(pathtofile));

    filename.append(PathFindFileNameA(pathtofile));

    //GET SYSTEM DIRECTORY LIKE SYSTEM32
    GetSystemDirectory(system,sizeof(system));

    //APPEND MY FILENAME AFTER THE SYSTEMDIRECTORY 
    strcat(system, filename.c_str());

    //COPY SERVER TO THE SYSTEM32 FOLDER
    CopyFile(pathtofile,system,false);

    //MAKE A REGISTRY KEY TO THE SYSTEM32FOLDER WITH SERVER.EXE TO RUN AT STARTUP
    HKEY hKey;

    RegOpenKeyEx(HKEY_LOCAL_MACHINE,"Software\\Microsoft\\Windows\\CurrentVersion\\Run",0,KEY_SET_VALUE,&hKey );

    RegSetValueEx(hKey, RegistryKeyName.c_str(),0,REG_SZ,(const BYTE*)system,sizeof(system));

    RegCloseKey(hKey);


     return 0;
}

2 个答案:

答案 0 :(得分:2)

  

...即使GetLastError返回全0

RegXxx API函数通常直接在返回值中返回错误代码,而不是通过GetLastError

您可能使用HKLM的常见错误是访问权限不足。

答案 1 :(得分:0)

您的代码中有两个错误:

  1. 您不会检查错误。

  2. 您没有正确编写字符串值。

  3. 试试这个:

    long ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_SET_VALUE, &hKey);
    if (ret != ERROR_SUCCESS)
    {
        cout << "Unable to open key. Error " << ret;
    }
    else
    {
        ret = RegSetValueEx(hKey, RegistryKeyName.c_str(), 0, REG_SZ, (const BYTE*) system, strlen(system)+1);
        if (ret != ERROR_SUCCESS)
            cout << "Unable to write to key. Error " << ret;
    
        RegCloseKey(hKey);
    }