Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

DLL Questions Series, 2

Posted on 2000-02-14
13
Medium Priority
?
340 Views
Last Modified: 2010-04-02
This is the second part of the series.
The Overview can be found at:
http://www.experts-exchange.com/jsp/qShow.jsp?ta=cplusprog&qid=10294044 
The previous parts can be found at:
(1) http://www.experts-exchange.com/jsp/qShow.jsp?ta=cplusprog&qid=10294050 

Using Visual Studio (ver. 6), how do I make a program to use a DLL called MyDll.DLL? The program itself is a pair of text files, one called MyProg.CPP the other MyProg.H.

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.
Use so-and-so functions to link the DLL with the project (or to make the program call the DLL).
Etc.

For now, we shall assume that the files contain simple C++ functions and do not use MFC classes.
0
Comment
Question by:Lescha
[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
  • 6
  • 4
  • 3
13 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 200 total points
ID: 2520026
The program can use the DLL in two ways explicitly and implicitly.

For explicit use (explicit linking).  The program needs to load the DLL using LoadLibrary().  This takes a string that specifies the name (and perhaps the path) of the DLL and returns a handle that can b used to refer to the DLL.  Then the procedure can use GetProcAddress() to obtain pointers to exported procedures (and also data) from the DLL.  These pointers can be used to call procedures in the DLL.  When the program is finished using the DLL, it can call FreeLibrary() to unload the DLL.

continues.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2520076
For implicit linking, the program needs to link to the DLL's export library.  This is a .lib file created when the DLL is linked.  The program can be made to link to this lib file in many ways, it can use a #pragma() to specify the lib to be linked to, it can place the .lib file in the project workspace, or you can make the DLL project a sub-project of the EXE and indicate that the EXE is dependant on the DLL.

In the EXE's source code you need to declare any of the procedures or data that is experted from the DLL and that you wish to use in the EXE.  These procedures/data must be declared as external, since they are not defined in the EXE, this can be done using the standard "extern" keyword, but instead it is usually done using the "_declspec(dllimport)" specification.  For example a procedure defined in the DLL like

__declspec(dllexport) int Increment(int i)
{
   return i + 1;
};

Would be declared like

__declspec(dllimport) int Increment(int i);

A varaible declared in an DLL like

__declspec(dllexport) int X = 5;

would be declared in the EXE like

__declspec(dllimport) int x;

Often DLL authors will create a .h file that defines all the exported items in the DLL, so the EXE can simply include this .h file and not contain the imported item declarations directly.

The DLL author can use conditional compilation so that the same .h file can be used when compiling the DLL itself.  In this case conditional compilation is used so that __declspec(dllimport) is used when the .h is used in code that uses the DLL and __declspec(dllexport) is used when the .h is used in the DLL.

continues.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2520089
For example.

-----Dll's header file.------
#ifndef In_SomeDll
#define SomeDllExport __declspec(dllexport)
#else
#define SomeDllExport __declspec(dllImport)
#endif

SomeDllExport int Increment(int i);

------Dll's source code-----
#define In_SomeDll
#include <SomeDll.h>

int Increment(in i)
{
   return i + 1;
}


-------EXE's source code------
#include <SomeDll.h>

int X = increment(5); // X = 6.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 11

Expert Comment

by:alexo
ID: 2522206
I could never compete with Todd's lightning speed...

Let me just add that:

1) It is a good idea to use a DllMain() function.  The stub generated by the "wizard" looks like this:

    BOOL APIENTRY DllMain(HANDLE hModule, DWORD  reason, LPVOID lpReserved)
    {
        switch (reason)
        {
            case DLL_PROCESS_ATTACH:
            case DLL_THREAD_ATTACH:
            case DLL_THREAD_DETACH:
            case DLL_PROCESS_DETACH:
                break;
        }
        return TRUE;
    }

Customize as needed.


2) The DLL file is linked with the following switches:

    /dll /out:"MyDll.dll"
