Skip to content
Snippets Groups Projects
Commit 52668e33 authored by Constantin Papizh's avatar Constantin Papizh
Browse files

Win registry functionality

parent fbf797eb
No related branches found
No related tags found
1 merge request!12bug 2539
......@@ -90,6 +90,10 @@
#define DAP_DUP(a) ( __typeof(a) ret = memcpy(ret,a,sizeof(*a)) )
#endif
#ifndef MAX_PATH
#define MAX_PATH 120
#endif
DAP_STATIC_INLINE void *_dap_aligned_alloc( uintptr_t alignment, uintptr_t size )
{
uintptr_t ptr = (uintptr_t) DAP_MALLOC( size + (alignment * 2) + sizeof(void *) );
......
#include "registry.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] = {};
if (strlen(path) > 3) { return path; }
HKEY hKey;
const char keyPath[] = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders";
LSTATUS err = RegOpenKeyExA(HKEY_CURRENT_USER,
keyPath,
0, KEY_READ, &hKey );
if (err != ERROR_SUCCESS) { return NULL; }
DWORD len = MAX_PATH;
err = RegGetValueA(hKey, NULL, "Personal", 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;
}
#ifndef REGISTRY_H
#define REGISTRY_H
#include <stdio.h>
#include <windows.h>
#include <tchar.h>
wchar_t* readRegKey(HKEY hKey, LPCWSTR regSubKey, LPCWSTR val);
wchar_t* getTapGUID();
wchar_t* getTapName();
wchar_t* getUserSID(LPCWSTR homePath);
char* regGetUsrPath();
#endif
HEADERS += $$PWD/dap_console_manager.h \
$$PWD/dap_cpu_monitor.h \
$$PWD/dap_process_manager.h \
$$PWD/dap_process_memory.h
$$PWD/dap_process_memory.h \
$$PWD/registry.h
SOURCES += $$PWD/dap_console_manager.c \
$$PWD/dap_cpu_monitor.c \
$$PWD/dap_process_manager.c \
$$PWD/dap_process_memory.c
$$PWD/dap_process_memory.c \
$$PWD/registry.c
INCLUDEPATH += $$PWD
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment