// htdActive.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <conio.h>
#include <Windows.h>
#include <ShlObj.h>   //复制
#pragma commet(lib,"shell32.lib")

//SDK导入激活功能 


bool setReg(const char* path,const unsigned int len);//设置注册表
//复制文件
bool copyTemplate(const char* path);
int main()
{
    
    //得到当前路径
    char path[0x100]{};
    GetModuleFileNameA(0, path, 0x100);
    int len = 0;
    for (int i = 0x100; i >0 ; i--)
    {
        if (path[i] == '\\') {
            len = i;
            path[i] = 0;
            break;
        }
    }

    //设置环境变量htd=当前目录 ,这样就可以很方便用我们的lib库文件和模板了
    if (setReg(path, len+1)) {
        std::cout << "注册表环境变量写入成功\n";

    }
    else
    {
        std::cout << "注册表环境变量写入失败\n";

    }


    //然后再把模板复制到VS的模块目录下面:C:\Users\Administrator\Documents\Visual Studio 2019\Templates\ProjectTemplates\Visual C++
    if (copyTemplate(path)) {
        std::cout << "复制模板成功\n";
        std::cout << "请重启计算机使用\n";

    }
    else
    {
        std::cout << "复制模板失败\n";

    }


    while (true)
    {

    }

}

bool setReg(const char* path, const unsigned int len) {
    const char* key{ "htd" };
    const char* subKey{ "SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment" };//注册表地址,选这个
    HKEY hKey{};
    LSTATUS lopenReg = RegOpenKeyExA(
        HKEY_LOCAL_MACHINE,
        subKey,
        0,
        KEY_SET_VALUE,
        &hKey
    );
    if (lopenReg != ERROR_SUCCESS) {
        return false;

    }
    //成功
    LSTATUS lsetReg = RegSetValueExA(hKey, key, 0, REG_SZ, (const BYTE*)path, len);
    if (lsetReg != ERROR_SUCCESS) {
        
        return false;
    }
    RegCloseKey(hKey);//记得关
    return true;

    
}

//复制文件
bool copyTemplate(const char* path) {
    CHAR ducoments[MAX_PATH];
    HRESULT result = SHGetFolderPathA(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, ducoments);
    if ( FAILED(result)) return false;
    std::string sDouc = ducoments;
    sDouc = sDouc + "\\Visual Studio 2019\\Templates\\ProjectTemplates\\Visual C++\\";//从面是我的文档
    std::string sPath = path;

    std::string consolefile = sDouc + "htdConsole.zip";
    std::string consolefile2 = sDouc + "htdmfc.zip";
    std::string consolefile3 = sDouc + "htdmfcdll.zip";


    std::string consolefileS=sPath+ "\\htdConsole.zip";
    std::string consolefileS2 = sPath + "\\htdmfc.zip";
    std::string consolefileS3 = sPath + "\\htdmfcdll.zip";


    bool b_1 = CopyFileA(consolefileS.c_str(), consolefile.c_str(), false);
    bool b_2 = CopyFileA(consolefileS2.c_str(), consolefile2.c_str(), false);
    bool b_3 = CopyFileA(consolefileS3.c_str(), consolefile3.c_str(), false);

    if (b_1 & b_2 & b_3) {
        return true;

    }
    else
    {
        return false;
    }

}

 

posted on 2021-04-17 16:01  一天的PHP之路  阅读(204)  评论(0)    收藏  举报