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. Calling DLL function that is not imported(MFC and WinMain)

Calling DLL function that is not imported(MFC and WinMain)

Scheduled Pinned Locked Moved C / C++ / MFC
c++visual-studioquestion
5 Posts 3 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.
  • S Offline
    S Offline
    sawerr
    wrote on last edited by
    #1

    Hi We know that, WinMain is buried in the MFC Framework. OK but how can Windows call it? I opened MFC project in VC++ and tried to find out how it imports the dll(Mfc90ud.dll) and functions in that dll. But i couldn't find it. I also disassembled the mfc90ud.dll but there is no function like WinMain in exported function list. Function names are just numbers. So, I have 2 questions. 1-) How does VS knows that Mfc90ud.dll must be imported and Windows know that it must call a function in a dll that is imported by EXE? 2-) If i import a dll and although i do not use one of the imported function in that dll, OS can still call that function? Or OS can only call imported dll functions which are used by exe?? If the function doesn't import by exe, it is never be called? Thanks...

    S R 2 Replies Last reply
    0
    • S sawerr

      Hi We know that, WinMain is buried in the MFC Framework. OK but how can Windows call it? I opened MFC project in VC++ and tried to find out how it imports the dll(Mfc90ud.dll) and functions in that dll. But i couldn't find it. I also disassembled the mfc90ud.dll but there is no function like WinMain in exported function list. Function names are just numbers. So, I have 2 questions. 1-) How does VS knows that Mfc90ud.dll must be imported and Windows know that it must call a function in a dll that is imported by EXE? 2-) If i import a dll and although i do not use one of the imported function in that dll, OS can still call that function? Or OS can only call imported dll functions which are used by exe?? If the function doesn't import by exe, it is never be called? Thanks...

      S Offline
      S Offline
      sgenie68
      wrote on last edited by
      #2

      Normally WinMain is not exported in DLLs. What happens is that WinMain is not called directly by the loader (and main(), likewise). The code that calls it, is a bootloading code, which is addressed by a DLL/EXE entry point. This code does all the initialization and at the end of it calls into WinMain/main(), the address of which is known during compilation.

      1 Reply Last reply
      0
      • S sawerr

        Hi We know that, WinMain is buried in the MFC Framework. OK but how can Windows call it? I opened MFC project in VC++ and tried to find out how it imports the dll(Mfc90ud.dll) and functions in that dll. But i couldn't find it. I also disassembled the mfc90ud.dll but there is no function like WinMain in exported function list. Function names are just numbers. So, I have 2 questions. 1-) How does VS knows that Mfc90ud.dll must be imported and Windows know that it must call a function in a dll that is imported by EXE? 2-) If i import a dll and although i do not use one of the imported function in that dll, OS can still call that function? Or OS can only call imported dll functions which are used by exe?? If the function doesn't import by exe, it is never be called? Thanks...

        R Offline
        R Offline
        Rajesh R Subramanian
        wrote on last edited by
        #3

        sawerr wrote:

        We know that, WinMain is buried in the MFC Framework. OK but how can Windows call it?

        The linker tries to locate and execute an entry-point in your code, which may be dependent on the type of application you are building: WinMain (or wWinMain if built for Unicode) or main (or wmain if built for Unicode). The former is for windowed applications and the latter is for console applications. If no entry point is defined in your application, you will get an undeclared identifier from the linker.

        sawerr wrote:

        If i import a dll and although i do not use one of the imported function in that dll, OS can still call that function? Or OS can only call imported dll functions which are used by exe?? If the function doesn't import by exe, it is never be called?

        That depends. There are DLLs that you may not reference directly from your code but Windows will load and execute code from those libraries. This is because some of the functions in those libraries may be indirectly referenced. Other than that, only what you are calling from your code will be loaded or executed. The fact is that this is a larger topic to be discussed in a thread like this. If you really are wanting to read up on how programs work and how Windows work, I'll recommend the book by Jeffrey Richter and Christopher Nasarre (every edition of the book gets a new name. The latest one is called Windows Via C/C++).

        It is a crappy thing, but it's life -^ Carlo Pallini

        S 1 Reply Last reply
        0
        • R Rajesh R Subramanian

          sawerr wrote:

          We know that, WinMain is buried in the MFC Framework. OK but how can Windows call it?

          The linker tries to locate and execute an entry-point in your code, which may be dependent on the type of application you are building: WinMain (or wWinMain if built for Unicode) or main (or wmain if built for Unicode). The former is for windowed applications and the latter is for console applications. If no entry point is defined in your application, you will get an undeclared identifier from the linker.

          sawerr wrote:

          If i import a dll and although i do not use one of the imported function in that dll, OS can still call that function? Or OS can only call imported dll functions which are used by exe?? If the function doesn't import by exe, it is never be called?

          That depends. There are DLLs that you may not reference directly from your code but Windows will load and execute code from those libraries. This is because some of the functions in those libraries may be indirectly referenced. Other than that, only what you are calling from your code will be loaded or executed. The fact is that this is a larger topic to be discussed in a thread like this. If you really are wanting to read up on how programs work and how Windows work, I'll recommend the book by Jeffrey Richter and Christopher Nasarre (every edition of the book gets a new name. The latest one is called Windows Via C/C++).

          It is a crappy thing, but it's life -^ Carlo Pallini

          S Offline
          S Offline
          sawerr
          wrote on last edited by
          #4

          Thanks for answers. I know that loader must fill IAT with imported functions. If I don't import a function so function can't be placed in IAT. So even this function is exported from dll, Windows can't call it. But i'm not sure about that so i started that thread. I understand that winmain(or whatever) isn't added to import section by exe, this is special case for entry point. A function can be called without adding IAT is entry point. This is valid just for entry point. Is that right?

          S 1 Reply Last reply
          0
          • S sawerr

            Thanks for answers. I know that loader must fill IAT with imported functions. If I don't import a function so function can't be placed in IAT. So even this function is exported from dll, Windows can't call it. But i'm not sure about that so i started that thread. I understand that winmain(or whatever) isn't added to import section by exe, this is special case for entry point. A function can be called without adding IAT is entry point. This is valid just for entry point. Is that right?

            S Offline
            S Offline
            sgenie68
            wrote on last edited by
            #5

            Exported functions can be called (or, rather, located and addressed) from the outside - so, whenever you need a function to be addressable from the outside you (or compiler/linker) make it exported. This is the only rule :)

            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