I was reading the Win32 programming book which is written by Jeffry Richter. In this book, there is an example code like the following:
#include <windows.h>
#include <iostream>
extern "C" const IMAGE_DOS_HEADER __ImageBase;
int main(int argc, const char* argv[])
{
HMODULE hModule = GetModuleHandle(NULL);
std::cout << "BaseImage with GetModuleHandle(NULL): " << hModule << std::endl;
// Use the pseudo-variable __ImageBase to get the address of the current module hModule/hInstance.
std::cout << "BaseImage with __ImageBase: " << (HINSTANCE)&__ImageBase << std::endl;
// Pass the address of the current method Main as parameter to GetModuleHandleEx
// to get the address of the current module hModule/hInstance.
hModule = NULL;
GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (PCTSTR)main, &hModule);
std::cout << "BaseImage Main with GetModuleHandleEx: " << hModule << std::endl;
return 0;
}
but I can't understand very well, why he used extern "C" for the structure. Also, when we remove the extern keyword, the program gives me the wrong result. Someone can explain why we need to define __ImageBase with this signature?