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. Doubt in DLL

Doubt in DLL

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
9 Posts 5 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.
  • H Offline
    H Offline
    Hungry Developer
    wrote on last edited by
    #1

    Hi, I created a MFC Regualr DLL using shared MFC. DLL contains 4 functions which are exported using __declspec(dllexport). Now in the client application i tried to make use of this DLL. two of exported functions are imported using __declspec(dllimport). They are working fine. Now i tried to make use of third function using LoadLibrary and GetProcAddress. This time i am getting NULL pointer instead of function pointer. Again i placed extern "C" while exporting the function and tried to make use of it.This time i am getting the function pointer and every thing is fine. Usage of extern "C" makes it work fine. Can any one explain me what actually happening here?:confused: Thanks in advance...:rose:

    C R C T 4 Replies Last reply
    0
    • H Hungry Developer

      Hi, I created a MFC Regualr DLL using shared MFC. DLL contains 4 functions which are exported using __declspec(dllexport). Now in the client application i tried to make use of this DLL. two of exported functions are imported using __declspec(dllimport). They are working fine. Now i tried to make use of third function using LoadLibrary and GetProcAddress. This time i am getting NULL pointer instead of function pointer. Again i placed extern "C" while exporting the function and tried to make use of it.This time i am getting the function pointer and every thing is fine. Usage of extern "C" makes it work fine. Can any one explain me what actually happening here?:confused: Thanks in advance...:rose:

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      Because your two first functions probably don't have any arguments. When you are using extern "C", it means that the function should be exported as a C function. In C, there is no function overloading so a function is identified only by its name. In C++, you can overload a function (a function with the same name can have different arguments). Which means that the name is not enough to identify the function. Thus, in C++ is introduced what is called name mangling: the types (and number) of parameters of the function are also used to identify the function. When you use GetProcAddress, you need to use the mangled name of the function (if there's no argument, the mangled name is the same as the function name). So, by using a C function, you avoid the name mangling. Hope it makes sense.

      Cédric Moonen Software developer
      Charting control [v1.4]

      1 Reply Last reply
      0
      • H Hungry Developer

        Hi, I created a MFC Regualr DLL using shared MFC. DLL contains 4 functions which are exported using __declspec(dllexport). Now in the client application i tried to make use of this DLL. two of exported functions are imported using __declspec(dllimport). They are working fine. Now i tried to make use of third function using LoadLibrary and GetProcAddress. This time i am getting NULL pointer instead of function pointer. Again i placed extern "C" while exporting the function and tried to make use of it.This time i am getting the function pointer and every thing is fine. Usage of extern "C" makes it work fine. Can any one explain me what actually happening here?:confused: Thanks in advance...:rose:

        R Offline
        R Offline
        Rajkumar R
        wrote on last edited by
        #3

        __declspec(dllexport/dllimport) uses C++ naming convention, so that the name exported from the dll is name mangled with C++ language convention, so when you try to get the address of the exported function using GetProcAddress(), the function name string won't match with name of the exported function which is decorted with c++ style, when using extern "C" linkage specifier naming convention used is that of C language which has the same undecorated name as that of the function.

        H 1 Reply Last reply
        0
        • H Hungry Developer

          Hi, I created a MFC Regualr DLL using shared MFC. DLL contains 4 functions which are exported using __declspec(dllexport). Now in the client application i tried to make use of this DLL. two of exported functions are imported using __declspec(dllimport). They are working fine. Now i tried to make use of third function using LoadLibrary and GetProcAddress. This time i am getting NULL pointer instead of function pointer. Again i placed extern "C" while exporting the function and tried to make use of it.This time i am getting the function pointer and every thing is fine. Usage of extern "C" makes it work fine. Can any one explain me what actually happening here?:confused: Thanks in advance...:rose:

          C Offline
          C Offline
          CPallini
          wrote on last edited by
          #4

          If you don't use extern "C" the name of the function is mangled the C++ way (you can see this effect viewing your DLL with Dependecy Walker). That means you need to pass the mangled name to GetProcAddress(). On the other hand, using extern "C" makes the function have C linkage, hence, roughly speaking, without mangling. :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

          H 1 Reply Last reply
          0
          • R Rajkumar R

            __declspec(dllexport/dllimport) uses C++ naming convention, so that the name exported from the dll is name mangled with C++ language convention, so when you try to get the address of the exported function using GetProcAddress(), the function name string won't match with name of the exported function which is decorted with c++ style, when using extern "C" linkage specifier naming convention used is that of C language which has the same undecorated name as that of the function.

            H Offline
            H Offline
            Hungry Developer
            wrote on last edited by
            #5

            Hi, It is very clear now. Thanks for your reply. :)

            1 Reply Last reply
            0
            • C CPallini

              If you don't use extern "C" the name of the function is mangled the C++ way (you can see this effect viewing your DLL with Dependecy Walker). That means you need to pass the mangled name to GetProcAddress(). On the other hand, using extern "C" makes the function have C linkage, hence, roughly speaking, without mangling. :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

              H Offline
              H Offline
              Hungry Developer
              wrote on last edited by
              #6

              HI, I got the point. Thanks for your reply. :)

              C 1 Reply Last reply
              0
              • H Hungry Developer

                HI, I got the point. Thanks for your reply. :)

                C Offline
                C Offline
                CPallini
                wrote on last edited by
                #7

                :)

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

                1 Reply Last reply
                0
                • H Hungry Developer

                  Hi, I created a MFC Regualr DLL using shared MFC. DLL contains 4 functions which are exported using __declspec(dllexport). Now in the client application i tried to make use of this DLL. two of exported functions are imported using __declspec(dllimport). They are working fine. Now i tried to make use of third function using LoadLibrary and GetProcAddress. This time i am getting NULL pointer instead of function pointer. Again i placed extern "C" while exporting the function and tried to make use of it.This time i am getting the function pointer and every thing is fine. Usage of extern "C" makes it work fine. Can any one explain me what actually happening here?:confused: Thanks in advance...:rose:

                  T Offline
                  T Offline
                  toxcct
                  wrote on last edited by
                  #8

                  one last solution to avoid using "extern C" is to put a .DEF file in your dll project, which tells to export a symbol as you name it in the file.

                  [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                  H 1 Reply Last reply
                  0
                  • T toxcct

                    one last solution to avoid using "extern C" is to put a .DEF file in your dll project, which tells to export a symbol as you name it in the file.

                    [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                    H Offline
                    H Offline
                    Hungry Developer
                    wrote on last edited by
                    #9

                    Hi, Thaks for your solution. :)

                    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