Click here to Skip to main content
15,885,988 members
Articles / Desktop Programming / Win32

Yet another DLL-wrapper for dynamic loading

Rate me:
Please Sign up or sign in to vote.
4.88/5 (11 votes)
25 Mar 2012LGPL314 min read 55.9K   1.2K   80  
A DLL-wrapper that requires no code changes when switching to dynamic loading and provides detailed error checking functionality.
#include "stdafx.h"

// A function called from a stub. If this is declared here it can of course
// also be used in the include files psapixx.h or psapix.h in as return
// value of individual entries. 
// This is, however hardly necessary as through p_symbol it allows returning
// function-specific values.
// However, there is no way to get to the parameters passed to the function
// as of now
class CDynlinkSymbol;

template<class T> T _s_stub_error(CDynlinkSymbol * p_symbol)
{
	printf("  ----: STUB called: Return type '%s', symbol name '%s'\n", typeid(T).name(), p_symbol ->get_name());
	return (DWORD) -1;
}


#define DYNLINK_DEFINE
#include <psapix.h>

#ifndef DYNLINK_LINK_IMPLICIT
	// When using explicit loading, we also include a prefixed version of the
	// library.
	// This means that all function names will be modified by prepending a 
	// prefix defined by DYNLINK_PREFIX.
	// CAREFUL: The header must be included in all files using the prefixed 
	// version with the same value assigned to the DYNLINK_PREFIX macro. See
	// test_dynlink.cpp for comparison
#	define DYNLINK_PREFIX S_
	
	// The following macros can be defined inside the header or outside. They
	// are only used when DYNLINK_DEFINE is defined - i.e. where the symbols 
	// are actually defined.
	// Therefore it is ok to have them here and not in test_dynlink.cpp

	// Use stubs for this import
#	define DYNLINK_USE_STUBS

	// Instead of simply returning a value, all stubs of functions without a
	// function specific return values (i.e. those declared with 
	// DYNLINK_DECLARE, not DYNLINK_DECLARE_EX) will call this function and
	// return its return value.
#	define DYNLINK_RETVAL_DWORD _s_stub_error<DWORD>(p_symbol)
#	define DYNLINK_RETVAL_int   _s_stub_error<int>(p_symbol)

	// .. or a simple return value indicating that the stub was called
#	define DYNLINK_RETVAL_BOOL  FALSE
#	include <psapixx.h>
#endif

void test_dynlink();
void test_dynamic();
void test_lateload();

// This file just loads the libraries and calls dynlink_test() which is defined 
// in test.cpp
int _tmain(int argc, _TCHAR* argv[])
{
	try
	{
		test_dynamic();
		test_lateload();
		test_dynlink();
	}
	catch(std::exception &e)
	{
		printf(e.what());
	}
	
}

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 GNU Lesser General Public License (LGPLv3)


Written By
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions