aboutsummaryrefslogtreecommitdiffstats
path: root/lib/src/util/windows/dlfcn.c
blob: c8173cdb0b8f8c9b56dd2375465b942f57fed153 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/* dlfcn.c */ 

#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>

static struct {
    long lasterror;
    const char *err_rutin;
} var = {
    0,
    NULL
};

void *dlopen (const char *filename, int flags)
{
    HINSTANCE hInst;

    hInst= LoadLibrary (filename);
    if (hInst==NULL) {
        var.lasterror = GetLastError ();
        var.err_rutin = "dlopen";
    }
    return hInst;
}

int dlclose (void *handle)
{
    BOOL ok;
    int rc= 0;

    ok= FreeLibrary ((HINSTANCE)handle);
    if (! ok) {
        var.lasterror = GetLastError ();
        var.err_rutin = "dlclose";
        rc= -1;
    }
    return rc;
}

void *dlsym (void *handle, const char *name)
{
    FARPROC fp;

    fp= GetProcAddress ((HINSTANCE)handle, name);
    if (!fp) {
        var.lasterror = GetLastError ();
        var.err_rutin = "dlsym";
    }
    return (void *)(intptr_t)fp;
}

const char *dlerror (void)
{
static char errstr [88];

    if (var.lasterror) {
        sprintf (errstr, "%s error #%ld", var.err_rutin, var.lasterror);
        return errstr;
    } else {
        return NULL;
    }
}