0
 
LVL 22

Expert Comment

by:nietod
ID: 2522374
>> I could never compete with Todd's lightning speed...
Well, this one wasn't  even close, you are more than 12 hours behind.

>> 2) The DLL file is linked with the following switches:
>> 
>>                   /dll /out:"MyDll.dll"
If you set up the project as a DLL in VC you probably don't have to worry about these switches as then should be set automatically for you.
0
 
LVL 11

Expert Comment

by:alexo
ID: 2522577
>> Well, this one wasn't  even close
Priorities...

>> If you set up the project as a DLL in VC [...]
Indeed.  This is just what goes on "behind the screen".
0
 
LVL 1

Author Comment

by:Lescha
ID: 2523912
Thanks, quite finely tuned answers!

1) Is there any principal difference between the two ways of linking? Is one of them, say, faster and the other more economical?

2) Where, given a smth.dll do I get a smth.lib from? Isn't smth.lib a static, not dynamic, library, anyway?
0
 
LVL 22

Expert Comment

by:nietod
ID: 2524146
Implicit linking by far the easiest to use and most widely used.  The programmer doesn't have to go to extra steps to call a function using implicit linking.  They just call the function just like any othet function and the compiler/linker/and Os all work to make the call succeeed.  With explicit linking, the program has to get a pointer to a function and call the function using the pointer.  There are extra steps involved and the danger that the pointer will be invalid or NULL.  

However, explicit linking is useful for cases where you don't have a .lib file for a DLL, of if you need to call a function that might not be available in all versions of a DLL.

>> given a smth.dll do I get a smth.lib from?
Usually the .lib is made when the .dll is linked.  However if you have a .dll and not coorespondign .lib, there are utilities that can genereate the .lib from the .dll.
0
 
LVL 11

Expert Comment

by:alexo
ID: 2524483
>> explicit linking is useful for cases where you don't have a .lib file for a DLL,
>> or if you  need to call a function that might not be available in all versions of a DLL.

Or the DLL might not be available on all systems.

Another interesting feature is the /delayload option.
0
 
LVL 1

Author Comment

by:Lescha
ID: 2528116
What about this 'pragma' thing? I read about it in MSDN but can't connect it with the DLLs.

Also: __declspec(dllImport) is used with implicit linking (i.e. via the LIB file), right? What utilities are there to create LIB from DLL?

Another thing: does using LIB instead of DLL not go against the philosophy of DLLs? I thought LIB is a static library, or do I have it wrong?
0
 
LVL 22

Expert Comment

by:nietod
ID: 2528261
>> What about this 'pragma' thing?
#pragma comment(lib,"lib path and name")
links the program to the specified lib file.

>> __declspec(dllImport) is used with implicit linking
yes.

>> What utilities are there
>> to create LIB from DLL?
I don't know any by name.

>> I thought LIB is a
>> static library, or do I have it wrong?
No that is right.  When you declare a function as external (meaning the function's code is not defined in the current translation unit) and call that function, the compiler places a "request" in the object code that the call code be generated at link time when the function's location is finally known.  When the program is linked, the final code for the call must be generated.  If no function is found to link to the program cannot be completed.  So the static library satisfies the link request.  it contains stub functions that the calls to DLL functions link to.  There are nto real funcitons, just stubs that at 6 bytes long and contain data that the program loader will use to make the dynamic link (the data indicates which DLL function is to be called.)  when the DLL is loaded, the stub is altered (fixed up) so it contains a jump instruction to the proper function in the DLL..  So a call to the DLL function goes to the stub and then jumps to the right location.
0
 
LVL 11

Expert Comment

by:alexo
ID: 2528396
search for 'implib'
0
 
LVL 1

Author Comment

by:Lescha
ID: 2529928
Thanks to all the Experts who participated in answering:

"nietod", "alexo".

Next part will be posted shortly.

0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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 tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

721 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