Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Class as a DLL

Class as a DLL

Scheduled Pinned Locked Moved C / C++ / MFC
c++
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    ForNow
    wrote on last edited by
    #1

    Just read an article on using classes as a DLLs following the class a __declspec(dllimport/dllexport) is needed. I was just wondering how MFC does it. On my system the MFC code is located in MFC140.DLL now when I inherit a let’s say CDialog class I don’t specify __declspec(dllimport) Thanks

    L V 2 Replies Last reply
    0
    • F ForNow

      Just read an article on using classes as a DLLs following the class a __declspec(dllimport/dllexport) is needed. I was just wondering how MFC does it. On my system the MFC code is located in MFC140.DLL now when I inherit a let’s say CDialog class I don’t specify __declspec(dllimport) Thanks

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      ForNow wrote:

      I don’t specify __declspec(dllimport)

      No because that declaration will be in the header file. You should take a look at Creating and Using a Dynamic Link Library (C++)[^] for more information. Basically the project that creates the dll needs __declspec(dllexport) and every application the wants to use it needs __declspec(dllimport). This is usually controlled by a #define in the header that goes with the dll. If you create a DLL project in Visual Studio and select the "Export Symbols" checkbox, you get the following in the header:

      // The following ifdef block is the standard way of creating macros which make exporting
      // from a DLL simpler. All files within this DLL are compiled with the WIN32PROJECT2_EXPORTS
      // symbol defined on the command line. This symbol should not be defined on any project
      // that uses this DLL. This way any other project whose source files include this file see
      // WIN32PROJECT2_API functions as being imported from a DLL, whereas this DLL sees symbols
      // defined with this macro as being exported.
      #ifdef WIN32PROJECT2_EXPORTS
      #define WIN32PROJECT2_API __declspec(dllexport)
      #else
      #define WIN32PROJECT2_API __declspec(dllimport)
      #endif

      // This class is exported from the Win32Project2.dll
      class WIN32PROJECT2_API CWin32Project2 {
      public:
      CWin32Project2(void);
      // TODO: add your methods here.
      };

      extern WIN32PROJECT2_API int nWin32Project2;

      WIN32PROJECT2_API int fnWin32Project2(void);

      F 1 Reply Last reply
      0
      • L Lost User

        ForNow wrote:

        I don’t specify __declspec(dllimport)

        No because that declaration will be in the header file. You should take a look at Creating and Using a Dynamic Link Library (C++)[^] for more information. Basically the project that creates the dll needs __declspec(dllexport) and every application the wants to use it needs __declspec(dllimport). This is usually controlled by a #define in the header that goes with the dll. If you create a DLL project in Visual Studio and select the "Export Symbols" checkbox, you get the following in the header:

        // The following ifdef block is the standard way of creating macros which make exporting
        // from a DLL simpler. All files within this DLL are compiled with the WIN32PROJECT2_EXPORTS
        // symbol defined on the command line. This symbol should not be defined on any project
        // that uses this DLL. This way any other project whose source files include this file see
        // WIN32PROJECT2_API functions as being imported from a DLL, whereas this DLL sees symbols
        // defined with this macro as being exported.
        #ifdef WIN32PROJECT2_EXPORTS
        #define WIN32PROJECT2_API __declspec(dllexport)
        #else
        #define WIN32PROJECT2_API __declspec(dllimport)
        #endif

        // This class is exported from the Win32Project2.dll
        class WIN32PROJECT2_API CWin32Project2 {
        public:
        CWin32Project2(void);
        // TODO: add your methods here.
        };

        extern WIN32PROJECT2_API int nWin32Project2;

        WIN32PROJECT2_API int fnWin32Project2(void);

        F Offline
        F Offline
        ForNow
        wrote on last edited by
        #3

        Thanks I was just curious how MFC does it the code is in MFC140.DLL and I don’t see any __declspec(import) When I want to use CDialog Also I assume when I follow theses steps in my .dll I’ll create a .lib Thanks

        L 1 Reply Last reply
        0
        • F ForNow

          Thanks I was just curious how MFC does it the code is in MFC140.DLL and I don’t see any __declspec(import) When I want to use CDialog Also I assume when I follow theses steps in my .dll I’ll create a .lib Thanks

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          The exported labels are not in the dll, as that means nothing to the compiler. They are in one of the header files somewhere in the MFC include folders. So if you find the definition of CDialog it will somehow be defined there.

          F 1 Reply Last reply
          0
          • L Lost User

            The exported labels are not in the dll, as that means nothing to the compiler. They are in one of the header files somewhere in the MFC include folders. So if you find the definition of CDialog it will somehow be defined there.

            F Offline
            F Offline
            ForNow
            wrote on last edited by
            #5

            FYI I did a dependency walker against MFC140.DLL and I got all the entry points N/A meaning without function names

            L L 2 Replies Last reply
            0
            • F ForNow

              FYI I did a dependency walker against MFC140.DLL and I got all the entry points N/A meaning without function names

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Well they will be in there somewhere, but MFC/C++ names get mangled up and are not always easy to recognise. Be that as it may, if the entry points didn't exist then the dll would not be usable. I have not used MFC for many years so cannot actually check any of this. The key point is to ignore the dll and inspect the header files.

              1 Reply Last reply
              0
              • F ForNow

                Just read an article on using classes as a DLLs following the class a __declspec(dllimport/dllexport) is needed. I was just wondering how MFC does it. On my system the MFC code is located in MFC140.DLL now when I inherit a let’s say CDialog class I don’t specify __declspec(dllimport) Thanks

                V Offline
                V Offline
                Victor Nijegorodov
                wrote on last edited by
                #7

                just read about the [Extension DLLs | Microsoft Docs](https://docs.microsoft.com/en-us/cpp/build/extension-dlls)

                1 Reply Last reply
                0
                • F ForNow

                  FYI I did a dependency walker against MFC140.DLL and I got all the entry points N/A meaning without function names

                  L Offline
                  L Offline
                  leon de boer
                  wrote on last edited by
                  #8

                  Exporting from a DLL Using __declspec(dllexport)[^]

                  Many export directives, such as ordinals, NONAME, and PRIVATE, can be made only in a .def file, and there is no way to specify these attributes without a .def file. However, using __declspec(dllexport) in addition to using a .def file does not cause build errors.

                  It's a protection scheme they are exported as privates or ordinals probably so they can avoid ANSI, MBCS, and Unicode issues on the call name.

                  In vino veritas

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups