If I write
long long int module = (long long int)GetModuleHandle(L"test.dll");
how does C++ know how to cast a HMODULE? As If I'm correct HMODULE should be a struct.
I understand how this works for basic types such as int, float and so on but for programmer generated ones there must be like a translation, right?
Well, it can't really, unless the code tells it how to, such as via a conversion operator or constructor.
There are a few built-in rules for primitive numerical things like int
to float
, or void*
to int
… though this latter example can only be done by a reinterpret_cast
or a C-style cast like yours. That's because the conversion doesn't really make logical sense unless "you know best", which is what you're promising the compiler.
Indeed, like most handle types, HMODULE
is actually an alias for a pointer type (specifically, void*
), though the things that HMODULE
s point to will generally be of some class type. So you're casting a void*
to a long long int
, which is something the language's rules know how to do (ref 1, ref 2).