diff --git a/CMakeLists.txt b/CMakeLists.txt index 99146be5b03c390008cb0745b783ec163940db69..ef3d2777d3efa0918374b53b13c44f39a53b4d0f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -52,6 +52,7 @@ if(WIN32) add_executable(${PROJECT_NAME} ./CellframeNodeDiagtool/DiagnosticWorker.cpp ./CellframeNodeDiagtool/AbstractDiagnostic.cpp + ./CellframeNodeDiagtool/registry.c ./CellframeNodeDiagtool/WinDiagnostic.cpp ./CellframeNodeDiagtool/NotifyWorker.cpp ./CellframeNodeTray/NodeTrayCommandController.cpp diff --git a/CellframeNodeDiagtool/AbstractDiagnostic.cpp b/CellframeNodeDiagtool/AbstractDiagnostic.cpp index 5271bbf374a67925761dccc55a7fce7f54115897..763bc0c78f5acde0c1cfc73f4fddea7948ef916a 100644 --- a/CellframeNodeDiagtool/AbstractDiagnostic.cpp +++ b/CellframeNodeDiagtool/AbstractDiagnostic.cpp @@ -3,7 +3,7 @@ #ifdef Q_OS_LINUX #elif defined(Q_OS_WIN) - +#include "registry.h" #elif defined(Q_OS_MACOS) #include "dap_common.h" #endif diff --git a/CellframeNodeDiagtool/WinDiagnostic.h b/CellframeNodeDiagtool/WinDiagnostic.h index 4ebef846290f82ae6d92f08ddf3c430ea0c6a402..5af40f5b30d7aee026fc8d144dc401e52778ff56 100644 --- a/CellframeNodeDiagtool/WinDiagnostic.h +++ b/CellframeNodeDiagtool/WinDiagnostic.h @@ -6,7 +6,7 @@ #include <windows.h> #include <psapi.h> #include <tlhelp32.h> -//#include "registry.h" +#include "registry.h" #include <pdh.h> diff --git a/CellframeNodeDiagtool/registry.c b/CellframeNodeDiagtool/registry.c new file mode 100644 index 0000000000000000000000000000000000000000..72c8cae929e07362e823c22b44b3e0703de304d7 --- /dev/null +++ b/CellframeNodeDiagtool/registry.c @@ -0,0 +1,141 @@ +#include "registry.h" +#include <shlobj.h> + +wchar_t* readRegKey(HKEY hKey, LPCWSTR regSubKey, LPCWSTR val) { + wchar_t *wret = (wchar_t*)malloc(MAX_PATH); + DWORD dwSize = MAX_PATH; + LSTATUS err = RegGetValueW(hKey, regSubKey, val, RRF_RT_REG_SZ, NULL, (void*)wret, &dwSize); + if (err == ERROR_SUCCESS) { + return wret; + } else { + free(wret); + return NULL; + } +} + +char* regGetUsrPath() { + static char path[MAX_PATH] = {'\0'}; + if (strlen(path) > 3) { return path; } + HKEY hKey; + const char keyPath[] = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders"; + LSTATUS err = RegOpenKeyExA(HKEY_LOCAL_MACHINE, + keyPath, + 0, KEY_READ, &hKey ); + if (err != ERROR_SUCCESS) { return NULL; } + DWORD len = MAX_PATH; + err = RegGetValueA(hKey, NULL, "Common Documents", RRF_RT_REG_SZ, NULL, (void*)path, &len); + RegCloseKey(hKey); + return path; +} + +wchar_t* regWGetUsrPath() { + static wchar_t path[MAX_PATH] = {'\0'}; + if (wcslen(path) > 3) { return path; } + HKEY hKey; + const char keyPath[] = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders"; + LSTATUS err = RegOpenKeyExA(HKEY_LOCAL_MACHINE, + keyPath, + 0, KEY_READ, &hKey ); + if (err != ERROR_SUCCESS) { return NULL; } + DWORD len = MAX_PATH; + err = RegGetValueW(hKey, NULL, L"Common Documents", RRF_RT_REG_SZ, NULL, (void*)path, &len); + RegCloseKey(hKey); + return path; +} + +wchar_t* getTapGUID() { + static wchar_t guid[MAX_PATH] = {}; + if (wcslen(guid) > 2) { return guid; } + + const wchar_t keyPath[] = L"SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}"; + HKEY baseKey; + LSTATUS err = RegOpenKeyExW(HKEY_LOCAL_MACHINE, keyPath, 0 + ,KEY_ENUMERATE_SUB_KEYS | KEY_WOW64_64KEY | KEY_READ + ,&baseKey); + if (err != ERROR_SUCCESS) { return NULL; } + DWORD index; + for (index = 0; ; ++index) { + wchar_t hKey[MAX_PATH]; + DWORD len = MAX_PATH; + if (RegEnumKeyExW(baseKey, index, hKey, &len, NULL, NULL, NULL, NULL) != ERROR_SUCCESS) { + break; + } + wchar_t *tmp = readRegKey(baseKey, hKey, L"ComponentId"); + if (tmp && wcscmp(tmp, L"tap0901") == 0) { + wchar_t *tmp2 = readRegKey(baseKey, hKey, L"NetCfgInstanceId"); + wcscpy(guid, tmp2); + free(tmp); + free(tmp2); + return guid; + } + if (tmp) free(tmp); + } + return NULL; +} + +wchar_t* getTapName() { + static wchar_t name[MAX_PATH] = {}; + if (wcslen(name) > 2) return name; + + wchar_t *guid = getTapGUID(); + if (guid == NULL) return NULL; + wchar_t keyPath[MAX_PATH] = L"SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}"; + wcscat(keyPath, L"\\"); + wcscat(keyPath, guid); + + HKEY baseKey; + LSTATUS err = RegOpenKeyExW(HKEY_LOCAL_MACHINE, keyPath, 0 + ,KEY_ENUMERATE_SUB_KEYS | KEY_WOW64_64KEY | KEY_READ + ,&baseKey); + if (err != ERROR_SUCCESS) { return NULL; } + DWORD index; + for (index = 0; ; ++index) { + wchar_t hKey[MAX_PATH]; + DWORD len = MAX_PATH; + if (RegEnumKeyExW(baseKey, index, hKey, &len, NULL, NULL, NULL, NULL) != ERROR_SUCCESS) { + break; + } + wchar_t *tmp = readRegKey(baseKey, hKey, L"Name"); + if (tmp) { + wcscpy(name, tmp); + free(tmp); + return name; + } + } + return NULL; +} + +wchar_t* getUserSID(LPCWSTR homePath) { + static wchar_t sid[MAX_PATH] = {}; + if (wcslen(sid) > 2) return sid; + + const wchar_t keyPath[] = L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList"; + HKEY baseKey; + LSTATUS err = RegOpenKeyExW(HKEY_LOCAL_MACHINE, keyPath, 0 + ,KEY_ENUMERATE_SUB_KEYS | KEY_WOW64_64KEY | KEY_READ + ,&baseKey); + if (err != ERROR_SUCCESS) { return NULL; } + DWORD index; + for (index = 0; ; ++index) { + wchar_t hKey[MAX_PATH]; + DWORD len = MAX_PATH; + if (RegEnumKeyExW(baseKey, index, hKey, &len, NULL, NULL, NULL, NULL) != ERROR_SUCCESS) { + break; + } + wchar_t *tmp = readRegKey(baseKey, hKey, L"ProfileImagePath"); + if (tmp && wcscmp(tmp, homePath) == 0) { + wcscpy(sid, hKey); + free(tmp); + return sid; + } + if (tmp) free(tmp); + } + return NULL; +} + +wchar_t* shGetUsrPath(){ + static WCHAR path[MAX_PATH]; + memset(path, L'\0', MAX_PATH * sizeof(WCHAR)); + SHGetFolderPathW(NULL, CSIDL_PROFILE, NULL, 0, path); + return path; +} diff --git a/CellframeNodeDiagtool/registry.h b/CellframeNodeDiagtool/registry.h new file mode 100644 index 0000000000000000000000000000000000000000..f184223fd4ee3d49fd17d1e7749a13d42f2077b6 --- /dev/null +++ b/CellframeNodeDiagtool/registry.h @@ -0,0 +1,25 @@ +#ifndef REGISTRY_H +#define REGISTRY_H + +#include <stdio.h> +#include <windows.h> +#include <tchar.h> + +#ifdef __cplusplus +extern "C" { +#endif + +wchar_t* readRegKey(HKEY hKey, LPCWSTR regSubKey, LPCWSTR val); +wchar_t* getTapGUID(); +wchar_t* getTapName(); +wchar_t* getUserSID(LPCWSTR homePath); +wchar_t* shGetUsrPath(); + +wchar_t* regWGetUsrPath(); +char* regGetUsrPath(); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/CellframeNodeTray/NodeTrayCommandController.cpp b/CellframeNodeTray/NodeTrayCommandController.cpp index 912bd81cbb40dd23916a9c55b46fa849425a2bdc..fefbcce54c036dbf190249c29719ba7c73a58453 100644 --- a/CellframeNodeTray/NodeTrayCommandController.cpp +++ b/CellframeNodeTray/NodeTrayCommandController.cpp @@ -59,11 +59,11 @@ bool NodeTrayCommandController::initConfTool() showMessage("Could not find config path"); } - QString path; + QString pathConftool; #if defined(Q_OS_LINUX) && !defined(Q_OS_ANDROID) - path = "/opt/cellframe-node/bin/cellframe-node-config"; + pathConftool = "/opt/cellframe-node/bin/cellframe-node-config"; #elif defined (Q_OS_MACOS) - path = "/Applications/CellframeNode.app/Contents/MacOS/cellframe-node-config"; + pathConftool = "/Applications/CellframeNode.app/Contents/MacOS/cellframe-node-config"; #elif defined (Q_OS_WIN) //HKLM "Software\${APP_NAME}" "Path" HKEY hKey; @@ -74,10 +74,10 @@ bool NodeTrayCommandController::initConfTool() GetStringRegKey(hKey, L"Path", path, L""); std::string stdpath(path.begin(),path.end()); - path = QString(QString::fromWCharArray(path.c_str()) + "/cellframe-node-config.exe"); + pathConftool = QString(QString::fromWCharArray(path.c_str()) + "/cellframe-node-config.exe"); #endif - QFileInfo confTool(path); + QFileInfo confTool(pathConftool); if(!confTool.exists()) { qWarning()<<"Could not find cellframe-node-config"; diff --git a/CellframeNodeTray/NodeTrayCommandController.h b/CellframeNodeTray/NodeTrayCommandController.h index 482a63a97be7685101403b0dff92aaae9dc78823..a307da6c61b2eab5b2dfe101d5ac6c64f36dee00 100644 --- a/CellframeNodeTray/NodeTrayCommandController.h +++ b/CellframeNodeTray/NodeTrayCommandController.h @@ -17,7 +17,7 @@ #include "DiagnosticWorker.h" #ifdef WIN32 -#include <windows.h> +#include "registry.h" #endif class NodeTrayCommandController : public QObject diff --git a/CellframeNodeTray/main.qml b/CellframeNodeTray/main.qml index 3e02d5ed888275e82c9e34057eb51f7819f2e2d0..46d7af718cd44f1d7fae2471a2a0415a9c84ab70 100644 --- a/CellframeNodeTray/main.qml +++ b/CellframeNodeTray/main.qml @@ -57,17 +57,17 @@ ApplicationWindow { } } MenuItem { - visible: CURRENT_OS === "macos" ? false : autostartItemMenu.status + visible: CURRENT_OS === "win" ? true : CURRENT_OS === "macos" ? false : autostartItemMenu.status text: qsTr("Stop cellframe-node") onTriggered: trayCommandController.serviceCommand(3) } MenuItem { - visible: CURRENT_OS === "macos" ? false : autostartItemMenu.status + visible: CURRENT_OS === "win" ? true : CURRENT_OS === "macos" ? false : autostartItemMenu.status text: qsTr("Start cellframe-node") onTriggered: trayCommandController.serviceCommand(4) } MenuItem { - visible: CURRENT_OS === "macos" ? false : autostartItemMenu.status + visible: CURRENT_OS === "win" ? true : CURRENT_OS === "macos" ? false : autostartItemMenu.status text: qsTr("Restart cellframe-node") onTriggered: trayCommandController.serviceCommand(5) } diff --git a/cellfram-node-diagtool.pro b/cellfram-node-diagtool.pro index 50cd47c3852fec4360caa239cfaf0bdc5cab8ff9..9665ba334832678d90c4628c3c2ce38e505eccbd 100644 --- a/cellfram-node-diagtool.pro +++ b/cellfram-node-diagtool.pro @@ -24,8 +24,10 @@ HEADERS += \ win32 { DEFINES += CLI_PATH=\\\"cellframe-node-cli.exe\\\" - HEADERS += CellframeNodeDiagtool/WinDiagnostic.h - SOURCES += CellframeNodeDiagtool/WinDiagnostic.cpp + HEADERS += CellframeNodeDiagtool/WinDiagnostic.h \ + CellframeNodeDiagtool/registry.h + SOURCES += CellframeNodeDiagtool/WinDiagnostic.cpp \ + CellframeNodeDiagtool/registry.c \ } mac {