Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using a DLL function in VC++

Status
Not open for further replies.

Public32

IS-IT--Management
Feb 27, 2003
6
CA
Hi,
I am somewhat new to pragramming in Windows and I want to know how I can use a dll in a c++ program. for instance if I was to use a function that is in c:\windows\system32\advapi32.dll
how do I include it in the project.? and then how do I call a function in that dll.

Thanks In advance.
S
 
Usually you'd just include the proper header file and link to the correct .lib file (static linking).

I haven't used these much, but I believe if you use LoadLibrary() and GetProcAddress() you can call DLL functions without linking to the .lib file (dynamic linking).
 
To include static(lib files) or dynamic(dll files) library in your project you must have the corrisponding header files,that represent the "blackbox" of the library that the author created for the users.If u don't have the header you can't use the dll, because you have no idea of that it contains! if u have header and compiled library(dll or lib) then u can link it to your project with the related various(and also quite laborious) procedures.I can send you my guide but it's in italian, sorry.
 
You do not need the header file if you are doing dynamic linking. You need only to know the name and signature of the function you want to call.
The example below calls a function from myDll.dll called myfunction. myfunction takes and returns an int.
Code:
//function signature
typedef int (*THEFUNCTION)(int); 

//dll instance and function
HINSTANCE dllinstance ; 
THEFUNCTION theFunctionAddress ; 
 
//load the DLL
dllinstance = LoadLibrary(TEXT("myDll"));
if(dllinstance != NULL) 
{
    //get the function using its name
    theFunctionAddress = (THEFUNCTION)GetProcAddress(dllinstance, "myfunction");
    if(theFunctionAddress != NULL)
    {
        //run the function in this example using 0 as the parameter
        int result = theFunctionAddress(0);
    }
    //free the dll
    BOOL ok = FreeLibrary(dllinstance );
}
note: the name of the function must be identical to that in the exports statement of the loaded DLL's .def file.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top