·  MVS 2008 – C++ – Create – DLL

 

- Info:

  - This tutorial shows how to use MVS 2008 and C++ to create Dynamic Liibrary DLL.

  - It also shows how to create C++ Console Application which calls that DLL.

  - DLL needs to be in te same directory as executable or Environment Variable Path needs to point to directory with DLL.

 

- Create .lib:

  - Create C++ File Test.c

  - RC on MyProject – Properties... – Configuration Properties – General –

    Project Defaults – Configuration Type: Dynamic Library (.dll) – OK

  - Debug – Start Without Debugging Ctrl+F5 (It should ask you to specify executable file)

 

    Test.h

    class Test {

    public:

      static __declspec(dllexport) void hello ();

             __declspec(dllexport) void hello2();

                                   void hello3();

    private:

             void hidden();

    };

 

    Test.cpp

    #include <iostream>   //cout

    #include "Test.h"

    

    using namespace std;

    

    void Test::hello () { cout << "Hello from C++ DLL!"        << endl; }

    void Test::hello2() { cout << "Hello fom object function!" << endl; }

    void Test::hello3() { cout << "Not exported!"              << endl; }

    void Test::hidden() { cout << "I am hidden!"               << endl; }

 

 

- Create Test Application:

  - Create C++ Project MyProject2

  - Create C++ File Test.c

  - RC on MyProject2 – Properties – Configuration Properties – C/C++ – General –

    Additional Include Directories: D:\Installed\Programming\Workspaces\VisualStudio\Solutions\MySolution\MyProject       OK

    (Adds Compiler parameter: /I "D:\Installed\Programming\Workspaces\VisualStudio\Solutions\MySolution\MyProject")

  - RC on MyProject2 – References... – Add New Reference... – MyProject – OK – OK

    (Adds Linker parameter: "..\debug\myproject.lib")

  - Debug – Start Without Debugging Ctrl+F5

 

    Test.cpp

    #include <stdio.h>  

    #include <Test.h>

    

    int main() {

      Test::hello();

      Test test;

           test.hello2();

         //test.hello3();

         //test.hidden(); //Can't access private members.

      return 0;

    }