Click here to Skip to main content
16,011,611 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Passing filename from C++ to Fortran Pin
RedSonja9-Dec-08 20:46
RedSonja9-Dec-08 20:46 
GeneralRe: Passing filename from C++ to Fortran Pin
cp98769-Dec-08 21:03
cp98769-Dec-08 21:03 
GeneralRe: Passing filename from C++ to Fortran Pin
RedSonja9-Dec-08 21:53
RedSonja9-Dec-08 21:53 
GeneralRe: Passing filename from C++ to Fortran Pin
cp98769-Dec-08 22:50
cp98769-Dec-08 22:50 
GeneralRe: Passing filename from C++ to Fortran Pin
Luc Pattyn9-Dec-08 22:25
sitebuilderLuc Pattyn9-Dec-08 22:25 
AnswerRe: Passing filename from C++ to Fortran Pin
RedSonja11-Dec-08 20:13
RedSonja11-Dec-08 20:13 
QuestionWinInet Multithread Problems [modified] Pin
Julian Popov8-Dec-08 22:38
Julian Popov8-Dec-08 22:38 
QuestionIs it possible to have an array of types ? Pin
Defenestration8-Dec-08 22:20
Defenestration8-Dec-08 22:20 
For one of my projects I've created a class wrapper around some functions contained in a DLL. To use these DLL functions, I instantiate an object of the type class wrapper, and then call the member function LoadDLL(). LoadDLL() calls ::LoadLibrary() for the DLL, and then calls GetProcAddress() for each DLL function I wish to wrap. If any of the functions couldn't be found, I wish to FreeLibrary(), and return false.

At the moment I do something along the lines of:

typedef	void		(*p2DLLFunc1)	(void);
typedef	char*		(*p2DLLFunc2)	(void);

bool	CDLLWrapper::LoadDLL()
{
	bool	bResult = false;

	m_hmoduleDll = ::LoadLibrary(_T("DLLName"));
	if (NULL != m_hmoduleDll)
	{ // DLL loaded successfully. Now get addresses of all functions we are supporting
		m_funcDLLFunc1	= reinterpret_cast<p2DLLFunc1>(GetProcAddress(m_hmoduleDll, _T("DLLFunc1")));
		m_funcDLLFunc2	= reinterpret_cast<p2DLLFunc2>(GetProcAddress(m_hmoduleDll, _T("DLLFunc2")));
		m_funcDLLFunc3	= reinterpret_cast<p2DLLFunc1>(GetProcAddress(m_hmoduleDll, _T("DLLFunc3")));
		
		if (	NULL != m_funcDLLFunc1	&&
			NULL != m_funcDLLFunc2	&&
			NULL != m_funcDLLFunc3)
		{ // SUCCESS: All wrapped functions found in DLL
			bResult = true;
		}
		else
		{ // FAILURE: Could not find one or more wrapped functions
			::FreeLibrary(m_hmoduleDll);
		}
	}
	
	return bResult;
}


However, when lots of functions are being wrapped the above method is a bit tedious and can be error prone. Instead, I would like to do it with a lookup table, like so

bool	CDLLWrapper::LoadDLL()
{
	struct	MyLUT {
			TCHAR*	tcFuncName;
				funcDLLFunc;
			int	iFunctionSignatureType;
	}; // struct	MyLUT {

	static	MyLUT	arrWrappedFuncs[]	=	{	_T("DLLFunc1"),		m_funcDLLFunc1,	0
								_T("DLLFunc2"),		m_funcDLLFunc2,	1
								_T("DLLFunc3"),		m_funcDLLFunc3,	0
	}; // static	MyLUT	arrWrappedFuncs[]

	const	int	iNUM_WRAPPED_FUNCTIONS	= sizeof(arrWrappedFuncs) / sizeof(MyLUT);

	bool	bResult = false;

	m_hmoduleDll = ::LoadLibrary(_T("DLLName"));
	if (NULL != m_hmoduleDll)
	{ // DLL loaded successfully. Now get addresses of all functions we are supporting
		bResult = true;	// Assume success
		for (int i = 0; i < iNUM_WRAPPED_FUNCTIONS; ++i)
		{
			switch(arrWrappedFuncs[i].iFunctionSignatureType)
			{
				case 0:
				{
					arrWrappedFuncs[i].funcDLLFunc	= reinterpret_cast<p2DLLFunc1>(GetProcAddress(m_hmoduleDll, arrWrappedFuncs[i].tcFuncName));				
				}
				break;
				
				case 1:
				{
					arrWrappedFuncs[i].funcDLLFunc	= reinterpret_cast<p2DLLFunc2>(GetProcAddress(m_hmoduleDll, arrWrappedFuncs[i].tcFuncName));				
				}
				break;
				
				default:
				{
					// assert()
				}
			}
			
			if (NULL == arrWrappedFuncs[i].funcDLLFunc)
			{ // one of the wrapped functions couldn't be found, so don't try to find any others
				bResult = false;
				break;
			}
		}
		
		if (!bResult)
		{ // FAILURE: Could not find one or more wrapped functions
			::FreeLibrary(m_hmoduleDll);
		}
	}
	
	return bResult;
}


