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. How to conditional call a library

How to conditional call a library

Scheduled Pinned Locked Moved C / C++ / MFC
questiontutorial
7 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.
  • T Offline
    T Offline
    thanh_bkhn
    wrote on last edited by
    #1

    Hi all, I have a program that calls magnification.lib in windows 7, windows 8. (I'm calling #include magnification.h) But we know this library doesn't exist in windows XP. How can I ignore this library when opening the application in windows XP?

    _ D C 3 Replies Last reply
    0
    • T thanh_bkhn

      Hi all, I have a program that calls magnification.lib in windows 7, windows 8. (I'm calling #include magnification.h) But we know this library doesn't exist in windows XP. How can I ignore this library when opening the application in windows XP?

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      Create function pointers in a header file for all the magnification APIs that you're going to use. Check the OS version and if Vista or above, call LoadLibrary on Magnification.dll. After this call GetProcAddress on the function to initialize the function pointer. So the header file could look like this -

      typedef BOOL (*PFN_MAG_INITIALIZE)(void);

      From the main program, you could have a call to some function called InitMagnification. In the InitMagnification function, do the dynamic loading after checking the OS version.

      PFN_MAG_INITIALIZE MagInitialize = NULL;

      if (osVersion >= "VISTA") // Consider this as pseudo-code
      {
      HMODULE hmod = LoadLibrary(_T("Magnification.dll"));

      MagInitialize = GetProcAddress(hmod, "MagInitialize");
      

      }

      In the source where this function is called, do the checking as follows -

      if (MagInitialize)
      {
      MagInitalize();
      }

      I have omitted error checking, which you have to do.

      «_Superman_»  _I love work. It gives me something to do between weekends.

      _Microsoft MVP (Visual C++) (October 2009 - September 2013)

      Polymorphism in C

      T 1 Reply Last reply
      0
      • T thanh_bkhn

        Hi all, I have a program that calls magnification.lib in windows 7, windows 8. (I'm calling #include magnification.h) But we know this library doesn't exist in windows XP. How can I ignore this library when opening the application in windows XP?

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        thanh_bkhn wrote:

        But we know this library doesn't exist in windows XP.
        How can I ignore this library when opening the application in windows XP?

        Sounds like you need to read up on implicit vs. explicit linking.

        "One man's wage rise is another man's price increase." - Harold Wilson

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

        T 1 Reply Last reply
        0
        • _ _Superman_

          Create function pointers in a header file for all the magnification APIs that you're going to use. Check the OS version and if Vista or above, call LoadLibrary on Magnification.dll. After this call GetProcAddress on the function to initialize the function pointer. So the header file could look like this -

          typedef BOOL (*PFN_MAG_INITIALIZE)(void);

          From the main program, you could have a call to some function called InitMagnification. In the InitMagnification function, do the dynamic loading after checking the OS version.

          PFN_MAG_INITIALIZE MagInitialize = NULL;

          if (osVersion >= "VISTA") // Consider this as pseudo-code
          {
          HMODULE hmod = LoadLibrary(_T("Magnification.dll"));

          MagInitialize = GetProcAddress(hmod, "MagInitialize");
          

          }

          In the source where this function is called, do the checking as follows -

          if (MagInitialize)
          {
          MagInitalize();
          }

          I have omitted error checking, which you have to do.

          «_Superman_»  _I love work. It gives me something to do between weekends.

          _Microsoft MVP (Visual C++) (October 2009 - September 2013)

          Polymorphism in C

          T Offline
          T Offline
          thanh_bkhn
          wrote on last edited by
          #4

          Thank you very much. After posting this question, I did exactly what you told me to do. Thank you again

          1 Reply Last reply
          0
          • D David Crow

            thanh_bkhn wrote:

            But we know this library doesn't exist in windows XP.
            How can I ignore this library when opening the application in windows XP?

            Sounds like you need to read up on implicit vs. explicit linking.

            "One man's wage rise is another man's price increase." - Harold Wilson

            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

            "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

            T Offline
            T Offline
            thanh_bkhn
            wrote on last edited by
            #5

            Thank you. I performed an explicitly link to my DLL.

            1 Reply Last reply
            0
            • T thanh_bkhn

              Hi all, I have a program that calls magnification.lib in windows 7, windows 8. (I'm calling #include magnification.h) But we know this library doesn't exist in windows XP. How can I ignore this library when opening the application in windows XP?

              C Offline
              C Offline
              chaau
              wrote on last edited by
              #6

              As an alternative, you could use delay load dll. In this case you do not need to change the code you have already written (related to statically linking to the library), all you need to do is just do not call chunk of codes related to magnification at all. Saves you a lot of annoying LoadLibrary/GetProcAddress calls. However, it looks like you have already implemented it. So, just learn about this, so that you could use in future

              T 1 Reply Last reply
              0
              • C chaau

                As an alternative, you could use delay load dll. In this case you do not need to change the code you have already written (related to statically linking to the library), all you need to do is just do not call chunk of codes related to magnification at all. Saves you a lot of annoying LoadLibrary/GetProcAddress calls. However, it looks like you have already implemented it. So, just learn about this, so that you could use in future

                T Offline
                T Offline
                thanh_bkhn
                wrote on last edited by
                #7

                Thank you for introducing me this. I found this way is more interesting than using LoadLibrary, but I still prefer to the old method, because it helps me to compile the program even on Windows XP

                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