Solved

using dll

Posted on 2002-03-05
4
286 Views
Last Modified: 2010-04-01
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
Comment
Question by:prinx
4 Comments
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 6841181
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
 

Author Comment

by:prinx
ID: 6841201
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
 
LVL 12

Expert Comment

by:pjknibbs
ID: 6841493
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
 
LVL 86

Accepted Solution

by:
jkr earned 100 total points
ID: 6841517
>>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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question