Link to home
Start Free TrialLog in
Avatar of Lescha
Lescha

asked on

DLL Questions Series, 1

Using Visual Studio (ver. 6), how do I convert a pair of text files, one called MyLib.CPP the other MyLib.H, into a DLL?

The expected answer is an algorithm constisting of actions of the kind:

Add so-and-so text to the so-and-so places in the file.
Use so-and-so options in the compiler/linker configuration.
Etc.

For now, we shall assume that the files contain simple C++ functions and do not use MFC classes.
ASKER CERTIFIED SOLUTION
Avatar of Zoppo
Zoppo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nietod
nietod

>> 1. Create a def file for the library MyLib.def like this
Most people don't use .def files anymore.  You still need them when exporting to languages that don't support the standard call decoration, like VB, but other than that it really isn't needed.  (No harm, but extra work.)
I just thought this is the only way to do this without need to modify the both given files MyLib.h and MyLib.cpp ...

ZOPPO
that's true.  I didn't think of it like that.  But you probably still have to modiy the .cpp file (add a DllMain()).  And while it works, it may not be the best way.  i.e. if development will continue in the DLL, it woudl probably be better to use __declspec(dllexport) instead of the .def file.
Yes, nietod, I agree ...



BTW, while I'm having your attention as one of the TOPs at EE, I wanted to ask you if you've seen my Windows-question Q.10294041 ... it's an ugly little problem I could not solve myself and perhaps you already saw something 'bout that

ZOPPO
Avatar of Lescha

ASKER

Whoa, guys, you lost me somewhere in between...

I know I can use both .def and __declspec(dllexport) methods. I also read the difference between them. However, this DllMain thing I know nothing about... What is it? Is it a must?

And, BTW, I forgot to do that in the beginning of the question. So I do this now:

I turn you attention to the fact that there is a "shell" question for this series, called 'DLL Question Series Overview". If you have general issues to discuss, like management of the series, feel free to do it there.
__declspec(dllexport) tends to be much easier to use, since it is palced in the source code (or .h) and associated with each function to be exported.  It is simpler because you don't have to remember to keep definitions in an additional file up-do-date.  It is also simpler because you don't have to worry about using a decorated name (which is the main reason it was provided).  The exports listed in the module defintion file have more power however, they allow you to define an alias for a function and allow you to export by ordinal.  However, these features usually aren't needed so exporting through a .def isn't used much.
Avatar of Lescha

ASKER

Thanks... Now about this DllMain() thing. What is it? What should it contain? Where should it "sit"? Is it a must?
DllMain() is the "entry point" to the DLL.  it is called when the DLL is loaded and unloaded and also when threads are created and destroyed.  All DLLs must have an entry point procedure, but I believe that VC doesn't not require you to specify one, it will suply one if you don't.  BCB does require you to specify one.
Every program/DLL has an entry point function.
A VC compiled console program uses _mainCRTStartup which in turn calls main().
A VC compiled windowed program uses _WinMainCRTStartup which in turn calls WinMain().
A VC compiled DLL uses _DllMainCRTStartup@12 which in turn calls DllMain(), if it exists.
Of course you can override the default one by using linker flags but that will bypass necessary CRT startup code and will lead to "interesting" results.

The DLL's entry point is called (with appropriate arguments) whenever a process or thread attaches to the DLL or detaches from it.
Avatar of Lescha

ASKER

Wait a minute, what do you mean "both when loaded and unloaded"? How can the same function be used for both loading and unloading the DLL? I assume that on loading I can initialize some variables or run Randomize function, and on unloading free some memory if I used it, but how will I distinguish between the two? Also: what arguments does this function take?
Avatar of Lescha

ASKER

Wait a minute, what do you mean "both when loaded and unloaded"? How can the same function be used for both loading and unloading the DLL? I assume that on loading I can initialize some variables or run Randomize function, and on unloading free some memory if I used it, but how will I distinguish between the two? Also: what arguments does this function take?
Take a look at DllMain in the help.  A parameter is passed to it to indicate the "circumstances" under which it is called.  As alexo indicated, this is not the "real" Dll entry point function.  The compiler suplies the "real" one.  This one initializes or destroys the global variables and initialzies/closes the RTL.  Then it calls the one you supply.
Avatar of Lescha

ASKER

Thanks to all the Experts who participated in answering:

"Zoppo", "nietod", "alexo".

Next part will be posted shortly.