Call structure in class of MFC

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?
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!!
Your question isn't clear. We'd like to help, but I'm sure what the problem is.
First thing I wrote function for DLL depending function pointers and that function pointers called through structure defined in another function, so I just want to know how to declare,define and return function successfully??
Firstly: As it looks like you're new here...

Please use code tags --> the <> button below the new post box or to the right of the post edit box. As well as looking nice, it provides line numbers which make it easier to refer to posted code.

1
2
3
4
5
6
7
8
#include <iostream>
using namespace std;

int main() {
    cout << "Hello nicely formatted world!\n";

    return 0;
}


And a couple of articles:

How to use code tags
http://www.cplusplus.com/articles/jEywvCM9/

How to use tags
http://www.cplusplus.com/articles/z13hAqkS/
(for code and other types of tag)

You can even go back and add tags to your earlier posts...
I just want to know how to declare,define and return function successfully??

Second: what are trying to use this returned function for?

And how are you initializing the function pointers?

Andy
Actually, as you mention MFC, so you're using classes, and DLLs, are you wanting to write a DLL wrapper class?

(If I'm using C++, rather than C, I use a DLL wrapper class, rather than a struct of function pointers, so the class can manage the pointers for me.)

Andy

Basic example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <iostream>
using namespace std;

typedef int(__stdcall *PFNADD)(int,int);
typedef int(__stdcall *PFNSUBTRACT)(int,int);
typedef int(__stdcall *PFNNEGATE)(int);
typedef int(__stdcall *PFNSET)(int);
typedef int(__stdcall *PFNZERO)();

class DLLWrapper
{
private:
    HMODULE     m_hMod;
    PFNADD      m_pfnAdd;
    PFNSUBTRACT m_pfnSubtract;
    PFNNEGATE   m_pfnNegate;
    PFNSET      m_pfnSet;
    PFNZERO     m_pfnZero;

public:
    DLLWrapper(LPCSTR modulePath = NULL)
    : m_hMod(NULL)
    , m_pfnAdd(NULL)
    , m_pfnSubtract(NULL)
    , m_pfnNegate(NULL)
    , m_pfnSet(NULL)
    , m_pfnZero(NULL)
    {
        if(NULL != modulePath)
            Load(modulePath);
        // could throw here, if it fails to load
    }

    ~DLLWrapper()
    {
        Unload();
    }

    DWORD Load(LPCSTR modulePath)
    {
        Unload();
        m_hMod = LoadLibraryA(modulePath);
        if(NULL == m_hMod)
            return GetLastError();
        m_pfnAdd = (PFNADD)GetProcAddress(m_hMod, "Test_Add");
        if(NULL != m_pfnAdd)
            m_pfnSubtract = (PFNSUBTRACT)GetProcAddress(m_hMod, "Test_Subtract");
        if(NULL != m_pfnSubtract)
            m_pfnNegate = (PFNNEGATE)GetProcAddress(m_hMod, "Test_Negate");
        if(NULL != m_pfnNegate)
            m_pfnSet = (PFNSET)GetProcAddress(m_hMod, "Test_Set");
        if(NULL != m_pfnSet)
            m_pfnZero = (PFNZERO)GetProcAddress(m_hMod, "Test_Zero");
        if(NULL == m_pfnZero)
        {
            Unload();
            return GetLastError();
        }
        return NOERROR;
    }

    DWORD Unload()
    {
        if(NULL != m_hMod)
            FreeLibrary(m_hMod);
        m_pfnAdd      = NULL;
        m_pfnSubtract = NULL;
        m_pfnNegate   = NULL;
        m_pfnSet      = NULL;
        m_pfnZero     = NULL;
        return NOERROR;
    }

    int Add(int lhs, int rhs) const
    {
        if(NULL == m_pfnAdd)
            return 0; // could throw here
        return (*m_pfnAdd)(lhs, rhs);
    }

    int Subtract(int lhs, int rhs) const
    {
        if(NULL == m_pfnSubtract)
            return 0; // could throw here
        return (*m_pfnSubtract)(lhs, rhs);
    }

    int Negate(int val) const
    {
        if(NULL == m_pfnNegate)
            return 0; // could throw here
        return (*m_pfnNegate)(val);
    }

    int Set(int val) const
    {
        if(NULL == m_pfnSet)
            return 0; // could throw here
        return (*m_pfnSet)(val);
    }

    int Zero() const
    {
        if(NULL == m_pfnZero)
            return 0; // could throw here
        return (*m_pfnZero)();
    }
};

int main()
{
    DLLWrapper wrapper("TEST_DLL.dll");

    int w = wrapper.Zero();
    cout << "Zero -> w = " << w << endl;
    int x = wrapper.Set(3);
    cout << "Set(3) -> x = " << x << endl;
    x = wrapper.Negate(x);
    cout << "Negate(x) -> x = " << x << endl;
    int y = wrapper.Add(w, x);
    cout << "Add(w, x) -> y = " << y << endl;
    int z = wrapper.Subtract(y, 2);
    cout << "Subtract(y, 2) -> z = " << z << endl;
    cout << endl;

    cout << "answer = " << z << endl;
    cout << endl;

    return 0;
}


Output:

Zero -> w = 0
Set(3) -> x = 3
Negate(x) -> x = -3
Add(w, x) -> y = -3
Subtract(y, 2) -> z = -5

answer = -5

Where the source for TEST_DLL is just:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD   fdwReason,
                       LPVOID  lpvReserved )
{
    switch(fdwReason)
    {
    case DLL_PROCESS_ATTACH:
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}

int __stdcall Test_Add(int lhs, int rhs)
{
    return (lhs + rhs);
}

int __stdcall Test_Subtract(int lhs, int rhs)
{
    return (lhs - rhs);
}

int __stdcall Test_Negate(int val)
{
    return -val;
}

int __stdcall Test_Set(int val)
{
    return val;
}

int __stdcall Test_Zero()
{
    return 0;
}


And the def file is (used to export undecorated names):

1
2
3
4
5
6
EXPORTS
	Test_Add
	Test_Subtract
	Test_Negate
	Test_Set
	Test_Zero

Last edited on
I initialise function pointer as given below:
1
2
3
4
5
        pointerFunction6 OpenHandle=NULL;
	pFunctionPointer7 ReadDataFile= NULL;

	OpenHandle=(pointerFunction6)GetProcAddress(hLib,"OpenHandle");
	ReadDataFile= (pFunctionPointer7)GetProcAddress(hLib,"ReadDataFile");

I write function according to different condition for different interfaces,which will used in my application and in this function I used API of that interfaces.These function pointers initialize in structure and used on Button clicked event in application!Is that possible or not?
Would the class wrapper approach I suggested work for you?

Your dialog class would have a DLL wrapper member which it could use to call through to the DLL functions.

Andy
Last edited on
Yes,Thanks I tried with this method
Ok

Note that you should always specify the calling convention (__stdcall in my example) when declaring function pointers that point at functions in another module.

Andy
Last edited on
Topic archived. No new replies allowed.