I know that I can use the WinApi DsGetDcName function DsGetDcName as follows:
DOMAIN_CONTROLLER_INFO* dcInfo = nullptr; unsigned long res = ::DsGetDcName(nullptr, nullptr, nullptr, nullptr, 0, &dcInfo);
This is unnatural, I know, but I want to understand why it also cannot be written like this:
void* dcInfo = nullptr; unsigned long res = ::DsGetDcName(nullptr, nullptr, nullptr, nullptr, 0, (DOMAIN_CONTROLLER_INFO**) dcInfo); if (res) { wchar_t* name; name = static_cast<DOMAIN_CONTROLLER_INFO*> (dcInfo)->DomainControllerName; }
The second version uses void* as the type of pointer, and that is why I get an access violation when it starts (when ::DsGetDcName ). But I do not understand why this is so? Is this dcInfo to how memory is aligned when specifying void* for dcInfo , and not of type DOMAIN_CONTROLLER_INFO* dcInfo ?
Decision
I will find out the problem, I can use a really confusing unsafe version of void *, I just did not pass the correct address of the pointer to this function. There he is:
void* dcInfo = nullptr; unsigned long res = ::DsGetDcName(nullptr, nullptr, nullptr, nullptr, 0, (DOMAIN_CONTROLLER_INFO**) &dcInfo);
Note that I'm skipping (DOMAIN_CONTROLLER_INFO**) &dcInfo instead of (DOMAIN_CONTROLLER_INFO**) dcInfo . I just shut myself up earlier because I told the compiler that I knew what I was doing, but I passed the pointer value to the function instead of the pointer (and yes, that pointer value was nullptr ) nullptr )
This is another reason to use the correct version (version 1). In this second case, the disadvantage is that you must execute the result again as follows:
wchar_t* name; name = static_cast<DOMAIN_CONTROLLER_INFO*>(dcInfo)->DomainControllerName;