aboutsummaryrefslogtreecommitdiffstats
path: root/pc/dlfcn.c
blob: f2394c6368e275701110ce7e0da1f29423df6fea (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
** dlfcn.c -- limited implementation of posix dynamic loading functions
*/

/*
 * Copyright (C) 2003 the Free Software Foundation, Inc.
 * 
 * This file is part of GAWK, the GNU implementation of the
 * AWK Programming Language.
 * 
 * GAWK is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 * 
 * GAWK is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

#include <dlfcn.h>
#include <errno.h>
#include <windows.h>

/* open the library file. We currently ignore flags. */
void *dlopen(const char * libname, int flags)
{
   HMODULE libH;


   /* if libname is specified, we need to load a library of that name */
   if (libname) {
      libH = LoadLibrary(libname);
   }

   /* otherwise, we're supposed to return a handle to global symbol
    * information, which includes the executable and all libraries loaded
    * with RTLD_GLOBAL. For our purposes, it doesn't really matter, so
    * we simply return the handle to the .exe */
   else {
      libH = GetModuleHandle(NULL);
   }

   return (void *)libH;
}


/* don't need the library any more */
int dlclose(void * libH)
{
   int rc;

   if (FreeLibrary((HMODULE)libH)) {
      rc = 0;
   }
   else {
      rc = -1;
   }

   return rc;
}

/* find the symbol */
void *dlsym(void * /*restrict*/ libH, const char * /*restrict*/ fnName)
{
   return (void *)GetProcAddress((HMODULE)libH, fnName);
}

char *dlerror(void)
{
   static char errbuf[1024];

   FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(),
                 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), errbuf, sizeof(errbuf), NULL);

   return errbuf;
}