PDF download Download Article PDF download Download Article

DLL files are dynamic-linked library files written and controlled with C++. DLLs make sharing, storing, and saving your code simple. This wikiHow will show you how to create a DLL file with Visual Studio , the Windows application, or Visual Studio for Mac. Make sure you have “Desktop Development with C++” checked when you install. If you already have Visual Studio but didn’t check that box, you can run the installer again to make sure you do.

Making and Using a Dynamic Link Library

  • To make a DLL, open Visual Studio and start a new project for C++. Set the project type as Library .
  • Click Dynamic-link Library (DLL) and create it. Add the code from the Microsoft website.
  • Add a CPP file to the DLL, and then select Build Solution .
  1. You can find this in your Start Menu or Applications folder. Since a DLL is a library of information, it is only one piece of a project, and usually requires an accompanying app to access it.
  2. You’ll find this either above the project space (Windows) or along the top of your screen (Macs). [1]
    Advertisement
  3. The “Create a New Project” dialog box will pop up. [2]
  4. These will filter what kinds of project templates appear.
    • Click Language to get a drop-down menu and click C++ .
  5. [3]
  6. Your choice will highlight blue. Click Next to continue. [4]
  7. For example, type “MathLibrary” in the box for a sample name. [5]
  8. The DLL project is created. [6]
  9. You can do this by clicking “ Add New Item ” from “ Project ” in the menu bar.
    • Select Visual C++ from the left menu of the dialog box.
    • Select Header file (.h) from the center of the dialog box.
    • Type the name as “MathLibrary.h” in the name field below the menu choices.
    • Click Add to generate the blank header file.
    •  // MathLibrary.h - Contains declarations of math functions 
       #pragma once 
       #ifdef MATHLIBRARY_EXPORTS 
       #define MATHLIBRARY_API __declspec(dllexport) 
       #else 
       #define MATHLIBRARY_API __declspec(dllimport) 
       #endif 
       // The Fibonacci recurrence relation describes a sequence F 
       // where F(n) is { n = 0, a 
       //               { n = 1, b 
       //               { n > 1, F(n-2) + F(n-1) 
       // for some initial integral values a and b. 
       // If the sequence is initialized F(0) = 1, F(1) = 1, 
       // then this relation produces the well-known Fibonacci 
       // sequence: 1, 1, 2, 3, 5, 8, 13, 21, 34, ... 
       // Initialize a Fibonacci relation sequence 
       // such that F(0) = a, F(1) = b. 
       // This function must be called before any other function. 
       extern 
       "C" 
       MATHLIBRARY_API 
       void 
       fibonacci_init 
       ( 
       const 
       unsigned 
       long 
       long 
       a 
       , 
       const 
       unsigned 
       long 
       long 
       b 
       ); 
       // Produce the next value in the sequence. 
       // Returns true on success and updates current value and index; 
       // false on overflow, leaves current value and index unchanged. 
       extern 
       "C" 
       MATHLIBRARY_API 
       bool 
       fibonacci_next 
       (); 
       // Get the current value in the sequence. 
       extern 
       "C" 
       MATHLIBRARY_API 
       unsigned 
       long 
       long 
       fibonacci_current 
       (); 
       // Get the position of the current value in the sequence. 
       extern 
       "C" 
       MATHLIBRARY_API 
       unsigned 
       fibonacci_index 
       (); 
      
    • This is sample code provided from the Microsoft help website.
  10. You can do this by clicking Add New Item from “Project” in the menu bar.
    • Select “ Visual C++ ” from the left menu of the dialog box.
    • Select “ C++ File (.cpp) ” from the center of the dialog box.
    • Type the name as “MathLibrary.cpp” in the name field below the menu choices.
    • Click Add to generate the blank file.
    •  // MathLibrary.cpp : Defines the exported functions for the DLL. 
       #include 
       "stdafx.h" // use pch.h in Visual Studio 2019 
        
       #include 
       <utility> 
        
       #include 
       <limits.h> 
        
       #include 
       "MathLibrary.h" 
        
       // DLL internal state variables: 
       static 
       unsigned 
       long 
       long 
       previous_ 
       ; 
       // Previous value, if any 
       static 
       unsigned 
       long 
       long 
       current_ 
       ; 
       // Current sequence value 
       static 
       unsigned 
       index_ 
       ; 
       // Current seq. position 
       // Initialize a Fibonacci relation sequence 
       // such that F(0) = a, F(1) = b. 
       // This function must be called before any other function. 
       void 
       fibonacci_init 
       ( 
       const 
       unsigned 
       long 
       long 
       a 
       , 
       const 
       unsigned 
       long 
       long 
       b 
       ) 
       { 
       index_ 
       = 
       0 
       ; 
       current_ 
       = 
       a 
       ; 
       previous_ 
       = 
       b 
       ; 
       // see special case when initialized 
       } 
       // Produce the next value in the sequence. 
       // Returns true on success, false on overflow. 
       bool 
       fibonacci_next 
       () 
       { 
       // check to see if we'd overflow result or position 
       if 
       (( 
       ULLONG_MAX 
       - 
       previous_ 
       < 
       current_ 
       ) 
       || 
       ( 
       UINT_MAX 
       == 
       index_ 
       )) 
       { 
       return 
       false 
       ; 
       } 
       // Special case when index == 0, just return b value 
       if 
       ( 
       index_ 
       > 
       0 
       ) 
       { 
       // otherwise, calculate next sequence value 
       previous_ 
       += 
       current_ 
       ; 
       } 
       std 
       :: 
       swap 
       ( 
       current_ 
       , 
       previous_ 
       ); 
       ++ 
       index_ 
       ; 
       return 
       true 
       ; 
       } 
       // Get the current value in the sequence. 
       unsigned 
       long 
       long 
       fibonacci_current 
       () 
       { 
       return 
       current_ 
       ; 
       } 
       // Get the current index position in the sequence. 
       unsigned 
       fibonacci_index 
       () 
       { 
       return 
       index_ 
       ; 
       } 
      
    • This is sample code provided from the Microsoft help website.
  11. You’ll find this either above the project space (Windows) or along the top of your screen (Macs).
  12. After you click that, you should see text similar to this:
       1 
       >------ 
       Build 
       started 
       : 
       Project 
       : 
       MathLibrary 
       , 
       Configuration 
       : 
       Debug 
       Win32 
       
      1 > MathLibrary . cpp 1 > dllmain . cpp 1 > Generating Code ... 1 > Creating library C : \ Users \ username \ Source \ Repos \ MathLibrary \ Debug \ MathLibrary . lib and object C : \ Users \ username \ Source \ Repos \ MathLibrary \ Debug \ MathLibrary . exp 1 > MathLibrary . vcxproj -> C : \ Users \ username \ Source \ Repos \ MathLibrary \ Debug \ MathLibrary . dll 1 > MathLibrary . vcxproj -> C : \ Users \ username \ Source \ Repos \ MathLibrary \ Debug \ MathLibrary . pdb ( Partial PDB ) ========== Build : 1 succeeded , 0 failed , 0 up - to - date , 0 skipped ==========
    • If your DLL creation was successful, you'll see that here. If there was an error, it will be listed here for you to fix.
  13. Advertisement

Expert Q&A

Ask a Question
      Advertisement

      Tips

      Submit a Tip
      All tip submissions are carefully reviewed before being published
      Name
      Please provide your name and last initial
      Thanks for submitting a tip for review!

      About This Article

      Article Summary X

      1. Open Visual Studio.
      2. Open a new Dynamic-link library (DLL) project.
      3. Add a header file.
      4. Add a CPP file.
      5. Check to see if the library works.

      Did this summary help you?
      Thanks to all authors for creating a page that has been read 147,552 times.

      Is this article up to date?

      Advertisement