Click here to Skip to main content
15,908,581 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In MFC dialogue based application,I've to call dll function using function pointer for different condition, so I think these function pointers are declared in wrapper function which containing structure & at last returns structure pointer which containing address of function pointer.How to do that? any idea?
C++
void *UniqueFunction(int flag)
{
	if(flag==0)
	{
		struct MyStruct
		{
		typedef int(*pFunctionPointer1)(CString,HANDLE *);
		typedef int(*pFunctionPointer2)(int,HANDLE);
		typedef int(*pFunctionPointer3)(int,int,char*,HANDLE);
		typedef int(*pFunctionPointer4)(int,int,int,HANDLE);
		typedef int(*pFunctionPointer5)(HANDLE);
		typedef CString(*pFunctionPointer6)(DWORD);
		}*FirstStruct;
		return FirstStruct;
	}

In above code I've tried to declare structure in function,it compiled error free but how to call that on event of button clicked?and is this right or wrong?Please suggest!!
Posted
Updated 19-Jun-13 21:02pm
v6

You are just returning a uninitialized void pointer to the calling function. Is that useful?
I suppose you have to make the struct declaration available to the caller (that it should not be local) and you have to initialize somehow (your requirements are not clear to me) the pointer you are returning.
 
Share this answer
 
Comments
Venkat Raghvan 20-Jun-13 3:20am    
I know that this not useful!I just want to know how to declare,define and returned successfully??
You need to instantiate the actual pointers from the typedefs. Your structure makes no sense as it only contains the definitions of the types but no actual variables. You need something like:
C++
struct MyStruct
{
    typedef int(*pFunctionPointer1Type)(CString,HANDLE *);
    pFunctionPointer1Type pFunctionPointer1;
//    typedef int(*pFunctionPointer2)(int,HANDLE);
//    typedef int(*pFunctionPointer3)(int,int,char*,HANDLE);
//    typedef int(*pFunctionPointer4)(int,int,int,HANDLE);
//    typedef int(*pFunctionPointer5)(HANDLE);
//    typedef CString(*pFunctionPointer6)(DWORD);
}*FirstStruct;
    FirstStruct = new MyStruct;
    FirstStruct->pFunctionPointer1 = FunctionOne;
// repeat for other pointers

// or if using dynamic DLL
HINSTANCE hDLL = ::LoadLibrary("mydllname");
FirstStruct->pFunctionPointer1 = (pFunctionPointer1Type)::GetProcAddress(hDLL, "FunctionOne");

return FirstStruct;
 
Share this answer
 
Hi,
I think you have to read about GetProcaddress function in MSDN.
see this article:
http://stackoverflow.com/questions/6031431/getprocaddress-function-in-c[^]

What I do usually is:
define typedef of different functions
declare a structure or a class with all types of functions
call loadLibrary
then get the pointer of each function of the library using GetProcAddress

after them you can call struct.function1(parameter).

Best regards.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900