许可优化
许可优化
产品
产品
解决方案
解决方案
服务支持
服务支持
关于
关于
软件库
当前位置:服务支持 >  软件文章 >  NX二次开发:插件的自动化部署方法

NX二次开发:插件的自动化部署方法

阅读数 3
点赞 0
article_banner

自己做好的插件,放到用户机上

   如果还手动让用户在custom_dirs.dat文件后面追加目录,就有点不专业了

   下面介绍如何自动部署插件

1、读取客户机安装的 NX  软件版本及其目录

NX会在如下注册表位置写入安装目录
SOFTWARE \Unigraphics Solutions\Installed Applications
在这里插入图片描述

   可以看到键值与版本对应关系

2、自动将本目录追加写入custom_dirs.dat

注意要先用管理权限改变此文件,使其可读写,否则会写入失败

总的示例代码放下面了

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


#include "GetRegValue.h"


using namespace std;
typedef std::vector<std::string>  StringList;

string GetExePath()
{
        char szFilePath[MAX_PATH + 1] = { 0 };
        GetModuleFileNameA(NULL, szFilePath, MAX_PATH);
        (strrchr(szFilePath, '\\'))[0] = 0; // 删除文件名,只获得路径字串//
        (strrchr(szFilePath, '\\'))[0] = 0; // 删除文件名,只获得路径字串//
        string path = szFilePath;
        return path;
}
StringList splitstr(const std::string& str, const std::string& pattern)
{
    StringList  li;
    std::string subStr;
    std::string tPattern;
    size_t      patternLen = pattern.length();
    size_t      strLen = str.length();

    for (size_t i = 0; i < str.length(); i++)
    {
        if (pattern[0] == str[i] && ((strLen - i) >= patternLen))
        {
            if (memcmp(&pattern[0], &str[i], patternLen) == 0)
            {
                i += patternLen - 1;
                if (!subStr.empty())
                {
                    li.push_back(subStr);
                    subStr.clear();
                }
            }
            else
            {
                subStr.push_back(str[i]);
            }
        }
        else
        {
            subStr.push_back(str[i]);
        }
    }

    if (!subStr.empty())
    {
        li.push_back(subStr);
    }

    return li;
}
void WriterInstallPath(string strValue) {

 
    if (strValue.empty())
    {
        std::cout << "未检测到NX安装位置,自动挂载停止\n软件安装将继续完成" << std::endl;
        std::cout << "\n输入任意键关闭窗口" << std::endl;
        std::cin.get();
        return;
    }

    StringList strlist = splitstr(strValue, "\\");
    string NXrootDirectory = "";
    for (size_t i = 0; i < strlist.size() - 2; i++)
    {
        if (i != 0)
        {
            NXrootDirectory = NXrootDirectory + "\\";
        }
        NXrootDirectory = NXrootDirectory + strlist[i];
    }
 
    string Installpath = GetExePath();
 
    string custom_dirsPath = NXrootDirectory + "\\UGII\\menus\\custom_dirs.dat";
 
    //文件属性设为一般
    SetFileAttributesA(custom_dirsPath.c_str(), FILE_ATTRIBUTE_NORMAL);
    fstream f;
    //追加写入,在原来基础上加了ios::app 
    f.open(custom_dirsPath, ios::out | ios::app);
    //输入你想写入的内容 
    if (!f.is_open())
    {
        f.close();
       

        std::cout << "尝试自动配置插件失败!请将下面文件只读属性去除" << std::endl;
        std::cout << custom_dirsPath << std::endl;
        std::cout << "" << std::endl;
        std::cin.get();
    }
    else
    {
        f << Installpath << endl;
    }

    f.close();


}

int main()
{
 

	map<string, string> NxName;
	NxName["1926"] = "Unigraphics V1926";
	NxName["1953"] = "Unigraphics V1953";
    NxName["1980"] = "Unigraphics V1980";
    NxName["12"] = "Unigraphics V30.0";


    //计算机\HKEY_LOCAL_MACHINE\SOFTWARE\Unigraphics Solutions\Installed Applications

    std::string strValue01 = GetRegValue(2, "SOFTWARE\\Unigraphics Solutions\\Installed Applications", NxName["1980"]);

    WriterInstallPath(strValue01);

    std::string strValue02 = GetRegValue(2, "SOFTWARE\\Unigraphics Solutions\\Installed Applications", NxName["1926"]);

    WriterInstallPath(strValue02);
 
}


#include <Windows.h>
#include "GetRegValue.h"
#include <fstream>

//可移植版本 wstring => string
std::string ws2s(const std::wstring& ws)
{
    std::string curLocale = setlocale(LC_ALL, "");
    const wchar_t* _Source = ws.c_str();
    size_t _Dsize = wcstombs(NULL, _Source, 0) + 1;
    char* _Dest = new char[_Dsize];
    memset(_Dest, 0, _Dsize);
    wcstombs(_Dest, _Source, _Dsize);
    std::string result = _Dest;
    delete[]_Dest;
    setlocale(LC_ALL, curLocale.c_str());
    return result;
}

