-
Notifications
You must be signed in to change notification settings - Fork 5
/
Utils.h
140 lines (107 loc) · 3.67 KB
/
Utils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#pragma once
#ifndef UTILS_H
#define UTILS_H
#include <iostream>
#include <QString>
#include <QFile>
#include <atlstr.h>
#include <Windows.h>
#include <QtCore>
class Utils{
public:
static CString QS2CS(QString qs) {
return qs.toStdString().c_str();
};
static QString CS2QS(CString cs){
return QString::fromWCharArray((LPCTSTR)cs, cs.GetLength());
};
static CString itostr(int i)
{
CString str;
str.Format(_T("%d"), i);
return str;
};
};
class CADService {
public:
static CString GetAcad2017Path() {
CString strAppName("SOFTWARE\\Autodesk\\AutoCAD\\R21.0\\ACAD-0001\\Install");
std::cout << strAppName.GetString() << std::endl;
HKEY hKey;
CString strAppRegeditPath("");
TCHAR szProductType[MAX_PATH];
memset(szProductType, 0, sizeof(szProductType));
DWORD dwBuflen = MAX_PATH;
LONG lRet = 0;
// 打开注册表,只有打开后才能进行其他操作
lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, //要打开的根键
LPCTSTR(strAppName), // 要打开的子键
0, //这个一定为0
KEY_WOW64_64KEY | KEY_QUERY_VALUE,//指定打开方式为读
&hKey
);
std::cout << "lret: " << lRet << std::endl;
if (lRet != ERROR_SUCCESS)
{
printf("open error!\n");
return strAppRegeditPath;
}
else
{
// 下面开始查询
lRet = RegQueryValueEx(hKey, //打开注册表时返回的句柄
TEXT("INSTALLDIR"), //要查询的名称
NULL, NULL,
(LPBYTE)szProductType,
&dwBuflen);
}
if (lRet != ERROR_SUCCESS)
{
printf("read error!\n");
return strAppRegeditPath;
}
else
{
RegCloseKey(hKey);
strAppRegeditPath = szProductType;
}
std::cout << strAppRegeditPath.GetString() << std::endl;
return strAppRegeditPath;
};
//编写acad.rx 文件
static BOOL WriteAcadRx() {
QString filename = Utils::CS2QS(GetAcad2017Path()) + "acad.rx";
std::cout << "arax rx filename: " << filename.toStdString() << std::endl;
QFile file(filename);
if(file.open(QIODevice::ReadWrite|QIODevice::Text)){
file.write("INGExiejingcad.arx");
file.flush();
file.close();
return TRUE;
}
else return FALSE;
};
static bool RemoveAcadRx(){
QString filename = Utils::CS2QS(GetAcad2017Path()) + "acad.rx";
QFile file(filename);
bool status = file.remove();
std::cout <<"remove acad rx: " << status << std::endl;
return status;
}
// 通过创建进程的方式启动 acad.exe
static BOOL LaunchACad() {
CString OutputPath;
CString m_strCADPath = GetAcad2017Path(); //从注册表中获取 acad 2017的路径
STARTUPINFO si; //一些必备参数设置
memset(&si, 0, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_SHOWNORMAL;
PROCESS_INFORMATION pi; //必备参数设置结束
OutputPath = m_strCADPath + "acad.exe";
BOOL bRet = CreateProcess(NULL, OutputPath.GetBuffer(OutputPath.GetLength()),
NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
return bRet;
};
};
#endif // UTILS_H