From 930093da7f8d9cf7c026af058f2171f09539732c Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Fri, 5 Jul 2024 10:41:07 +0100 Subject: [PATCH] add create_interface() to cdll.c. Use it to initialize the dll interface and return a pointer to it. --- include/cdll.h | 2 +- src/cdll.c | 117 ++++++++++++++++++++++++++++++------------------- src/vmrcli.c | 49 +++++---------------- 3 files changed, 84 insertions(+), 84 deletions(-) diff --git a/include/cdll.h b/include/cdll.h index dd7ccf3..591c786 100644 --- a/include/cdll.h +++ b/include/cdll.h @@ -3,6 +3,6 @@ #include "VoicemeeterRemote.h" -long initialize_dll_interfaces(PT_VMR vmr); +PT_VMR create_interface(); #endif /*__CDLL_H__*/ \ No newline at end of file diff --git a/src/cdll.c b/src/cdll.c index c667c11..ee4b954 100644 --- a/src/cdll.c +++ b/src/cdll.c @@ -3,63 +3,40 @@ #include #include "cdll.h" #include "util.h" +#include "log.h" -/*******************************************************************************/ -/** GET VOICEMEETER DIRECTORY **/ -/*******************************************************************************/ +static T_VBVMR_INTERFACE iVMR; -#define INSTALLER_UNINST_KEY "VB:Voicemeeter {17359A74-1236-5467}" +static long initialize_dll_interfaces(PT_VMR vmr); +static bool registry_get_voicemeeter_folder(char *szDir); -#ifndef KEY_WOW64_32KEY -#define KEY_WOW64_32KEY 0x0200 -#endif - -bool __cdecl registry_get_voicemeeter_folder(char *szDir) +PT_VMR create_interface() { - char szKey[256]; - char sss[1024]; - DWORD nnsize = 1024; - HKEY hkResult; - LONG rep; - DWORD pptype = REG_SZ; - sss[0] = 0; - const char uninstDirKey[] = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall"; + PT_VMR vmr = &iVMR; + int rep; - // build Voicemeeter uninstallation key - strcpy(szKey, uninstDirKey); - strcat(szKey, "\\"); - strcat(szKey, INSTALLER_UNINST_KEY); - - // open key - rep = RegOpenKeyEx(HKEY_LOCAL_MACHINE, szKey, 0, KEY_READ, &hkResult); - if (rep != ERROR_SUCCESS) + rep = initialize_dll_interfaces(vmr); + if (rep < 0) { - // if not present we consider running in 64bit mode and force to read 32bit registry - rep = RegOpenKeyEx(HKEY_LOCAL_MACHINE, szKey, 0, KEY_READ | KEY_WOW64_32KEY, &hkResult); + if (rep == -100) + { + log_fatal("Voicemeeter is not installed"); + exit(EXIT_FAILURE); + } + else + { + log_fatal("Error loading Voicemeeter dll with code %d\n", rep); + exit(EXIT_FAILURE); + } } - if (rep != ERROR_SUCCESS) - return false; - // read uninstall path from registry - rep = RegQueryValueEx(hkResult, "UninstallString", 0, &pptype, (unsigned char *)sss, &nnsize); - RegCloseKey(hkResult); - if (pptype != REG_SZ) - return false; - if (rep != ERROR_SUCCESS) - return false; - // remove name to get the path only - remove_name_in_path(sss); - if (nnsize > 512) - nnsize = 512; - strncpy(szDir, sss, nnsize); - - return true; + return vmr; } /*******************************************************************************/ /** GET DLL INTERFACE **/ /*******************************************************************************/ -long initialize_dll_interfaces(PT_VMR vmr) +static long initialize_dll_interfaces(PT_VMR vmr) { HMODULE G_H_Module = NULL; char szDllName[1024]; @@ -168,4 +145,56 @@ long initialize_dll_interfaces(PT_VMR vmr) return -38; return 0; +} + +/*******************************************************************************/ +/** GET VOICEMEETER DIRECTORY **/ +/*******************************************************************************/ + +#define INSTALLER_UNINST_KEY "VB:Voicemeeter {17359A74-1236-5467}" + +#ifndef KEY_WOW64_32KEY +#define KEY_WOW64_32KEY 0x0200 +#endif + +static bool registry_get_voicemeeter_folder(char *szDir) +{ + char szKey[256]; + char sss[1024]; + DWORD nnsize = 1024; + HKEY hkResult; + LONG rep; + DWORD pptype = REG_SZ; + sss[0] = 0; + const char uninstDirKey[] = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall"; + + // build Voicemeeter uninstallation key + strcpy(szKey, uninstDirKey); + strcat(szKey, "\\"); + strcat(szKey, INSTALLER_UNINST_KEY); + + // open key + rep = RegOpenKeyEx(HKEY_LOCAL_MACHINE, szKey, 0, KEY_READ, &hkResult); + if (rep != ERROR_SUCCESS) + { + // if not present we consider running in 64bit mode and force to read 32bit registry + rep = RegOpenKeyEx(HKEY_LOCAL_MACHINE, szKey, 0, KEY_READ | KEY_WOW64_32KEY, &hkResult); + } + if (rep != ERROR_SUCCESS) + return false; + // read uninstall path from registry + rep = RegQueryValueEx(hkResult, "UninstallString", 0, &pptype, (unsigned char *)sss, &nnsize); + RegCloseKey(hkResult); + + if (pptype != REG_SZ) + return false; + if (rep != ERROR_SUCCESS) + return false; + // remove name to get the path only + remove_name_in_path(sss); + if (nnsize > 512) + nnsize = 512; + strncpy(szDir, sss, nnsize); + + return true; } \ No newline at end of file diff --git a/src/vmrcli.c b/src/vmrcli.c index 8e6715a..4ac787d 100644 --- a/src/vmrcli.c +++ b/src/vmrcli.c @@ -36,12 +36,11 @@ struct result } val; }; -static T_VBVMR_INTERFACE iVMR; -bool vflag = false; +static bool vflag = false; void help(void); enum kind set_kind(char *kval); -int init_voicemeeter(PT_VMR vmr, enum kind kind); +int init_voicemeeter(enum kind kind); void interactive(PT_VMR vmr); void parse_input(PT_VMR vmr, char *input, int len); void parse_command(PT_VMR vmr, char *command); @@ -109,11 +108,12 @@ int main(int argc, char *argv[]) } } - PT_VMR vmr = &iVMR; + PT_VMR vmr = create_interface(); - int rep = init_voicemeeter(vmr, kind); + int rep = login(vmr, kind); if (rep != 0) { + log_fatal("Error logging into the Voicemeeter API"); exit(EXIT_FAILURE); } @@ -144,9 +144,14 @@ int main(int argc, char *argv[]) rep = logout(vmr); if (rep == 0) + { return EXIT_SUCCESS; + } else + { + log_fatal("Error logging out of the Voicemeeter API"); return EXIT_FAILURE; + } } /** @@ -202,40 +207,6 @@ enum kind set_kind(char *kval) } } -/** - * @brief Defines the DLL interface as a struct. - * Logs into the API. - * - * @param vmr The API interface as a struct - * @param kind - * @return int - */ -int init_voicemeeter(PT_VMR vmr, enum kind kind) -{ - int rep = initialize_dll_interfaces(vmr); - if (rep < 0) - { - if (rep == -100) - { - log_fatal("Voicemeeter is not installed"); - } - else - { - log_fatal("Error loading Voicemeeter dll with code %d\n", rep); - } - return rep; - } - - rep = login(vmr, kind); - if (rep != 0) - { - log_fatal("Error logging into Voicemeeter"); - return rep; - } - - return 0; -} - /** * @brief Continuously read lines from stdin. * Break if 'Q' is entered on the interactive prompt.