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