Solved

using dll

Posted on 2002-03-05
4
277 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
>>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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
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…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now