1. 程式人生 > >登錄檔注入RegInject(dll)

登錄檔注入RegInject(dll)

// RegInject.h

#pragma once

// RegInject 對話方塊

class RegInject : public CDialogEx
{
    DECLARE_DYNAMIC(RegInject)

public:
    RegInject(CWnd* pParent = NULL);   // 標準建構函式
    virtual ~RegInject();

// 對話方塊資料
    enum { IDD = IDD_DIALOG6 };

protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支援

    DECLARE_MESSAGE_MAP()
public:
    CString m_strDllPath;
    afx_msg void OnBnClickedButton1();
    afx_msg void OnBnClickedButton2();
};

// RegInject.cpp

// RegInject.cpp : 實現檔案
//

#include "stdafx.h"
#include "MyInjectTool.h"
#include "RegInject.h"
#include "afxdialogex.h"

// RegInject 對話方塊

IMPLEMENT_DYNAMIC(RegInject, CDialogEx)

RegInject::RegInject(CWnd* pParent /*=NULL*/)
    : CDialogEx(RegInject::IDD, pParent)
    , m_strDllPath(_T(""))
{

}

RegInject::~RegInject()
{
}

void RegInject::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
    DDX_Text(pDX, IDC_EDIT1, m_strDllPath);
}

BEGIN_MESSAGE_MAP(RegInject, CDialogEx)
    ON_BN_CLICKED(IDC_BUTTON1, &RegInject::OnBnClickedButton1)
    ON_BN_CLICKED(IDC_BUTTON2, &RegInject::OnBnClickedButton2)
END_MESSAGE_MAP()

// RegInject 訊息處理程式

void RegInject::OnBnClickedButton1()
{
    // TODO:  在此新增控制元件通知處理程式程式碼
    char szFilter[] = "動態連結庫|*.dll";
    CFileDialog fileDlg(TRUE, "dll", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szFilter);
    UpdateData(TRUE);
    if (fileDlg.DoModal() == IDOK)
    {
        m_strDllPath = fileDlg.GetPathName();
    }
    UpdateData(FALSE);
}

void RegInject::OnBnClickedButton2()
{
    // TODO:  在此新增控制元件通知處理程式程式碼
    BOOL bRet = FALSE;
    HKEY hKey = NULL;
    LONG nReg;
    char szDllPath[MAX_PATH] = { 0 };

    //開啟HKEY_LOCAL_MACHINE/Software/Microsoft/WindowsNT/CurrentVersion/Windows
    nReg = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
        "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Windows",
        0,
        KEY_ALL_ACCESS,
        &hKey);

    if (nReg != ERROR_SUCCESS)
    {
        MessageBox("開啟登錄檔失敗");
        RegCloseKey(hKey);
        return;
    }

    //設定AppInit_DLLs的鍵值為我們的Dll
    nReg = RegSetValueEx(hKey,
        "AppInit_DLLs",
        0,
        REG_SZ,
        //(byte *)m_strDllPath.GetBuffer(0),
        NULL,
        m_strDllPath.GetLength()
        );
    if (nReg != ERROR_SUCCESS)
    {
        MessageBox("設定登錄檔失敗!");
        RegCloseKey(hKey);
        return;
    }

    RegCloseKey(hKey);
    return;
}