All pastes #365860 Raw Edit

Stuff

public text v1 · immutable
#365860 ·published 2007-02-21 09:08 UTC
rendered paste body
// Standard includes
//--------------------------------------------------------------------------------
#include <iostream>

// Define the platform to compile for
//--------------------------------------------------------------------------------

#define PLATFORM_WINDOWS
//#define PLATFORM_LINUX
//#define PLATFORM_BSD

//--------------------------------------------------------------------------------

// Platform-dependent includes
//--------------------------------------------------------------------------------

#ifdef PLATFORM_WINDOWS
	#include <windows.h>
#endif
#ifdef PLATFORM_LINUX || PLATFORM_BSD
	#include <dlfcn.h>
#endif

//--------------------------------------------------------------------------------

// Note: Only Windows has been tested so far.


class module
{
public:
	module(std::string module_name)
	{
		load(module_name);
	}

	// Loads a module into memory
	// module_name: The file name without an extension.
	void load(std::string module_name)
	{
		std::string file_name(module_name);

		#ifdef PLATFORM_WINDOWS
		{
			file_name += ".dll";
			handle = LoadLibrary(file_name.c_str());
		}
		#endif
		#ifdef PLATFORM_LINUX
		{
			file_name += ".so";
			handle = dlopen(file_name.c_str());
		}
		#endif
		#ifdef PLATFORM_BSD
		{
			file_name += ".dll";
			handle = dlopen(file_name.c_str());
		}

		#endif
	}

	// Unloads the module from memory
	void unload()
	{
		#ifdef PLATFORM_WINDOWS
		{
			FreeLibrary(handle);
		}
		#endif
		#ifdef PLATFORM_LINUX || PLATFORM_BSD
		{
			dlclose(handle);
		}
		#endif
	}

	bool is_loaded()
	{
		return handle ? true : false;
	}

	template<class T>
	T get(std::string api_name)
	{
		#ifdef PLATFORM_WINDOWS
		{
			FARPROC fp = GetProcAddress(handle, api_name.c_str());
			return T(fp);
		}
		#endif
		#ifdef PLATFORM_LINUX || PLATFORM_BSD
		{

		}
		#endif
	}

private:
	#ifdef PLATFORM_WINDOWS
		HMODULE handle;
	#endif
	#ifdef PLATFORM_LINUX || PLATFORM_BSD
		void* handle;
	#endif
};

typedef (WINAPI *mbox_func)(HWND,LPCTSTR,LPCTSTR,UINT);

int main()
{
	module test("user32");

	mbox_func f = test.get<mbox_func> ("MessageBoxA");

	f(0, "test", "test", 0);

	return 0;
}