views:

82

answers:

3

What is my main concern is , I am able to write a C++ dll using VC++ . Now the dll is present in the Debug folder.

How can I use my DLL in other C++ Console Application. How to add reference or link the DLL to the application.

Another point, While creating a DLL , The VC++ wizard gives me thre option:

  1. An Empty DLL project
  2. A Simple DLL project
  3. A DLL that Exports some Symbol

Now as per article from CP I have used the 3rd option. (Unable to follow as the dll was used by an MFC app, Some how little advanced at this point of time)

Do I need to always choose the third option? What are the the other two options mean?

+3  A: 

Not completely sure what you questions are but:

It doesn't really matter which option you use it is just a matter of what the wizard does for you; if you use the third option then the wizard creates a bit in your header file that looks like this:

#ifdef TEST_EXPORTS
#define TEST_API __declspec(dllexport)
#else
#define TEST_API __declspec(dllimport)
#endif

The way this works is that in the DLL project TEST_EXPORTS is defined in the compiler options so TEST_API evaluates to dllexport thus telling the linker to export these symbols. If you include this header in another project it defines TEST_API as dllimport which tells the compiler to link to it at run-time in the DLL. This #define method of exporting symbols is widely used and easy to read.

In order to call a function/class inside the DLL you need to export the symbols one of two ways: a) using the __declspec(dllexport) [this seems the more convenient option in almost all cases ]OR b) using a .DEF file in your project

Also wanted to mention that you need to include either the DLL project in your solution for the .exe file OR the .lib generated by the DLL compile.

Elemental
+1  A: 

You can use "A DLL that Exports some Symbol" to learn how Dll project should be built. Once you understand this, use "A Simple DLL project". You can prefer to start always with "A DLL that Exports some Symbol", and change the code generated by Wizard, replacing sample exported class/function/symbol with your own code.

To reference .Dll project from a client project, add .lib file to the client project linker dependencies: Project - Properties - Linker - Input - Additional Dependencies. To ensure that .lib file can be found by linker, add directory where .lib file is placed, to the linker directories list. There are two places this can be done: locally in the client project (Project - Properties - Linker - General - Additional library directories) and globally, for all VC++ projects (Tools - Options - VC++ Directories - Libraries).

The last thing is to ensure that DLL can be loaded by client .exe at runtime. Dll must be in the current directory, executable directory, Windows, system directory, or available through PATH variable.

Alex Farber
+1  A: 

The DLL can be imported by specifying it as a dependency in project settings of the Console application within Visual Studio as described by Alex Farber. You have to make sure that the application is able to find the DLL by placing the DLL at any location specified the PATH variable. You can also load the DLL programmatically within you application using LoadLibrary method (see documentation here http://msdn.microsoft.com/en-us/library/ms684175(VS.85).aspx) and call a method exported within the DLL using funtion GetProcAddress ( refer http://msdn.microsoft.com/en-us/library/ms683212(VS.85).aspx ).

Swapnil