src/loadso/win32/SDL_sysloadso.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 29 May 2006 04:04:35 +0000
branchSDL-1.3
changeset 1668 4da1ee79c9af
parent 1662 782fd950bd46
permissions -rw-r--r--
more tweaking indent options
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2006 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 #ifdef SDL_LOADSO_WIN32
    25 
    26 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    27 /* System dependent library loading routines                           */
    28 
    29 #define WIN32_LEAN_AND_MEAN
    30 #include <windows.h>
    31 
    32 #include "SDL_loadso.h"
    33 
    34 void *
    35 SDL_LoadObject(const char *sofile)
    36 {
    37     void *handle = NULL;
    38     const char *loaderror = "Unknown error";
    39 
    40 #if defined(_WIN32_WCE)
    41     char errbuf[512];
    42 
    43     wchar_t *errbuf_t = SDL_malloc(512 * sizeof(wchar_t));
    44     wchar_t *sofile_t = SDL_malloc((MAX_PATH + 1) * sizeof(wchar_t));
    45 
    46     MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, sofile, -1, sofile_t,
    47                         MAX_PATH);
    48     handle = (void *) LoadLibrary(sofile_t);
    49 
    50     /* Generate an error message if all loads failed */
    51     if (handle == NULL) {
    52         FormatMessage((FORMAT_MESSAGE_IGNORE_INSERTS |
    53                        FORMAT_MESSAGE_FROM_SYSTEM),
    54                       NULL, GetLastError(),
    55                       MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
    56                       errbuf_t, SDL_arraysize(errbuf), NULL);
    57         WideCharToMultiByte(CP_ACP, 0, errbuf_t, -1, errbuf, 511, NULL, NULL);
    58         loaderror = errbuf;
    59     }
    60 
    61     SDL_free(sofile_t);
    62     SDL_free(errbuf_t);
    63 
    64 #else /*if defined(__WIN32__) */
    65     char errbuf[512];
    66 
    67     handle = (void *) LoadLibrary(sofile);
    68 
    69     /* Generate an error message if all loads failed */
    70     if (handle == NULL) {
    71         FormatMessage((FORMAT_MESSAGE_IGNORE_INSERTS |
    72                        FORMAT_MESSAGE_FROM_SYSTEM),
    73                       NULL, GetLastError(),
    74                       MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
    75                       errbuf, SDL_arraysize(errbuf), NULL);
    76         loaderror = errbuf;
    77     }
    78 #endif
    79 
    80     if (handle == NULL) {
    81         SDL_SetError("Failed loading %s: %s", sofile, loaderror);
    82     }
    83     return (handle);
    84 }
    85 
    86 void *
    87 SDL_LoadFunction(void *handle, const char *name)
    88 {
    89     void *symbol = NULL;
    90     const char *loaderror = "Unknown error";
    91 
    92 #if defined(_WIN32_WCE)
    93     char errbuf[512];
    94     int length = SDL_strlen(name);
    95 
    96     wchar_t *name_t = SDL_malloc((length + 1) * sizeof(wchar_t));
    97     wchar_t *errbuf_t = SDL_malloc(512 * sizeof(wchar_t));
    98 
    99     MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, name, -1, name_t, length);
   100 
   101     symbol = (void *) GetProcAddress((HMODULE) handle, name_t);
   102     if (symbol == NULL) {
   103         FormatMessage((FORMAT_MESSAGE_IGNORE_INSERTS |
   104                        FORMAT_MESSAGE_FROM_SYSTEM),
   105                       NULL, GetLastError(),
   106                       MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
   107                       errbuf_t, SDL_arraysize(errbuf), NULL);
   108         WideCharToMultiByte(CP_ACP, 0, errbuf_t, -1, errbuf, 511, NULL, NULL);
   109         loaderror = errbuf;
   110     }
   111 
   112     SDL_free(name_t);
   113     SDL_free(errbuf_t);
   114 
   115 #else /*if defined(WIN32) */
   116     char errbuf[512];
   117 
   118     symbol = (void *) GetProcAddress((HMODULE) handle, name);
   119     if (symbol == NULL) {
   120         FormatMessage((FORMAT_MESSAGE_IGNORE_INSERTS |
   121                        FORMAT_MESSAGE_FROM_SYSTEM),
   122                       NULL, GetLastError(),
   123                       MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
   124                       errbuf, SDL_arraysize(errbuf), NULL);
   125         loaderror = errbuf;
   126     }
   127 #endif
   128 
   129     if (symbol == NULL) {
   130         SDL_SetError("Failed loading %s: %s", name, loaderror);
   131     }
   132     return (symbol);
   133 }
   134 
   135 void
   136 SDL_UnloadObject(void *handle)
   137 {
   138     if (handle != NULL) {
   139         FreeLibrary((HMODULE) handle);
   140     }
   141 }
   142 
   143 #endif /* SDL_LOADSO_WIN32 */
   144 /* vi: set ts=4 sw=4 expandtab: */