Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
Hello!
I write my own dll in MFC and want to call the functions of dll in my MFC dialogue based application.There are multiple functions in DLL which are return Handle,int etc. and I would like to call that functions on different events happened on dialogue application.
so my question is how to declare function pointer of that functions globally and call that function with respect to that event?
Please give solution!!!
Posted
Comments
Sergey Alexandrovich Kryukov 20-Mar-13 2:33am    
Not clear what's the problem. Just do it by definition; if in doubt, write a code for simplest sample in just two files, test it, and, if there are problems, show this sample, using "Improve question".
—SA

1 solution

Take a look at this article with example and explanation: DYNAMIC LINK LIBRARY - DLL[^]

Basically you can import public symbols into an application or export functions from a DLL using two methods:
- Use a module definition (.DEF) file when building the DLL.

A minimal .DEF file must contain the following module-definition statements:
The first statement in the file must be the LIBRARY statement. This statement identifies the .DEF file as belonging to a DLL. The LIBRARY statement is followed by the name of the DLL. The linker places this name in the DLL's import library.

The EXPORTS statement lists the names and, optionally, the ordinal values of the functions exported by the DLL. You assign the function an ordinal value by following the function's name with an at sign (@) and a number. When you specify ordinal values, they must be in the range 1 through N, where N is the number of functions exported by the DLL.

For example, a DLL that contains the code to implement a binary search tree might look like the following:
C++
LIBRARY   BTREE
EXPORTS
   Insert   @1
   Delete   @2
   Member   @3
   Min   @4


- Use the keywords __declspec(dllimport) or __declspec(dllexport) in a function definition in the main application:

C++
__declspec(dllexport) int mydll(LPTSTR lpszMsg)

And here an example how to use explicit linking to load the DLL at run time:
C++
typedef UINT (CALLBACK* LPFNDLLFUNC1)(DWORD, UINT);


HINSTANCE hDLL;               // Handle to DLL
LPFNDLLFUNC1 lpfnDllFunc1;    // Function pointer
DWORD dwParam1;
UINT  uParam2, uReturnVal;
hDLL = LoadLibrary("MyDLL");

if (hDLL != NULL)
{
   lpfnDllFunc1 = (LPFNDLLFUNC1)GetProcAddress(hDLL, "DLLFunc1");
   if (!lpfnDllFunc1)
   {
      // handle the error
      FreeLibrary(hDLL);

      return SOME_ERROR_CODE;
   }
   else
   {
      // call the function
      uReturnVal = lpfnDllFunc1(dwParam1, uParam2);
   }
}
 
Share this answer
 
v3

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