[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

using dll

Posted on 2002-03-05
4
Medium Priority
?
316 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 400 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

650 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