The two problems I've got are:

1) How can I store a type in an array, so that I can use it in the reinterpret_cast<> ?
2) What type to define funcDLLFunc in struct MyLUT ?

Not sure about (1), but was thinking of using a union for (2), containing all the different function signatures. Every time a newly wrapped function had a different signature, I would just add a new member to the union and create a new Type.

If it's not possible to store a type in an array, then I guess the only solution is to have a switch statement on iFunctionSignatureType, and then just have the GetProcAddress() line for each type with a different reinterpret_cast<>. However, this is tedious as well, hence my hope for being able to store a type in an array.


Any ideas ?
AnswerRe: Is it possible to have an array of types ? Pin
Code-o-mat8-Dec-08 23:38
Code-o-mat8-Dec-08 23:38 
AnswerRe: Is it possible to have an array of types ? Pin
Defenestration8-Dec-08 23:43
Defenestration8-Dec-08 23:43 
GeneralRe: Is it possible to have an array of types ? Pin
Code-o-mat9-Dec-08 0:01
Code-o-mat9-Dec-08 0:01 
AnswerRe: Is it possible to have an array of types ? Pin
Defenestration9-Dec-08 0:12
Defenestration9-Dec-08 0:12 
GeneralRe: Is it possible to have an array of types ? Pin
Code-o-mat9-Dec-08 0:36
Code-o-mat9-Dec-08 0:36 
GeneralRe: Is it possible to have an array of types ? Pin
Defenestration9-Dec-08 0:48
Defenestration9-Dec-08 0:48 
GeneralRe: Is it possible to have an array of types ? Pin
Code-o-mat9-Dec-08 1:19
Code-o-mat9-Dec-08 1:19 
GeneralRe: Is it possible to have an array of types ? Pin
Defenestration9-Dec-08 1:27
Defenestration9-Dec-08 1:27 
QuestionRe: Is it possible to have an array of types ? Pin
David Crow9-Dec-08 3:14
David Crow9-Dec-08 3:14 
AnswerRe: Is it possible to have an array of types ? Pin
Michael Dunn9-Dec-08 13:52
sitebuilderMichael Dunn9-Dec-08 13:52 
QuestionHow to unblock pipe write operation? Pin
sashoalm8-Dec-08 21:39
sashoalm8-Dec-08 21:39 
QuestionNeed some Bits/Bytes and Types information Pin
gabbana8-Dec-08 21:24
gabbana8-Dec-08 21:24 
AnswerRe: Need some Bits/Bytes and Types information Pin
CPallini8-Dec-08 22:16
mveCPallini8-Dec-08 22:16 
AnswerRe: Need some Bits/Bytes and Types information Pin
Code-o-mat8-Dec-08 22:22
Code-o-mat8-Dec-08 22:22 
AnswerRe: Need some Bits/Bytes and Types information Pin
gabbana8-Dec-08 22:32
gabbana8-Dec-08 22:32 
GeneralRe: Need some Bits/Bytes and Types information Pin
gabbana8-Dec-08 22:35
gabbana8-Dec-08 22:35 
GeneralRe: Need some Bits/Bytes and Types information Pin
Code-o-mat8-Dec-08 23:00
Code-o-mat8-Dec-08 23:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.