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