win32_library.h (815B)
1 #pragma once 2 3 #include <windows.h> 4 5 typedef HMODULE Library; 6 7 Library library_open( const char * path ) { 8 return LoadLibraryExA( path, NULL, LOAD_LIBRARY_SEARCH_SYSTEM32 ); 9 } 10 11 FARPROC library_function( Library lib, const char * name ) { 12 return GetProcAddress( lib, name ); 13 } 14 15 const char * library_last_error() { 16 /* 17 * TODO: maybe this should go in a helper function 18 * these functions should really only get called by the main thread, so 19 * this should be ok 20 */ 21 DWORD error = GetLastError(); 22 if( error == 0 ) { 23 return NULL; 24 } 25 26 static char buf[ 1024 ]; 27 FormatMessageA( FORMAT_MESSAGE_FROM_SYSTEM, NULL, error, 28 MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ), buf, sizeof( buf ), NULL ); 29 return buf; 30 } 31 32 void library_close( Library lib ) { 33 if( FreeLibrary( lib ) == 0 ) { 34 FATAL( "FreeLibrary" ); 35 } 36 }