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. ATL / WTL / STL
  4. How To Add a Dll TO MFC Application in visual Studio

How To Add a Dll TO MFC Application in visual Studio

Scheduled Pinned Locked Moved ATL / WTL / STL
csharpc++visual-studiotutorialquestion
14 Posts 4 Posters 30 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.
  • L Lost User

    Well that just indicates that you have a bad address somewhere in the code. You need to use your debugger to find out where it occurs and why.

    U Offline
    U Offline
    User 13439554
    wrote on last edited by
    #5

    you mean that i have loaded .dll file correctly and the error is because of somewhat like syntax error? but i have exactly copied the name of function in dll. i dont know why this error occurs

    L 1 Reply Last reply
    0
    • U User 13439554

      Hello everybody i have a mfc application project in Visual studio 2010. it is a GUI infact. i want to get communication with a mini circuit signal generator device. the device has a dll file named "mcl_gen64.dll i want to use functions of that dll in my code but i dont have any idea how to do this any idea please?

      J Offline
      J Offline
      Jochen Arndt
      wrote on last edited by
      #6

      To use functions from a DLL you have two choices: Early and late binding. Early binding: Link your application with the DLL by using the #import directive in one file (usually a source file using functions from the DLL) specifying the file name (usually without extension or with .lib), or add the .lib file to your project settings (Linker - Input). Include the header file and call the functions defined in that file. Late binding: This is only necessary if the DLL might not be present when your application is executed or you do not have a .lib file. Then use LoadLibrary and GetProcAddress like in your above example. But check both return values to be not NULL. Have a look at the header file (if present), to know how to define the function prototypes. Example:

      typedef int (__stdcall *ConnectByAddressfuncPtr)(short Addr);

      ConnectByAddressfuncPtr LibMainConnectByAddress = NULL;
      HMODULE hLib = LoadLibrary(_T("mcl_gen.dll"));
      if (hLib)
      LibMainConnectByAddress = (ConnectByAddressfuncPtr)GetProcAddress(hLib,"ConnectByAddress");
      if (LibMainConnectByAddress)
      LibMainConnectByAddress(0x01);

      Note that I have initialised the function pointer with NULL and checked it before calling the function. Note also the __stdcall in the prototype declaration. It defines the calling convention used by the DLL. You have to check which is used (by inspecting the header file or asking the provider). __stdcall is common but it might be also __cdecl.

      U 1 Reply Last reply
      0
      • U User 13439554

        you mean that i have loaded .dll file correctly and the error is because of somewhat like syntax error? but i have exactly copied the name of function in dll. i dont know why this error occurs

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

        I have no idea why the error occurred, beyond saying that is is caused by a bad address reference somewhere. And the only way to find out where it occurred is by using the debugger. Are you certain that the parameter 0x01 that you send to the ConnectByAddress function is valid? Check the documentation to see what that function is supposed to do, and what it is supposed to return.

        1 Reply Last reply
        0
        • J Jochen Arndt

          To use functions from a DLL you have two choices: Early and late binding. Early binding: Link your application with the DLL by using the #import directive in one file (usually a source file using functions from the DLL) specifying the file name (usually without extension or with .lib), or add the .lib file to your project settings (Linker - Input). Include the header file and call the functions defined in that file. Late binding: This is only necessary if the DLL might not be present when your application is executed or you do not have a .lib file. Then use LoadLibrary and GetProcAddress like in your above example. But check both return values to be not NULL. Have a look at the header file (if present), to know how to define the function prototypes. Example:

          typedef int (__stdcall *ConnectByAddressfuncPtr)(short Addr);

          ConnectByAddressfuncPtr LibMainConnectByAddress = NULL;
          HMODULE hLib = LoadLibrary(_T("mcl_gen.dll"));
          if (hLib)
          LibMainConnectByAddress = (ConnectByAddressfuncPtr)GetProcAddress(hLib,"ConnectByAddress");
          if (LibMainConnectByAddress)
          LibMainConnectByAddress(0x01);

          Note that I have initialised the function pointer with NULL and checked it before calling the function. Note also the __stdcall in the prototype declaration. It defines the calling convention used by the DLL. You have to check which is used (by inspecting the header file or asking the provider). __stdcall is common but it might be also __cdecl.

          U Offline
          U Offline
          User 13439554
          wrote on last edited by
          #8

          Hi. thank you for your answer. i dont have any .lib file i tried your code but "LibMainConnectByAddress" still remains NULL. so the code skips LibMainConnectByAddress(0x01); do you know why? thank you again

          J L 2 Replies Last reply
          0
          • U User 13439554

            Hi. thank you for your answer. i dont have any .lib file i tried your code but "LibMainConnectByAddress" still remains NULL. so the code skips LibMainConnectByAddress(0x01); do you know why? thank you again

            J Offline
            J Offline
            Jochen Arndt
            wrote on last edited by
            #9

            All I can suggest is to check if the DLL exports the function "ConnectByAddress" (e.g. using the Dependency Walker (depends.exe) Home Page[^] or executing dumpbin /exports path_to_dll on the command line; dumpbin is in the VC/bin folder) and trying it with different calling conventions like __cdecl. I would use the Dependency Walker because it shows also the calling convention for the exported functions.

            1 Reply Last reply
            0
            • U User 13439554

              Hi. thank you for your answer. i dont have any .lib file i tried your code but "LibMainConnectByAddress" still remains NULL. so the code skips LibMainConnectByAddress(0x01); do you know why? thank you again

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

              Member 13471493 wrote:

              "LibMainConnectByAddress" still remains NULL.

              You omitted to mention that in your reply to me. If that function pointer is null then it will cause the error you have seen. You need to check the function name that you are trying to get the address for, does it actually exist in the dll?

              U 1 Reply Last reply
              0
              • L Lost User

                Member 13471493 wrote:

                "LibMainConnectByAddress" still remains NULL.

                You omitted to mention that in your reply to me. If that function pointer is null then it will cause the error you have seen. You need to check the function name that you are trying to get the address for, does it actually exist in the dll?

                U Offline
                U Offline
                User 13439554
                wrote on last edited by
                #11

                Yes it Does. i tried others functions that exist in dll too. but i got the same error. i am sure about syntax and existence of these functions in the dll.

                L J 2 Replies Last reply
                0
                • U User 13439554

                  Yes it Does. i tried others functions that exist in dll too. but i got the same error. i am sure about syntax and existence of these functions in the dll.

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

                  But are you sure that the value you are sending to the function is correct?

                  1 Reply Last reply
                  0
                  • U User 13439554

                    Yes it Does. i tried others functions that exist in dll too. but i got the same error. i am sure about syntax and existence of these functions in the dll.

                    J Offline
                    J Offline
                    Jochen Arndt
                    wrote on last edited by
                    #13

                    Have you used the Dependency Walker or dumpbin meanwhile? The exported names may be mangled (see Name mangling - Wikipedia[^] ). Then you have to pass the mangled name to GetProcAddress.

                    1 Reply Last reply
                    0
                    • U User 13439554

                      Hello everybody i have a mfc application project in Visual studio 2010. it is a GUI infact. i want to get communication with a mini circuit signal generator device. the device has a dll file named "mcl_gen64.dll i want to use functions of that dll in my code but i dont have any idea how to do this any idea please?

                      M Offline
                      M Offline
                      Michael Haephrati
                      wrote on last edited by
                      #14

                      This article explains it. Basically you use LoadLibrary with the DLL's path given to it. The rest is mapping the functions you will need to call.

                      - Michael Haephrati מיכאל האפרתי

                      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