Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How C header files import win32api functions?

short question.

When I do:

#include <windows.h>

CreateProcess(..)

How does the header file knows to connect me to the correct dll?

Is there a dictionary inside those header files that maps each win32api function to the relevant dll?

like image 314
Trigosin Darom Avatar asked Dec 23 '22 19:12

Trigosin Darom


1 Answers

The headers only contain declarations for the API functions, not their definitions. The definitions are found by linking the appropriate libraries.

For example, copy the following into a .c file:

#include <Windows.h>

int main()
{
    MessageBoxW(NULL, L"Hello World", L"", MB_OK);
    return 0;
}

Try to compile it on the Visual Studio Command Prompt with the command cl example.c. You will see the error message:

example.obj : error LNK2019: unresolved external symbol __imp__MessageBoxW@16 referenced in function _main

Note that compilation itself actually succeeds; it's the linker that complains about the missing function definition.

If you instead compile it with cl example.c /link user32.lib, the linker will find the definition for MessageBox. You could also just link example.obj user32.lib.

So, to answer your question: the headers don't need to know which DLLs the functions are in because the project is required to provide the appropriate library references. Fortunately, the MSDN documentation for Windows API functions will tell you which DLL the function is in and which library to link.

like image 113
Govind Parmar Avatar answered Dec 28 '22 09:12

Govind Parmar