src/loadso/windows/SDL_loadso.c
changeset 1173 e9cf8c1b4590
child 1312 c9b51268668f
equal deleted inserted replaced
1172:f69f4d25fb20 1173:e9cf8c1b4590
       
     1 /*
       
     2     SDL - Simple DirectMedia Layer
       
     3     Copyright (C) 1997-2004 Sam Lantinga
       
     4 
       
     5     This library is free software; you can redistribute it and/or
       
     6     modify it under the terms of the GNU Library General Public
       
     7     License as published by the Free Software Foundation; either
       
     8     version 2 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     Library General Public License for more details.
       
    14 
       
    15     You should have received a copy of the GNU Library General Public
       
    16     License along with this library; if not, write to the Free
       
    17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    18 
       
    19     Sam Lantinga
       
    20     slouken@libsdl.org
       
    21 */
       
    22 
       
    23 #ifdef SAVE_RCSID
       
    24 static char rcsid =
       
    25  "@(#) $Id$";
       
    26 #endif
       
    27 
       
    28 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
       
    29 /* System dependent library loading routines                           */
       
    30 
       
    31 #if !SDL_INTERNAL_BUILDING_LOADSO
       
    32 #error Do not compile directly...compile src/SDL_loadso.c instead!
       
    33 #endif
       
    34 
       
    35 #if !defined(WIN32) && !defined(_WIN32_WCE)
       
    36 #error Compiling for the wrong platform?
       
    37 #endif
       
    38 
       
    39 #include <stdio.h>
       
    40 #include <windows.h>
       
    41 
       
    42 #include "SDL_types.h"
       
    43 #include "SDL_error.h"
       
    44 #include "SDL_loadso.h"
       
    45 
       
    46 void *SDL_LoadObject(const char *sofile)
       
    47 {
       
    48 	void *handle = NULL;
       
    49 	const char *loaderror = "Unknown error";
       
    50 
       
    51 #if defined(_WIN32_WCE)
       
    52 	char errbuf[512];
       
    53 
       
    54 	wchar_t *errbuf_t = malloc(512 * sizeof(wchar_t));
       
    55 	wchar_t *sofile_t = malloc((MAX_PATH+1) * sizeof(wchar_t));
       
    56 
       
    57 	MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, sofile, -1, sofile_t, MAX_PATH);
       
    58 	handle = (void *)LoadLibrary(sofile_t);
       
    59 
       
    60 	/* Generate an error message if all loads failed */
       
    61 	if ( handle == NULL ) {
       
    62 		FormatMessage((FORMAT_MESSAGE_IGNORE_INSERTS |
       
    63 					FORMAT_MESSAGE_FROM_SYSTEM),
       
    64 				NULL, GetLastError(), 
       
    65 				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
       
    66 				errbuf_t, SDL_TABLESIZE(errbuf), NULL);
       
    67 		WideCharToMultiByte(CP_ACP, 0, errbuf_t, -1, errbuf, 511, NULL, NULL);
       
    68 		loaderror = errbuf;
       
    69 	}
       
    70 
       
    71 	free(sofile_t);
       
    72 	free(errbuf_t);
       
    73 
       
    74 #else /*if defined(WIN32)*/
       
    75 	char errbuf[512];
       
    76 
       
    77 	handle = (void *)LoadLibrary(sofile);
       
    78 
       
    79 	/* Generate an error message if all loads failed */
       
    80 	if ( handle == NULL ) {
       
    81 		FormatMessage((FORMAT_MESSAGE_IGNORE_INSERTS |
       
    82 					FORMAT_MESSAGE_FROM_SYSTEM),
       
    83 				NULL, GetLastError(), 
       
    84 				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
       
    85 				errbuf, SDL_TABLESIZE(errbuf), NULL);
       
    86 		loaderror = errbuf;
       
    87 	}
       
    88 #endif
       
    89 
       
    90 	if ( handle == NULL ) {
       
    91 		SDL_SetError("Failed loading %s: %s", sofile, loaderror);
       
    92 	}
       
    93 	return(handle);
       
    94 }
       
    95 
       
    96 void *SDL_LoadFunction(void *handle, const char *name)
       
    97 {
       
    98 	void *symbol = NULL;
       
    99 	const char *loaderror = "Unknown error";
       
   100 
       
   101 #if defined(_WIN32_WCE)
       
   102 	char errbuf[512];
       
   103 	int length = strlen(name);
       
   104 
       
   105 	wchar_t *name_t = malloc((length + 1) * sizeof(wchar_t));
       
   106 	wchar_t *errbuf_t = malloc(512 * sizeof(wchar_t));
       
   107 
       
   108 	MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, name, -1, name_t, length);
       
   109 
       
   110 	symbol = (void *)GetProcAddress((HMODULE)handle, name_t);
       
   111 	if ( symbol == NULL ) {
       
   112 		FormatMessage((FORMAT_MESSAGE_IGNORE_INSERTS |
       
   113 					FORMAT_MESSAGE_FROM_SYSTEM),
       
   114 				NULL, GetLastError(), 
       
   115 				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
       
   116 				errbuf_t, SDL_TABLESIZE(errbuf), NULL);
       
   117 		WideCharToMultiByte(CP_ACP, 0, errbuf_t, -1, errbuf, 511, NULL, NULL);
       
   118 		loaderror = errbuf;
       
   119 	}
       
   120 
       
   121 	free(name_t);
       
   122 	free(errbuf_t);
       
   123 
       
   124 #else /*if defined(WIN32)*/
       
   125 	char errbuf[512];
       
   126 
       
   127 	symbol = (void *)GetProcAddress((HMODULE)handle, name);
       
   128 	if ( symbol == NULL ) {
       
   129 		FormatMessage((FORMAT_MESSAGE_IGNORE_INSERTS |
       
   130 					FORMAT_MESSAGE_FROM_SYSTEM),
       
   131 				NULL, GetLastError(), 
       
   132 				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
       
   133 				errbuf, SDL_TABLESIZE(errbuf), NULL);
       
   134 		loaderror = errbuf;
       
   135 	}
       
   136 #endif
       
   137 
       
   138 	if ( symbol == NULL ) {
       
   139 		SDL_SetError("Failed loading %s: %s", name, loaderror);
       
   140 	}
       
   141 	return(symbol);
       
   142 }
       
   143 
       
   144 void SDL_UnloadObject(void *handle)
       
   145 {
       
   146 	if ( handle != NULL ) {
       
   147 		FreeLibrary((HMODULE)handle);
       
   148 	}
       
   149 }
       
   150