Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ conversion question
    primarykey
    data
    text
    <p>I know that I can use <em>correctly</em> the WinApi function <code>DsGetDcName</code> like this:</p> <pre><code>DOMAIN_CONTROLLER_INFO* dcInfo = nullptr; unsigned long res = ::DsGetDcName(nullptr, nullptr, nullptr, nullptr, 0, &amp;dcInfo); </code></pre> <p>It is unnatural, I know, but I want to understand why one cannot write it also like this:</p> <pre><code> void* dcInfo = nullptr; unsigned long res = ::DsGetDcName(nullptr, nullptr, nullptr, nullptr, 0, (DOMAIN_CONTROLLER_INFO**) dcInfo); if (res) { wchar_t* name; name = static_cast&lt;DOMAIN_CONTROLLER_INFO*&gt; (dcInfo)-&gt;DomainControllerName; } </code></pre> <p>The second version uses <code>void*</code> as a pointer type, and this is why I get an acces violation when running it (at call to the <code>::DsGetDcName</code>). But I don't understand why is this? Does it have to do with the way the memory is aligned when specifying <code>void*</code> for <code>dcInfo</code> as opposed to type <code>DOMAIN_CONTROLLER_INFO* dcInfo</code> ?</p> <p><strong>SOLUTION</strong></p> <p>I find out the problem, I can use actually the <em>convoluted</em> unsafe void* version, I just didn't passed the right pointer address to that function. Here it is:</p> <pre><code>void* dcInfo = nullptr; unsigned long res = ::DsGetDcName(nullptr, nullptr, nullptr, nullptr, 0, (DOMAIN_CONTROLLER_INFO**) &amp;dcInfo); </code></pre> <p>Notice that I pass <code>(DOMAIN_CONTROLLER_INFO**) &amp;dcInfo</code> instead of <code>(DOMAIN_CONTROLLER_INFO**) dcInfo</code>. I was just shutting myself in the foot before because I told the compiler I know what I'm doing but passed to the function a pointer value instead of the address of the pointer needed (and yes, that pointer value was <code>nullptr</code> ) :-))</p> <p>This is one more reason to use the right version (version 1). In this second case also the disadvantage is that you have to cast again the result like this:</p> <pre><code>wchar_t* name; name = static_cast&lt;DOMAIN_CONTROLLER_INFO*&gt;(dcInfo)-&gt;DomainControllerName; // Get DC </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload