简体   繁体   English

Windows API如何工作?

[英]How does Windows API work?

It seems we are free to use Windows API simply by including header files needed. 似乎我们可以自由地通过包含所需的头文件来自由使用Windows API。 However, i couldn't make myself understand how this is possible given that header files do not have function definitions but declarations, which is supposed to be considered as an error in that it lacks implementation details. 但是,鉴于头文件没有函数定义而是声明,因此我无法理解这一点,因为缺少实现细节,应该将其视为错误。 In what way does a compiler locate an implementation detail and map it into memory? 编译器以什么方式定位实现细节并将其映射到内存中?

The Windows API is implemented in DLLs that are installed when you install Windows. Windows API在安装Windows时安装的DLL中实现。 Those DLLs are in the System directory and have names like User32.dll, Kernel32.dll, etc. 这些DLL位于System目录中,并具有User32.dll,Kernel32.dll等名称。

As you correctly noted, the Windows API header files supplied with your compiler contain only the declarations. 如您正确指出的那样,编译器随附的Windows API头文件仅包含声明。 When your C program calls one of those functions (or any other function that's identified in a header file but not actually compiled with your project), the compiler places a record in the generated object file that says, in effect, "I need to call a function called GetWindowRect (or whatever the name of the function you called)." 当您的C程序调用这些函数之一(或在头文件中标识但未随您的项目实际编译的任何其他函数)时,编译器会在生成的目标文件中放置一条记录,该记录实际上说:“我需要调用一个名为GetWindowRect的函数(或您调用的函数的任何名称)。”

The linker is what actually resolves the names. 链接器实际上是解析名称的工具。 If you look at your linker options, you'll see that it's linking some libraries like User32.lib, Kernel32.lib, etc. Those libraries contain compiled functions that are little more than stubs--code that causes the corresponding DLL to be loaded, and then calls the function in the DLL. 如果查看链接器选项,则会看到它正在链接某些库,例如User32.lib,Kernel32.lib等。这些库包含的编译函数只不过是存根而已,这些代码导致相应的DLL被加载。 ,然后在DLL中调用该函数。

It's a little bit more involved than that, but that's the general idea. 比这要复杂得多,但这是总的想法。 In summary: 综上所述:

  1. You include the Windows API header files. 您包括Windows API头文件。
  2. Your code makes a call to one or more Windows API functions that are declared in those header files. 您的代码将调用在这些头文件中声明的一个或多个Windows API函数。
  3. The compiler makes a note for the linker to resolve those API function calls. 编译器为链接器记录了解析这些API函数调用的注释。
  4. The linker resolves the calls by finding the stub functions in the libraries that you specify on the linker command line. 链接器通过在链接器命令行上指定的库中找到存根函数来解析调用。
  5. At runtime, a call to one of those functions causes the corresponding DLL to be loaded, and then control is branched to the function in the DLL. 在运行时,对这些函数之一的调用导致相应的DLL被加载,然后控件被分支到DLL中的函数。

It works the same as any other library, for example C stdlib - your program only needs to 'know' the prototypes of functions (and thus the underlying symbol names). 它的工作方式与任何其他库(例如C stdlib -您的程序仅需要“知道”函数的原型(并因此知道底层的符号名称)。 After compilation comes linking - that's when the symbols (function definitions) that are missing get 'linked' with a correct library. 编译后出现链接-就是在丢失的符号(函数定义)与正确的库“链接”时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM