add create_interface() to cdll.c.

Use it to initialize the dll interface and return a pointer to it.
This commit is contained in:
onyx-and-iris 2024-07-05 10:41:07 +01:00
parent a28db25bcc
commit 930093da7f
3 changed files with 84 additions and 84 deletions

View File

@ -3,6 +3,6 @@
#include "VoicemeeterRemote.h" #include "VoicemeeterRemote.h"
long initialize_dll_interfaces(PT_VMR vmr); PT_VMR create_interface();
#endif /*__CDLL_H__*/ #endif /*__CDLL_H__*/

View File

@ -3,63 +3,40 @@
#include <windows.h> #include <windows.h>
#include "cdll.h" #include "cdll.h"
#include "util.h" #include "util.h"
#include "log.h"
/*******************************************************************************/ static T_VBVMR_INTERFACE iVMR;
/** GET VOICEMEETER DIRECTORY **/
/*******************************************************************************/
#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 PT_VMR create_interface()
#define KEY_WOW64_32KEY 0x0200
#endif
bool __cdecl registry_get_voicemeeter_folder(char *szDir)
{ {
char szKey[256]; PT_VMR vmr = &iVMR;
char sss[1024]; int rep;
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 rep = initialize_dll_interfaces(vmr);
strcpy(szKey, uninstDirKey); if (rep < 0)
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 if (rep == -100)
rep = RegOpenKeyEx(HKEY_LOCAL_MACHINE, szKey, 0, KEY_READ | KEY_WOW64_32KEY, &hkResult); {
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 vmr;
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;
} }
/*******************************************************************************/ /*******************************************************************************/
/** GET DLL INTERFACE **/ /** GET DLL INTERFACE **/
/*******************************************************************************/ /*******************************************************************************/
long initialize_dll_interfaces(PT_VMR vmr) static long initialize_dll_interfaces(PT_VMR vmr)
{ {
HMODULE G_H_Module = NULL; HMODULE G_H_Module = NULL;
char szDllName[1024]; char szDllName[1024];
@ -168,4 +145,56 @@ long initialize_dll_interfaces(PT_VMR vmr)
return -38; return -38;
return 0; 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;
} }

View File

@ -36,12 +36,11 @@ struct result
} val; } val;
}; };
static T_VBVMR_INTERFACE iVMR; static bool vflag = false;
bool vflag = false;
void help(void); void help(void);
enum kind set_kind(char *kval); 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 interactive(PT_VMR vmr);
void parse_input(PT_VMR vmr, char *input, int len); void parse_input(PT_VMR vmr, char *input, int len);
void parse_command(PT_VMR vmr, char *command); 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) if (rep != 0)
{ {
log_fatal("Error logging into the Voicemeeter API");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -144,9 +144,14 @@ int main(int argc, char *argv[])
rep = logout(vmr); rep = logout(vmr);
if (rep == 0) if (rep == 0)
{
return EXIT_SUCCESS; return EXIT_SUCCESS;
}
else else
{
log_fatal("Error logging out of the Voicemeeter API");
return EXIT_FAILURE; 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. * @brief Continuously read lines from stdin.
* Break if 'Q' is entered on the interactive prompt. * Break if 'Q' is entered on the interactive prompt.