src/loadso/win32/SDL_sysloadso.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 07 May 2009 12:03:51 +0000
branchSDL-1.2
changeset 4178 2dc6c9f343c8
parent 4159 a1b03ba2fcd0
child 6137 4720145f848b
permissions -rw-r--r--
Fixed dynamic loading on Windows CE
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2009 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 *SDL_LoadObject(const char *sofile)
    35 {
    36 	void *handle = NULL;
    37 	const char *loaderror = "Unknown error";
    38 
    39 #if defined(_WIN32_WCE)
    40 	char errbuf[512];
    41 
    42 	wchar_t *errbuf_t = SDL_malloc(512 * sizeof(wchar_t));
    43 	wchar_t *sofile_t = SDL_malloc((MAX_PATH+1) * sizeof(wchar_t));
    44 
    45 	MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, sofile, -1, sofile_t, MAX_PATH);
    46 	handle = (void *)LoadLibrary(sofile_t);
    47 
    48 	/* Generate an error message if all loads failed */
    49 	if ( handle == NULL ) {
    50 		FormatMessage((FORMAT_MESSAGE_IGNORE_INSERTS |
    51 					FORMAT_MESSAGE_FROM_SYSTEM),
    52 				NULL, GetLastError(), 
    53 				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
    54 				errbuf_t, SDL_arraysize(errbuf), NULL);
    55 		WideCharToMultiByte(CP_ACP, 0, errbuf_t, -1, errbuf, 511, NULL, NULL);
    56 		loaderror = errbuf;
    57 	}
    58 
    59 	SDL_free(sofile_t);
    60 	SDL_free(errbuf_t);
    61 
    62 #else /*if defined(__WIN32__)*/
    63 	char errbuf[512];
    64 
    65 	handle = (void *)LoadLibrary(sofile);
    66 
    67 	/* Generate an error message if all loads failed */
    68 	if ( handle == NULL ) {
    69 		FormatMessage((FORMAT_MESSAGE_IGNORE_INSERTS |
    70 					FORMAT_MESSAGE_FROM_SYSTEM),
    71 				NULL, GetLastError(), 
    72 				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
    73 				errbuf, SDL_arraysize(errbuf), NULL);
    74 		loaderror = errbuf;
    75 	}
    76 #endif
    77 
    78 	if ( handle == NULL ) {
    79 		SDL_SetError("Failed loading %s: %s", sofile, loaderror);
    80 	}
    81 	return(handle);
    82 }
    83 
    84 void *SDL_LoadFunction(void *handle, const char *name)
    85 {
    86 	void *symbol = NULL;
    87 	const char *loaderror = "Unknown error";
    88 
    89 #if defined(_WIN32_WCE)
    90 	char errbuf[512];
    91 	int length = SDL_strlen(name);
    92 
    93 	wchar_t *name_t = SDL_malloc((length + 1) * sizeof(wchar_t));
    94 	wchar_t *errbuf_t = SDL_malloc(512 * sizeof(wchar_t));
    95 
    96 	MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, name, -1, name_t, length+1);
    97 
    98 	symbol = (void *)GetProcAddress((HMODULE)handle, name_t);
    99 	if ( symbol == NULL ) {
   100 		FormatMessage((FORMAT_MESSAGE_IGNORE_INSERTS |
   101 					FORMAT_MESSAGE_FROM_SYSTEM),
   102 				NULL, GetLastError(), 
   103 				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
   104 				errbuf_t, SDL_arraysize(errbuf), NULL);
   105 		WideCharToMultiByte(CP_ACP, 0, errbuf_t, -1, errbuf, 511, NULL, NULL);
   106 		loaderror = errbuf;
   107 	}
   108 
   109 	SDL_free(name_t);
   110 	SDL_free(errbuf_t);
   111 
   112 #else /*if defined(WIN32)*/
   113 	char errbuf[512];
   114 
   115 	symbol = (void *)GetProcAddress((HMODULE)handle, name);
   116 	if ( symbol == NULL ) {
   117 		FormatMessage((FORMAT_MESSAGE_IGNORE_INSERTS |
   118 					FORMAT_MESSAGE_FROM_SYSTEM),
   119 				NULL, GetLastError(), 
   120 				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
   121 				errbuf, SDL_arraysize(errbuf), NULL);
   122 		loaderror = errbuf;
   123 	}
   124 #endif
   125 
   126 	if ( symbol == NULL ) {
   127 		SDL_SetError("Failed loading %s: %s", name, loaderror);
   128 	}
   129 	return(symbol);
   130 }
   131 
   132 void SDL_UnloadObject(void *handle)
   133 {
   134 	if ( handle != NULL ) {
   135 		FreeLibrary((HMODULE)handle);
   136 	}
   137 }
   138 
   139 #endif /* SDL_LOADSO_WIN32 */