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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how do I use a regular DLL file? 1

Status
Not open for further replies.

Sidro

MIS
Sep 28, 2002
197
US
hi,
I've created a simple dll file using the win32 dynamic link library and a .def file.Now I would like to use it in a win32 console application.How do I set it up?This is how my DLL file looks like and the name of the file is Test.dll

#include<iostream>
using namespace std;

void print()
{
cout<<&quot;Hello world!&quot;;

}

 
Hello Psidro,

Regular DLLs are a bit more involved than using Extension DLLs. To use a function from the DLL &quot;explicitly&quot; you need to load the DLL and obtain a pointer to the function. You then call the function through the pointer and then free the library when you're through with it. The typical steps are:

[tt]
HINSTANCE hInstance;
PRINT* pFunction;
hInstance = ::LoadLibrary(&quot;test.dll&quot;); // <- load the dll

// get a pointer to the 'print()' function in the dll
pFunction = (PRINT*)::GetProcAddress(hInstance,&quot;print&quot;);

(*pFunction)(); // <- call the print() function
::FreeLibrary(hInstance); // <- free the dll
[/tt]
tellis.gif

programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.
 
thanks qednick for your help.I am curious and in the process of learning to build windows application. What kind of datatype is HInstance and can you give an example of how to use extension DLLs? I want to see whats the difference in the two methods.And do you know of a good book on these sort of topic?
 
Well, extension DLLs are far far easier to use BUT you need to link to the .lib file and #include the header files for the class you are linking to in the DLL.

To create an extension DLL you create your new DLL project in VC++ (or whatever) and choose the 'extension DLL' as the type. You then insert whatever classes and resources you need in the DLL. Now... where you have declared a class, you need to stick a little something in there to &quot;export&quot; it: [tt]

class AFX_EXT_CLASS MyDLLClass
{
....
{;[/tt]

Then, compile the DLL and copy it (the actual DLL) and the class header file into another project (such as an application which will be &quot;importing&quot; the class). Final step is to #include the header file wherever it's needed and then to link to the .LIB file produced with the DLL.

To link to the .LIB file (in VC++ at least!) you need to choose 'Project->Settings' menu option. Click on the tab labelled 'Link' and choose the 'General' category in the combo box in the dialog. There should be a text filed (edit box) in there called something like 'Library/Link Modules' (I think!) - you need to add the path to the .LIB file for the DLL file in that edit box.

I guess this sounds kinda complicated but when you do it you'll see just how easy it is! Once you've followed these steps you can use your DLL class just as if the class was declared within your application project. No need to load libraries or anything like that!!

The HINSTANCE is a 'handle' to a particular instance of a program or resource file or whatever (including DLLs).

The best book I've seen with DLL examples so far is &quot;Programming Visual C++&quot; from Microsoft press. The Visual C++ Bible also give good examples but the former is clearer.

[deejay]
tellis.gif

programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.
 
Ohhh, actually.. there's something I forgot to put in that regular DLL example code. You need to declare a typedef for the function pointer first! : [tt]

typedef void (PRINT)(void);

HINSTANCE hInstance;
PRINT* pFunction;
hInstance = ::LoadLibrary(&quot;test.dll&quot;); // <- load the dll

// get a pointer to the 'print()' function in the dll
pFunction = (PRINT*)::GetProcAddress(hInstance,&quot;print&quot;);

(*pFunction)(); // <- call the print() function
::FreeLibrary(hInstance); // <- free the dll[/tt]

The typedef is basically telling the compiler what the prototype and return type is for the function you are calling in the DLL.

With experience, you'll soon get to know which kind of DLL to use under certain circumstances. I always try to use extension DLLs when I am simply &quot;importing&quot; a whole class because this makes things so much easier. However, there may be times when you need to load a library at run time, perhaps because the user has chosen a particular language or maybe because you need to determine whether a particular function will work on that user's system. These are the times when you should use a regular DLL and &quot;explicitly&quot; call the function through a pointer like in the above example.

[rockband]
tellis.gif

programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top