<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[WinApi Funktion in C++ &amp;quot;aendern&amp;quot;]]></title><description><![CDATA[<p>Hallo Leute, folgende IP Funktion von der MSDN Seite ist gegeben (Ausschnitt)</p>
<blockquote>
<p>1.Declare a pointer to an IP_ADAPTER_INFO variable called pAdapterInfo, and a ULONG variable called ulOutBufLen. These variables are passed as parameters to the GetAdaptersInfo function. Also create a DWORD variable called dwRetVal (for error checking).</p>
<pre><code>IP_ADAPTER_INFO  *pAdapterInfo;
ULONG            ulOutBufLen;
DWORD            dwRetVal;
</code></pre>
<p>2.Allocate memory for the structures.</p>
<pre><code>pAdapterInfo = (IP_ADAPTER_INFO *) malloc( sizeof(IP_ADAPTER_INFO) );
ulOutBufLen = sizeof(IP_ADAPTER_INFO);
</code></pre>
<p>3.Make an initial call to GetAdaptersInfo to get the size needed into the ulOutBufLen variable.<br />
Note This call to the function is meant to fail, and is used to ensure that the ulOutBufLen variable specifies a size sufficient for holding all the information returned to pAdapterInfo. This is a common programming model for data structures and functions of this type.</p>
<pre><code>if (GetAdaptersInfo( pAdapterInfo, &amp;ulOutBufLen) != ERROR_SUCCESS) {
    free (pAdapterInfo);
    pAdapterInfo = (IP_ADAPTER_INFO *) malloc ( ulOutBufLen );
}
</code></pre>
</blockquote>
<p>Ich verstehe die ganze Sache wohl nicht richtig. Es wird doch mit dem Malloc im Prinzip auch nur Speicher für 1x struct IP_ADAPTER_INFO angefordert oder ?</p>
<p>Könnte ich nicht einfach ein</p>
<pre><code>pAdapterInfo = new IP_ADAPTER_INFO ; 

if (!pAdapterInfo){
         do_sm_Error_Handling();
}
</code></pre>
<p>machen ? Oder hab ich das falsch verstanden ? Sollte man die Funktionen eher C mäßig 1:1 von der MSDN Seite übernehmen ?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/331343/winapi-funktion-in-c-quot-aendern-quot</link><generator>RSS for Node</generator><lastBuildDate>Fri, 01 May 2026 16:47:39 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/331343.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 23 Feb 2015 17:46:19 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to WinApi Funktion in C++ &amp;quot;aendern&amp;quot; on Mon, 23 Feb 2015 17:46:19 GMT]]></title><description><![CDATA[<p>Hallo Leute, folgende IP Funktion von der MSDN Seite ist gegeben (Ausschnitt)</p>
<blockquote>
<p>1.Declare a pointer to an IP_ADAPTER_INFO variable called pAdapterInfo, and a ULONG variable called ulOutBufLen. These variables are passed as parameters to the GetAdaptersInfo function. Also create a DWORD variable called dwRetVal (for error checking).</p>
<pre><code>IP_ADAPTER_INFO  *pAdapterInfo;
ULONG            ulOutBufLen;
DWORD            dwRetVal;
</code></pre>
<p>2.Allocate memory for the structures.</p>
<pre><code>pAdapterInfo = (IP_ADAPTER_INFO *) malloc( sizeof(IP_ADAPTER_INFO) );
ulOutBufLen = sizeof(IP_ADAPTER_INFO);
</code></pre>
<p>3.Make an initial call to GetAdaptersInfo to get the size needed into the ulOutBufLen variable.<br />
Note This call to the function is meant to fail, and is used to ensure that the ulOutBufLen variable specifies a size sufficient for holding all the information returned to pAdapterInfo. This is a common programming model for data structures and functions of this type.</p>
<pre><code>if (GetAdaptersInfo( pAdapterInfo, &amp;ulOutBufLen) != ERROR_SUCCESS) {
    free (pAdapterInfo);
    pAdapterInfo = (IP_ADAPTER_INFO *) malloc ( ulOutBufLen );
}
</code></pre>
</blockquote>
<p>Ich verstehe die ganze Sache wohl nicht richtig. Es wird doch mit dem Malloc im Prinzip auch nur Speicher für 1x struct IP_ADAPTER_INFO angefordert oder ?</p>
<p>Könnte ich nicht einfach ein</p>
<pre><code>pAdapterInfo = new IP_ADAPTER_INFO ; 

if (!pAdapterInfo){
         do_sm_Error_Handling();
}
</code></pre>
<p>machen ? Oder hab ich das falsch verstanden ? Sollte man die Funktionen eher C mäßig 1:1 von der MSDN Seite übernehmen ?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2444031</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2444031</guid><dc:creator><![CDATA[c_to_cpp]]></dc:creator><pubDate>Mon, 23 Feb 2015 17:46:19 GMT</pubDate></item><item><title><![CDATA[Reply to WinApi Funktion in C++ &amp;quot;aendern&amp;quot; on Mon, 23 Feb 2015 18:21:32 GMT]]></title><description><![CDATA[<p>Um anderen das googlen zu ersparen: es geht um diese MSDN Seite:<br />
<a href="https://msdn.microsoft.com/de-de/library/windows/desktop/aa366314%28v=vs.85%29.aspx" rel="nofollow">https://msdn.microsoft.com/de-de/library/windows/desktop/aa366314%28v=vs.85%29.aspx</a></p>
<p>c_to_cpp schrieb:</p>
<blockquote>
<p>Ich verstehe die ganze Sache wohl nicht richtig. Es wird doch mit dem Malloc im Prinzip auch nur Speicher für 1x struct IP_ADAPTER_INFO angefordert oder ?</p>
</blockquote>
<p>Ja: im ersten Durchgang schon.<br />
Der Puffer wird aber fast sicher zu klein sein.<br />
Dann schlägt <code>GetAdaptersInfo()</code> zwar fehl, aber immerhin steht danach in <code>ulOutBufLen</code> wie groß der Puffer denn mindestens sein muss, damit das nicht wieder passiert.<br />
Also wird der anfängliche (zu kleine) Puffer freigegeben, ein ausreichender neuer alloziert und das ganze wiederholt.</p>
<p>c_to_cpp schrieb:</p>
<blockquote>
<p>Sollte man die Funktionen eher C mäßig 1:1 von der MSDN Seite übernehmen ?</p>
</blockquote>
<p>IMHO ja. Pack das Ding in eine Funktion und lass Dir einen vielleicht einen smartpointer zurückgeben, wenn Du mehr C++ drinhaben willst. new/delete statt malloc/free macht es noch einfacher...</p>
<p>Übrigens ist <code>GetAdaptersInfo()</code> seit XP(!) deprecated, wenn ich das richtig lese.<br />
<a href="https://msdn.microsoft.com/de-de/library/windows/desktop/aa365917%28v=vs.85%29.aspx" rel="nofollow">https://msdn.microsoft.com/de-de/library/windows/desktop/aa365917%28v=vs.85%29.aspx</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2444043</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2444043</guid><dc:creator><![CDATA[Furble Wurble]]></dc:creator><pubDate>Mon, 23 Feb 2015 18:21:32 GMT</pubDate></item><item><title><![CDATA[Reply to WinApi Funktion in C++ &amp;quot;aendern&amp;quot; on Mon, 23 Feb 2015 18:55:43 GMT]]></title><description><![CDATA[<blockquote>
<p>new/delete statt malloc/free macht es noch einfacher...</p>
</blockquote>
<p>Wie wende ich dann ein new mit der Buffer Größe an ?</p>
<pre><code>pAdapterInfo = new IP_ADAPTER_INFO ; 

if (!pAdapterInfo){
         do_sm_Error_Handling();
}
</code></pre>
<p>=&gt; Das wuerde ja nur für 1x Struct Speicher anfordern....</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2444053</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2444053</guid><dc:creator><![CDATA[c_to_cpp]]></dc:creator><pubDate>Mon, 23 Feb 2015 18:55:43 GMT</pubDate></item><item><title><![CDATA[Reply to WinApi Funktion in C++ &amp;quot;aendern&amp;quot; on Mon, 23 Feb 2015 19:52:44 GMT]]></title><description><![CDATA[<p>c_to_cpp schrieb:</p>
<blockquote>
<blockquote>
<p>new/delete statt malloc/free macht es noch einfacher...</p>
</blockquote>
<p>Wie wende ich dann ein new mit der Buffer Größe an ?</p>
</blockquote>
<pre><code class="language-cpp">char* p=new char[N];
//...
delete []p;
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2444058</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2444058</guid><dc:creator><![CDATA[Furble Wurble]]></dc:creator><pubDate>Mon, 23 Feb 2015 19:52:44 GMT</pubDate></item><item><title><![CDATA[Reply to WinApi Funktion in C++ &amp;quot;aendern&amp;quot; on Mon, 23 Feb 2015 21:45:14 GMT]]></title><description><![CDATA[<p>Ok, das war mir schon klar, ich meinte wie ich es in diesem Fall auf</p>
<pre><code>pAdapterInfo = (IP_ADAPTER_INFO *) malloc ( ulOutBufLen );
</code></pre>
<p>anwende. Vor dem Malloc ist ja ein Cast, wie setzte ich das auf new / delete um ?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2444072</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2444072</guid><dc:creator><![CDATA[c_to_cpp]]></dc:creator><pubDate>Mon, 23 Feb 2015 21:45:14 GMT</pubDate></item><item><title><![CDATA[Reply to WinApi Funktion in C++ &amp;quot;aendern&amp;quot; on Mon, 23 Feb 2015 21:50:30 GMT]]></title><description><![CDATA[<p>c_to_cpp schrieb:</p>
<blockquote>
<p>Vor dem Malloc ist ja ein Cast, wie setzte ich das auf new / delete um ?</p>
</blockquote>
<p>Genauso?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2444073</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2444073</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Mon, 23 Feb 2015 21:50:30 GMT</pubDate></item><item><title><![CDATA[Reply to WinApi Funktion in C++ &amp;quot;aendern&amp;quot; on Mon, 23 Feb 2015 22:22:02 GMT]]></title><description><![CDATA[<p>Du kannst auch mit <code>char*</code> arbeiten und erst später casten, wenn Dir gleich casten nicht gefällt <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
<p>Auch auf die Gefahr hin mich zu wiederholen:<br />
MSDN schlägt selber vor<a href="https://msdn.microsoft.com/de-de/library/windows/desktop/aa365915%28v=vs.85%29.aspx" rel="nofollow"> <code>GetAdaptersAddresses()</code> </a>statt <code>GetAdaptersInfo()</code> zu verwenden.<br />
Das Procedere ist aber gleich:</p>
<pre><code class="language-cpp">unique_ptr&lt;IP_ADAPTER_ADDRESSES&gt;
get_adapters_addresses(ULONG family, ULONG flags) {
  using ptr_t = std::unique_ptr&lt;IP_ADAPTER_ADDRESSES&gt;;
  ULONG size = 15*1024;  // lt. doku
  PIP_ADAPTER_ADDRESSES addr = (PIP_ADAPTER_ADDRESSES)new char[size];
  ULONG rc;
  while((rc=GetAdapterAddresses(family, flags, nullptr, addr, &amp;size))==ERROR_BUFFER_OVERFLOW){
    delete[]addr;
    addr=(PIP_ADAPTER_ADDRESSES)new char[size];
  }
  if(rc==ERROR_SUCCESS)
    return ptr_t{addr};
  // Hier koennte Ihre Fehlerbehandlung stehen...
  delete[]addr;
  switch(rc){
  case ERROR_NOT_ENOUGH_MEMORY:
    throw bad_alloc{};
  // ....
  default:
    throw runtime_error{&quot;What the zuny!?&quot;};
  }
}
</code></pre>
<p>So ungefähr könnte das aussehen - hab aber keine Windows-Mühle.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2444076</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2444076</guid><dc:creator><![CDATA[Furble Wurble]]></dc:creator><pubDate>Mon, 23 Feb 2015 22:22:02 GMT</pubDate></item><item><title><![CDATA[Reply to WinApi Funktion in C++ &amp;quot;aendern&amp;quot; on Mon, 23 Feb 2015 22:30:17 GMT]]></title><description><![CDATA[<p>Benutz doch in solchen Beispielen konsequent die Smartpointer. So wie jetzt ist es doch bloß C mit new statt malloc und der Threadersteller fragt sich, wo überhaupt der Unterschied zwischen den Sprachen ist.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2444078</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2444078</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Mon, 23 Feb 2015 22:30:17 GMT</pubDate></item><item><title><![CDATA[Reply to WinApi Funktion in C++ &amp;quot;aendern&amp;quot; on Mon, 23 Feb 2015 22:43:13 GMT]]></title><description><![CDATA[<p>Ja, Sire! Sofort Sire!</p>
<pre><code class="language-cpp">unique_ptr&lt;IP_ADAPTER_ADDRESSES&gt;
get_adapters_addresses(ULONG family, ULONG flags) {
  using ptr_t = std::unique_ptr&lt;IP_ADAPTER_ADDRESSES&gt;;
  ULONG size = 15*1024;  // lt. doku
  auto addr = ptr_t{(PIP_ADAPTER_ADDRESSES)new char[size]};
  ULONG rc;
  while((rc=GetAdapterAddresses(family, flags, nullptr, addr.get(), &amp;size))==ERROR_BUFFER_OVERFLOW)
    addr=ptr_t{(PIP_ADAPTER_ADDRESSES)new char[size]};
  if(rc==ERROR_SUCCESS)
    return addr;
  // Hier koennte Ihre Fehlerbehandlung stehen...
  switch(rc){
  case ERROR_NOT_ENOUGH_MEMORY:
    throw bad_alloc{};
  // ....
  default:
    throw runtime_error{&quot;What the zuny!?&quot;};
  }
}
</code></pre>
<p>@c_to_cpp: wie Du siehst sind nach SeppJs Einwand und einer weiteren Iteration des Codes alle händischen Speicherfreigaben verschwunden. Der Code ist kürzer und klarer.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2444079</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2444079</guid><dc:creator><![CDATA[Furble Wurble]]></dc:creator><pubDate>Mon, 23 Feb 2015 22:43:13 GMT</pubDate></item><item><title><![CDATA[Reply to WinApi Funktion in C++ &amp;quot;aendern&amp;quot; on Mon, 23 Feb 2015 23:37:49 GMT]]></title><description><![CDATA[<p>@c_to_cpp<br />
Arbeite erstmal mit malloc/free, ist für den Anfang das einfachste.</p>
<p>@Furble Wurble<br />
Darf man denn überhaupt ein mit <code>new char[]</code> besorgtes char-Array mit <code>delete (POD_TYPE*)addressOfFirstElement</code> freigeben?<br />
Wenn nicht, was ich vermute, dann bekommt man mit <code>unique_ptr&lt;POD_TYPE&gt;</code> ein bisschen ein UB Problem.<br />
Ich hab' das bisher immer mit <code>malloc</code> + <code>shared_ptr&lt;POD_TYPE&gt;</code> + custom deleter der <code>free</code> aufruft gemacht.</p>
<p>(Man kann natürlich auch nen <code>unique_ptr&lt;POD_TYPE, CLibFreeDeleter&gt;</code> o.ä. verwenden. Wenn man vorwiegend <code>auto</code> verwendet ist das OK, aber sonst... bäh.)</p>
<p>ps.: So:</p>
<pre><code class="language-cpp">#include &lt;memory&gt;
#include &lt;cassert&gt;

template &lt;class T&gt;
std::shared_ptr&lt;T&gt; AllocateSizedPod(size_t size)
{
    assert(size &gt;= sizeof(T));
    std::shared_ptr&lt;T&gt; const ptr(static_cast&lt;T*&gt;(std::calloc(1, size)), std::free);
    if (!ptr)
        throw std::bad_alloc();

    return ptr;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2444080</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2444080</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Mon, 23 Feb 2015 23:37:49 GMT</pubDate></item><item><title><![CDATA[Reply to WinApi Funktion in C++ &amp;quot;aendern&amp;quot; on Mon, 23 Feb 2015 23:40:14 GMT]]></title><description><![CDATA[<p>hustbaer schrieb:</p>
<blockquote>
<p>@Furble Wurble<br />
Darf man denn überhaupt ....</p>
</blockquote>
<p>Nein. Das ist mir auch aufgefallen - sonst würde ich auch schon pofen. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /></p>
<p>Tatsächlich stimme ich Dir zu malloc/free ist einfacher korrekt hinzubekommen, als new/delete.</p>
<p>Ich bastel gerade noch an einem wrapper, aber der nimmt einen vector&lt;char&gt; als Speicher...ist aber nur für mich, damit ich gleich schlafen kann. Ohne schlechtes Gewissen... <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2444082</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2444082</guid><dc:creator><![CDATA[Furble Wurble]]></dc:creator><pubDate>Mon, 23 Feb 2015 23:40:14 GMT</pubDate></item><item><title><![CDATA[Reply to WinApi Funktion in C++ &amp;quot;aendern&amp;quot; on Tue, 24 Feb 2015 00:22:29 GMT]]></title><description><![CDATA[<p>Nimm new und delete, das macht es noch leichter!<br />
<em>~ Furble Wurble</em></p>
<p>Ich &lt;- doof!</p>
<p>Mal eben so aus dem Lamäng einen Code hinpfuschen...Nie wieder! <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f576.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--sunglasses"
      title=":sunglasses:"
      alt="🕶"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2444084</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2444084</guid><dc:creator><![CDATA[Furble Wurble]]></dc:creator><pubDate>Tue, 24 Feb 2015 00:22:29 GMT</pubDate></item><item><title><![CDATA[Reply to WinApi Funktion in C++ &amp;quot;aendern&amp;quot; on Tue, 24 Feb 2015 13:42:24 GMT]]></title><description><![CDATA[<p>Danke erstmal. Der Wrapper von Furble wäre evtl. noch interessant... ?<br />
Ich werde es erstmal bei malloc / free belassen. Habe noch 2 Fragen :</p>
<ol>
<li>Wenn ich das ganze mit den Smart_Pointern usw. weglasse (Habe mich damit noch zuwenig befasst) müsste folgender Code aber gehen oder ?</li>
</ol>
<pre><code>PIP_ADAPTER_ADDRESSES addr = (PIP_ADAPTER_ADDRESSES)new char[size];

while((rc=GetAdapterAddresses(family, flags, nullptr, addr, &amp;size)==ERROR_BUFFER_OVERFLOW){
   delete[]addr; 
   addr=(PIP_ADAPTER_ADDRESSES)new char[size]; 
}
</code></pre>
<p>Hier müsste ja wie im Original code nach dem 1. Aufruf (wenn dieser denn Failt) die &quot;neue&quot; Speichergroesse angefordert werden. Kann es hier ein Problem geben ? Ich weiß nicht ob GetAdapterAdresses addr evtl. ändert, ich würde den dann noch wegspeichern davor , damit delete wieder den Originalpointer erhält?</p>
<p>2.)<br />
auf der MSDN Seite ( <a href="https://www.google.de/search?&amp;q=site%3Amsdn.microsoft.com%20https%3A%2F%2Fmsdn.microsoft.com%2Fde-de%2Flibrary%2Fwindows%2Fdesktop%2Faa366314%2528v%3Dvs.85%2529.aspx%20" rel="nofollow">Google: site:msdn.microsoft.com https://msdn.microsoft.com/de-de/library/windows/desktop/aa366314%28v=vs.85%29.aspx</a> )</p>
<p>wird der Speicher so freigegeben ... ??? :</p>
<blockquote>
<p><a href="//6.Free">//6.Free</a> any memory allocated for the pAdapterInfo structure.</p>
<pre><code>if (pAdapterInfo)
        free(pAdapterInfo);
</code></pre>
</blockquote>
<p>==&gt;&gt; pAdapterInfo ist doch eigentlich ne Linked-List, ist das Speicherfreigeben so nicht falsch ? Der gibt doch nur für ein Element frei ? ? ?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2444158</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2444158</guid><dc:creator><![CDATA[c_to_cpp]]></dc:creator><pubDate>Tue, 24 Feb 2015 13:42:24 GMT</pubDate></item><item><title><![CDATA[Reply to WinApi Funktion in C++ &amp;quot;aendern&amp;quot; on Tue, 24 Feb 2015 15:54:11 GMT]]></title><description><![CDATA[<p>c_to_cpp schrieb:</p>
<blockquote>
<p>Ich weiß nicht ob GetAdapterAdresses addr evtl. ändert, ich würde den dann noch wegspeichern davor, damit delete wieder den Originalpointer erhält?</p>
</blockquote>
<p><code>GetAdaptersAddresses()</code> <em>kann</em> den Wert (die Adresse auf die der Zeiger zeigt) des Parameters <code>AdapterAddresses</code> nicht ändern, da er per value übergeben wird.</p>
<p>c_to_cpp schrieb:</p>
<blockquote>
<p>==&gt;&gt; pAdapterInfo ist doch eigentlich ne Linked-List, ist das Speicherfreigeben so nicht falsch ? Der gibt doch nur für ein Element frei ? ? ?</p>
</blockquote>
<p>Das <code>delete[]</code> gibt allen Speicher frei, den du mit <code>new T[N]</code> angefordert hast. <code>IP_ADAPTER_ADDRESSES</code> ist zwar eine linked list, &quot;lebt&quot; aber nur in dem von dir bereitgestelltem Speicher. Eine Liste ist es deshalb, weil die einzelnen <code>IP_ADAPTER_ADDRESSES</code> unterschiedlich groß sein können. Du hättest sonst keine Möglichkeit festzustellen, wo ein <code>IP_ADAPTER_ADDRESSES</code> endet und ein neues anfängt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2444181</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2444181</guid><dc:creator><![CDATA[Swordfish]]></dc:creator><pubDate>Tue, 24 Feb 2015 15:54:11 GMT</pubDate></item><item><title><![CDATA[Reply to WinApi Funktion in C++ &amp;quot;aendern&amp;quot; on Tue, 24 Feb 2015 15:55:39 GMT]]></title><description><![CDATA[<p>Wenn man die <code>PIP_ADAPTER_ADDRESSES</code> nur lokal braucht kann man das auch ganz ohne Pointer per std::vector&lt;char&gt; lösen (ohne Sicherheitsgurte und Helm):</p>
<pre><code>#include &lt;vector&gt;

void f()
{
   ULONG RequiredSize = 0;
   ::GetAdaptersAddresses( AF_INET, 0, NULL, NULL, &amp;RequiredSize );
   if( RequiredSize &gt; 0 )
   {
      std::vector&lt;char&gt; Buffer( RequiredSize );
      PIP_ADAPTER_ADDRESSES Addresses = reinterpret_cast&lt;PIP_ADAPTER_ADDRESSES&gt;( &amp;Buffer[0] );

      ::GetAdaptersAddresses( AF_INET, 0, NULL, Addresses, RequiredSize );
   }
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2444182</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2444182</guid><dc:creator><![CDATA[DocShoe]]></dc:creator><pubDate>Tue, 24 Feb 2015 15:55:39 GMT</pubDate></item><item><title><![CDATA[Reply to WinApi Funktion in C++ &amp;quot;aendern&amp;quot; on Tue, 24 Feb 2015 16:18:36 GMT]]></title><description><![CDATA[<blockquote>
<p>GetAdaptersAddresses() kann den Wert (die Adresse auf die der Zeiger zeigt) des Parameters AdapterAddresses nicht ändern, da er per value übergeben wird.</p>
</blockquote>
<p>Stimmt, peinlich.<br />
Ok, dann müsste das mit meinen paar Zeilen eigentlich funktionieren...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2444186</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2444186</guid><dc:creator><![CDATA[c_to_cpp]]></dc:creator><pubDate>Tue, 24 Feb 2015 16:18:36 GMT</pubDate></item><item><title><![CDATA[Reply to WinApi Funktion in C++ &amp;quot;aendern&amp;quot; on Tue, 24 Feb 2015 19:09:41 GMT]]></title><description><![CDATA[<p>c_to_cpp schrieb:</p>
<blockquote>
<blockquote>
<p>GetAdaptersAddresses() kann den Wert (die Adresse auf die der Zeiger zeigt) des Parameters AdapterAddresses nicht ändern, da er per value übergeben wird.</p>
</blockquote>
<p>Stimmt, peinlich.<br />
Ok, dann müsste das mit meinen paar Zeilen eigentlich funktionieren...</p>
</blockquote>
<p>Bloß, dass man dann eben im Falle einer Exception ein Problem hat.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2444217</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2444217</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Tue, 24 Feb 2015 19:09:41 GMT</pubDate></item><item><title><![CDATA[Reply to WinApi Funktion in C++ &amp;quot;aendern&amp;quot; on Tue, 24 Feb 2015 19:40:15 GMT]]></title><description><![CDATA[<p>c_to_cpp schrieb:</p>
<blockquote>
<p>Danke erstmal. Der Wrapper von Furble wäre evtl. noch interessant... ?</p>
</blockquote>
<p>Na klar. Ich habe ja gerade eine richtige Serie von guten Ideen in diesem Thread <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /><br />
So habe ich mir das minimal gedacht:</p>
<pre><code class="language-cpp">template&lt;typename T&gt;
class wrapper : private vector&lt;char&gt; {
  using impl_t = vector&lt;char&gt;;
public:
  using pointer = T;
  using impl_t::resize;
  wrapper() = default;
  wrapper(size_t sz)
    : impl_t(sz) { }
  pointer get() { return reinterpret_cast&lt;pointer&gt;(&amp;(*this)[0]); }
};

using pip_adapter_addresses = wrapper&lt;PIP_ADAPTER_ADDRESSES&gt;;

pip_adapter_addresses
get_adapters_addresses(ULONG family, ULONG flags) {
  ULONG size = 15*1024;  // lt. doku
  pip_adapter_addresses addr(size);
  ULONG rc;
  while((rc=GetAdaptersAddresses(family, flags, nullptr, addr.get(), &amp;size))==ERROR_BUFFER_OVERFLOW)
    addr.resize(size);
  if(rc==ERROR_SUCCESS)
    return addr;
  // Hier koennte Ihre Fehlerbehandlung stehen...
  switch(rc){
  case ERROR_NOT_ENOUGH_MEMORY:
    throw bad_alloc{};
  // ....
  default:
    throw runtime_error{&quot;What the zuny!?&quot;};
  }
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2444224</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2444224</guid><dc:creator><![CDATA[Furble Wurble]]></dc:creator><pubDate>Tue, 24 Feb 2015 19:40:15 GMT</pubDate></item><item><title><![CDATA[Reply to WinApi Funktion in C++ &amp;quot;aendern&amp;quot; on Tue, 24 Feb 2015 20:03:24 GMT]]></title><description><![CDATA[<p>DocShoe schrieb:</p>
<blockquote>
<p>[...]</p>
</blockquote>
<p>yay! <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44d.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_up"
      title=":+1:"
      alt="👍"
    /> <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f60b.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_savoring_food"
      title=":yum:"
      alt="😋"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2444231</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2444231</guid><dc:creator><![CDATA[Swordfish]]></dc:creator><pubDate>Tue, 24 Feb 2015 20:03:24 GMT</pubDate></item></channel></rss>