Visual Studio 2017

How to Create New DLL Project with Visual Studio 2017 Professional Update 2


2017.10.07: created by
Japanese English
To Table of Contents

[Notice] I pointed out the undefined "PROJECTNAME_EXPORTS" constant in Microsoft's "Developer Network (Japanese)". Then, even on Sunday morning (2017.10.8), an appropriate reply was posted in less than 40 minutes, and a report to the development team of Microsoft came up with a movie. The excellent engineer is awesome.


How to Create New DLL Projectwith Visual Studio 2017 Update 2

  1. Select "File" -> "New" -> "Project" -> "Visual C++" -> "Windows desktop" -> "Windows desktop wizards"






  2. Type the project name and click "OK" button.

  3. In "Windows desktop project" window, select "Dynamic Link Library (.dll)" in "Application Type", check out the "Export symbols" option for "additional options".




If you encounter "dllimport .." errors when building the DLL in Visual Studio 2017 Update 2 (version 15.3.5)

[Notice] This bug has been fixed in Visual Studio 2017 version 15.4 released on Oct/11/2017.

The project of DLL contains the files "PROJECTNAME.h" and "PROJECTNAME.cpp". I will explain these as "Example.h" and "Example.cpp".

  1. The contents of "Example.h" and "Example.cpp" at initial state are as follows.
  2. Example.h
    #ifdef EXAMPLE_EXPORTS
    #define EXAMPLE_API __declspec(dllexport)
    #else
    #define EXAMPLE_API __declspec(dllimport)
    #endif
    
    class EXAMPLE_API CExample {
    public:
    	CExample(void);
    };
    
    extern EXAMPLE_API int nExample;
    
    EXAMPLE_API int fnExample(void);
    
    
    Example.cpp (εˆζœŸηŠΆζ…‹)
    #include "stdafx.h"
    #include "Example.h"
    
    
    EXAMPLE_API int nExample=0;
    
    EXAMPLE_API int fnExample(void)
    {
        return 42;
    }
    
    CExample::CExample()
    {
        return;
    }
    
    
  3. "Example.h" will be used in either of the following cases.
      [1] When compiling "Example.cpp" to create "Example.dll"
      [2] When compiling programs that use "Example.dll"
  4. In case of [1], since Visual Studio defines EXAMPLE_EXPORTS when compiling, EXAMPLE_API in "Example.cpp" is treated as __declspec(dllexport) .

    In case of [2], since Visual Studio does not define EXAMPLE_EXPORTS when compiling, EXAMPLE_API in "Example.cpp" is treated as __declspec(dllimport) .

    That is, it is a convenient situation that the same "Example.h" can be used in both [1] and [2].

  5. However, in Visual Studio 2017 Professional Update2, there seems to be a bug in compiling without defining EXAMPLE_EXPORTS even in case of [1].
  6. To avoid this bug, before you include "Example.h" in "Example.cpp", Write "define if EXAMPLE_EXPORTS is not defined".
  7. Example.cpp (to avoid bugs)
    #include "stdafx.h"
    #ifndef EXAMPLE_EXPORTS
    #define EXAMPLE_EXPORTS
    #endif
    #include "Example.h"
    
    
    EXAMPLE_API int nExample=0;
    
    EXAMPLE_API int fnExample(void)
    {
        return 42;
    }
    
    CExample::CExample()
    {
        return;
    }
    
    
  8. Notice that the constant name is "PROJECTNAME_EXPORTS". In the example above, since the project name is "Example", the constant was named EXAMPLE_EXPORTS . Please read and replace it with your own project name.


http://nw.tsuda.ac.jp/