Why can t CreateWindow be called via GetProcAddress?

Mondo Technology Updated on 2024-01-19

Sometimes, I see people tossing and turning over the question: "I want to use GetProcAddress to get the address to CreateWindow or ExitWindows, but it doesn't work." Why?”

Usually, they're trying to write platform calls (p invoke) because from a low-level perspective, platform calls are implemented via getprocaddress.

The question is: why can't getprocaddress be used on these functions?

The reason for this is that they (CreateWindow or ExitWindows) are not really export functions, and if you look at the corresponding header file, you will see such macro definitions.

In fact, createwindow is a two-dimensional macro definition, first of all, it expands to createwindowa or createwindoww depending on whether unicode is currently defined. These function-like macros are then expanded again into the actual export function createwindowexa or createwindowexw.

If you include winuserh header, all of this is handled automatically by the compiler, but if for some reason you want to use GetProcAddress for a function-like macro like CreateWindow, you'll have to manually expand the macro to see what the actual function is, and pass that function name to GetProcAddress.

The same principle applies to inline functions. These functions can't be obtained via getprocaddress because they don't export at all, they are provided to you as a source in the header file.

Note that whether something is a real function or a function-like macro (or inline function) may depend on your target platform. For example, GetWindowLongptra is a true export function on 64-bit Windows, but on 32-bit Windows, it's just a macro that resolves to GetWindowLonga. As another example, the Interlocked family of functions is an export function on x86 versions of Windows, but inline on all other Windows architectures.

It seems quite complicated, so how can you figure it all out?Here's how: study the header file.

In the header file, you'll see whether the function is a redirect, a function-like macro, an inline function, an intrinsic function, or an appropriate export function. If you can't figure it out from the header file, you can always just write a program that calls the function you're interested in, and then look at the disassembly to see what was actually generated.

Summary. When something you don't understand, the best way is to go through the source file (header file). Be assured: there is a reason for everything.

At last. Raymond Chen's "The Old New Thing" is one of my favorite blogs, and it has a lot of little knowledge about Windows, which is really helpful for Windows developers. **why can’t i getprocaddress for createwindow?》

Related Pages