1
0
mirror of https://github.com/mpv-player/mpv synced 2025-03-09 15:47:44 +00:00

osdep/io: provide dlopen, etc. abstraction wrappers for windows

This commit is contained in:
chuck- 2020-06-12 10:41:48 +02:00 committed by Dudemanguy
parent 0084854f8f
commit adb118290b
2 changed files with 79 additions and 0 deletions

View File

@ -699,6 +699,76 @@ off_t mp_lseek(int fd, off_t offset, int whence)
return _lseeki64(fd, offset, whence);
}
_Thread_local
static struct {
DWORD errcode;
char *errstring;
} mp_dl_result = {
.errcode = 0,
.errstring = NULL
};
static void mp_dl_free(void)
{
if (mp_dl_result.errstring != NULL) {
talloc_free(mp_dl_result.errstring);
}
}
static void mp_dl_init(void)
{
atexit(mp_dl_free);
}
void *mp_dlopen(const char *filename, int flag)
{
wchar_t *wfilename = mp_from_utf8(NULL, filename);
HMODULE lib = LoadLibraryW(wfilename);
talloc_free(wfilename);
mp_dl_result.errcode = GetLastError();
return (void *)lib;
}
void *mp_dlsym(void *handle, const char *symbol)
{
FARPROC addr = GetProcAddress((HMODULE)handle, symbol);
mp_dl_result.errcode = GetLastError();
return (void *)addr;
}
char *mp_dlerror(void)
{
static pthread_once_t once_init_dlerror = PTHREAD_ONCE_INIT;
pthread_once(&once_init_dlerror, mp_dl_init);
mp_dl_free();
if (mp_dl_result.errcode == 0)
return NULL;
// convert error code to a string message
LPWSTR werrstring = NULL;
FormatMessageW(
FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_IGNORE_INSERTS
| FORMAT_MESSAGE_ALLOCATE_BUFFER,
NULL,
mp_dl_result.errcode,
MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL),
(LPWSTR) &werrstring,
0,
NULL);
mp_dl_result.errcode = 0;
if (werrstring) {
mp_dl_result.errstring = mp_to_utf8(NULL, werrstring);
LocalFree(werrstring);
}
return mp_dl_result.errstring == NULL
? "unknown error"
: mp_dl_result.errstring;
}
#if HAVE_UWP
void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
{

View File

@ -113,6 +113,9 @@ char *mp_getenv(const char *name);
char ***mp_penviron(void);
off_t mp_lseek(int fd, off_t offset, int whence);
void *mp_dlopen(const char *filename, int flag);
void *mp_dlsym(void *handle, const char *symbol);
char *mp_dlerror(void);
// mp_stat types. MSVCRT's dev_t and ino_t are way too short to be unique.
typedef uint64_t mp_dev_t_;
@ -172,6 +175,12 @@ void mp_globfree(mp_glob_t *pglob);
#undef lseek
#define lseek(...) mp_lseek(__VA_ARGS__)
#define RTLD_NOW 0
#define RTLD_LOCAL 0
#define dlopen(fn,fg) mp_dlopen((fn), (fg))
#define dlsym(h,s) mp_dlsym((h), (s))
#define dlerror mp_dlerror
// Affects both "stat()" and "struct stat".
#undef stat
#define stat mp_stat