Click here to Skip to main content
15,886,362 members
Articles / Operating Systems / Windows

Platform Independent Coding - DLLs and SOs

Rate me:
Please Sign up or sign in to vote.
4.79/5 (86 votes)
28 Mar 2007CPOL9 min read 157.7K   2.3K   86  
If you want to write platform independent code, just read through the following article.
//Boby Thomas - march 2006

#ifndef os_call_h
#define os_call_h

#if defined(_MSC_VER) // Microsoft compiler
	#include <windows.h>
#elif defined(__GNUC__) // GNU compiler
	#include <dlfcn.h>
#else
#error define your copiler
#endif

#include<string>
/*
#define RTLD_LAZY   1
#define RTLD_NOW    2
#define RTLD_GLOBAL 4
*/

void* LoadSharedLibrary(char *pcDllname, int iMode = 2)
{

	std::string sDllName = pcDllname;
	#if defined(_MSC_VER) // Microsoft compiler
	sDllName += ".dll";
		return (void*)LoadLibrary(pcDllname);
	#elif defined(__GNUC__) // GNU compiler
	sDllName += ".so";
		return dlopen(sDllName.c_str(),iMode);
	#endif


}
void *GetFunction(void *Lib, char *Fnname)
{
#if defined(_MSC_VER) // Microsoft compiler
	return (void*)GetProcAddress((HINSTANCE)Lib,Fnname);
#elif defined(__GNUC__) // GNU compiler
	return dlsym(Lib,Fnname);
#endif
}

bool FreeSharedLibrary(void *hDLL)
{
#if defined(_MSC_VER) // Microsoft compiler
	return FreeLibrary((HINSTANCE)hDLL);
#elif defined(__GNUC__) // GNU compiler
	return dlclose(hDLL);
#endif
}


#endif //os_call_h

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) DWS
Australia Australia

Comments and Discussions