fix vst3 bindir datadir on windows

This commit is contained in:
Stefano D'Angelo 2025-04-21 09:49:12 +02:00
parent a5727492cd
commit fb767d9bbf

View File

@ -2609,36 +2609,77 @@ EXPORT APIENTRY BOOL
DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved) { DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved) {
(void)hInstance; (void)hInstance;
(void)lpReserved; (void)lpReserved;
char * path;
if (dwReason == DLL_PROCESS_ATTACH) { if (dwReason == DLL_PROCESS_ATTACH) {
if (refs == 0) { if (refs == 0) {
int pathLength; DWORD path_length;
char path[260]; DWORD n_size;
HMODULE hm = NULL; char * new_path;
if (GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (LPCSTR) &bindir, &hm) == 0) { char * c;
TRACE("GetModuleHandle failed, error = %lu\n", GetLastError());
return 0; HMODULE hm;
if (GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (LPCSTR)&bindir, &hm) == 0) {
TRACE("GetModuleHandle failed, error = %lu\n", GetLastError());
goto err_handle;
} }
if ((pathLength = GetModuleFileName(hm, path, sizeof(path))) == 0) {
TRACE("GetModuleFileName failed, error = %lu\n", GetLastError()); path = NULL;
return 0; n_size = MAX_PATH;
while (1) {
new_path = (char *)realloc(path, n_size);
if (new_path == NULL) {
TRACE("GetModuleFileName failed, not enough memory (requested %lu bytes)\n", n_size);
goto err_realloc;
}
path = new_path;
path_length = GetModuleFileNameA(hm, path, n_size);
if (path_length == 0) {
TRACE("GetModuleFileName failed, error = %lu\n", GetLastError());
goto err_filename;
}
if (path_length < n_size)
break;
n_size *= 2;
} }
char *c = strrchr(path, '\\');
*c = '\0';
bindir = (char*) malloc(strlen(path) + 1);
memcpy(bindir, path, strlen(path));
bindir[strlen(path)] = '\0';
c = strrchr(path, '\\'); c = strrchr(path, '\\');
*c = '\0'; *c = '\0';
static char* res = "\\Resources"; bindir = _strdup(path);
datadir = malloc(strlen(path) + strlen(res) + 1); if (bindir == NULL) {
sprintf(datadir, "%s%s", path, res); TRACE("bindir _strdup failed\n");
goto err_bindir;
}
c = strrchr(path, '\\');
*c = '\0';
datadir = (char *)malloc(strlen(path) + sizeof("\\Resources") + 1);
if (datadir == NULL) {
TRACE("datadir malloc failed\n");
goto err_datadir;
}
sprintf(datadir, "%s\\Resources", path);
TRACE("bindir = %s \ndatadir = %s\n", bindir, datadir); TRACE("bindir = %s \ndatadir = %s\n", bindir, datadir);
} }
refs++; refs++;
} else if (dwReason == DLL_PROCESS_DETACH) { } else if (dwReason == DLL_PROCESS_DETACH)
vstExit(); vstExit();
}
return 1; return 1;
err_datadir:
free(bindir);
err_bindir:
err_filename:
err_realloc:
if (path != NULL)
free(path);
err_handle:
return 0;
} }
#elif defined(__APPLE__) || defined(__linux__) #elif defined(__APPLE__) || defined(__linux__)
@ -2658,9 +2699,11 @@ char ModuleEntry(void *handle) {
v.f = vstExit; v.f = vstExit;
if (dladdr((void*) v.d, &info) == 0) if (dladdr((void*) v.d, &info) == 0)
return 0; return 0;
file = realpath(info.dli_fname, NULL); file = realpath(info.dli_fname, NULL);
if (file == NULL) if (file == NULL)
return 0; return 0;
char *c = strrchr(file, '/'); char *c = strrchr(file, '/');
*c = '\0'; *c = '\0';
bindir = strdup(file); bindir = strdup(file);
@ -2668,10 +2711,13 @@ char ModuleEntry(void *handle) {
goto err_bindir; goto err_bindir;
c = strrchr(file, '/'); c = strrchr(file, '/');
*c = '\0'; *c = '\0';
datadir = x_asprintf("%s/Resources", file); datadir = x_asprintf("%s/Resources", file);
if (datadir == NULL) if (datadir == NULL)
goto err_datadir; goto err_datadir;
free(file); free(file);
TRACE("bindir = %s \ndatadir = %s\n", bindir, datadir); TRACE("bindir = %s \ndatadir = %s\n", bindir, datadir);
} }
refs++; refs++;