//可移植版本 string => wstring
std::wstring s2ws(const std::string& s)
{
    std::string curLocale = setlocale(LC_ALL, "");
    const char* _Source = s.c_str();
    size_t _Dsize = mbstowcs(NULL, _Source, 0) + 1;
    wchar_t* _Dest = new wchar_t[_Dsize];
    wmemset(_Dest, 0, _Dsize);
    mbstowcs(_Dest, _Source, _Dsize);
    std::wstring result = _Dest;
    delete[]_Dest;
    setlocale(LC_ALL, curLocale.c_str());
    return result;
}

std::string GetRegValue(int nKeyType, const std::string& strUrl, const std::string& strKey)
{
    std::string strValue("");
    HKEY hKey = NULL;
    HKEY  hKeyResult = NULL;
    DWORD dwSize = 0;
    DWORD dwDataType = 0;
    std::wstring wstrUrl = s2ws(strUrl);
    std::wstring wstrKey = s2ws(strKey);

    switch (nKeyType)
    {
    case 0:
    {
        hKey = HKEY_CLASSES_ROOT;
        break;
    }
    case 1:
    {
        hKey = HKEY_CURRENT_USER;
        break;
    }
    case 2:
    {
        hKey = HKEY_LOCAL_MACHINE;
        break;
    }
    case 3:
    {
        hKey = HKEY_USERS;
        break;
    }
    case 4:
    {
        hKey = HKEY_PERFORMANCE_DATA;
        break;
    }
    case 5:
    {
        hKey = HKEY_CURRENT_CONFIG;
        break;
    }
    case 6:
    {
        hKey = HKEY_DYN_DATA;
        break;
    }
    case 7:
    {
        hKey = HKEY_CURRENT_USER_LOCAL_SETTINGS;
        break;
    }
    case 8:
    {
        hKey = HKEY_PERFORMANCE_TEXT;
        break;
    }
    case 9:
    {
        hKey = HKEY_PERFORMANCE_NLSTEXT;
        break;
    }
    default:
    {
        return strValue;
    }
    }

    //打开注册表
    if (ERROR_SUCCESS == ::RegOpenKeyEx(hKey, wstrUrl.c_str(), 0, KEY_QUERY_VALUE||KEY_WOW64_64KEY, &hKeyResult))
    {
        // 获取缓存的长度dwSize及类型dwDataType
        ::RegQueryValueEx(hKeyResult, wstrKey.c_str(), 0, &dwDataType, NULL, &dwSize);
        switch (dwDataType)
        {
        case REG_MULTI_SZ:
        {
            //分配内存大小
            BYTE* lpValue = new BYTE[dwSize];
            //获取注册表中指定的键所对应的值
            LONG lRet = ::RegQueryValueEx(hKeyResult, wstrKey.c_str(), 0, &dwDataType, lpValue, &dwSize);
            delete[] lpValue;
            break;
        }
        case REG_SZ:
        {
            //分配内存大小
            wchar_t* lpValue = new wchar_t[dwSize];
            memset(lpValue, 0, dwSize * sizeof(wchar_t));
            //获取注册表中指定的键所对应的值
            if (ERROR_SUCCESS == ::RegQueryValueEx(hKeyResult, wstrKey.c_str(), 0, &dwDataType, (LPBYTE)lpValue, &dwSize))
            {
                std::wstring wstrValue(lpValue);
                strValue = ws2s(wstrValue);
            }
            delete[] lpValue;
            break;
        }
        default:
            break;
        }
    }

    //关闭注册表
    ::RegCloseKey(hKeyResult);


    return strValue;
}

编译成 exe 后,双击执行就安装上了

END


免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删


相关文章
技术文档
QR Code
微信扫一扫,欢迎咨询~
customer

online

联系我们
武汉格发信息技术有限公司
湖北省武汉市经开区科技园西路6号103孵化器
电话:155-2731-8020 座机:027-59821821
邮件:tanzw@gofarlic.com
Copyright © 2023 Gofarsoft Co.,Ltd. 保留所有权利
遇到许可问题?该如何解决!?
评估许可证实际采购量? 
不清楚软件许可证使用数据? 
收到软件厂商律师函!?  
想要少购买点许可证,节省费用? 
收到软件厂商侵权通告!?  
有正版license,但许可证不够用,需要新购? 
联系方式 board-phone 155-2731-8020
close1
预留信息,一起解决您的问题
* 姓名:
* 手机:

* 公司名称:

姓名不为空

姓名不为空

姓名不为空
手机不正确

手机不正确

手机不正确
公司不为空

公司不为空

公司不为空