• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 324
  • Last Modified:

using dll

in a source i found out the wae to use functions from a dll you load on the fly is to :
FunctionName=(cfunc)GetProcAddress((HMODULE)hLib, "Functionname");
 is tat correct ?
what if i want to use a class and its functions etc ?
how would i do that ? thanks
0
prinx
Asked:
prinx
1 Solution
 
Roshan DavisCommented:
MSDN Says

Using Run-Time Dynamic Linking
You can use the same DLL in both load-time and run-time dynamic linking. The following source code produces the same output as the load-time example in the previous section. The program uses the LoadLibrary function to get a handle to MYPUTS.DLL. If LoadLibrary succeeds, the program uses the returned handle in the GetProcAddress function to get the address of the DLL's myPuts function. After calling the DLL function, the program calls the FreeLibrary function to unload the DLL.

The following example illustrates an important difference between run-time and load-time dynamic linking. If the MYPUTS.DLL file is not available, the application using load-time dynamic linking simply terminates. The run-time dynamic linking example, however, can respond to the error.

// File:  RUNTIME.C
// A simple program that uses LoadLibrary and
// GetProcAddress to access myPuts from MYPUTS.DLL.
 
#include <stdio.h>
#include <windows.h>
 
typedef VOID (*MYPROC)(LPTSTR);
 
VOID main(VOID)
{
    HINSTANCE hinstLib;
    MYPROC ProcAdd;
    BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;
 
    // Get a handle to the DLL module.
 
    hinstLib = LoadLibrary("myputs");
 
    // If the handle is valid, try to get the function address.
 
    if (hinstLib != NULL)
    {
        ProcAdd = (MYPROC) GetProcAddress(hinstLib, "myPuts");
 
        // If the function address is valid, call the function.
 
        if (fRunTimeLinkSuccess = (ProcAdd != NULL))
            (ProcAdd) ("message via DLL function\n");
 
        // Free the DLL module.
 
        fFreeResult = FreeLibrary(hinstLib);
    }
 
    // If unable to call the DLL function, use an alternative.
 
    if (! fRunTimeLinkSuccess)
        printf("message via alternative method\n");
}
Because the program uses run-time dynamic linking, you should not link with the import library when creating the program module.



GOOD LUCK
0
 
prinxAuthor Commented:
thanks for the information they are very useful ;>
however i need to know how to create and use an object of a class that is defined in the dll..thanks
0
 
pjknibbsCommented:
prinx: DLLs do not support C++ style objects and classes--they only have individual function calls. If you want something which is more object-oriented you'll have to start using COM.
0
 
jkrCommented:
>>what if i want to use a class and its functions etc ?

See e.g. http://www.codeguru.com/win32/dyndllclass.shtml
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now