<?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[Generelles Problem: hInstance ist lokal in WinMain]]></title><description><![CDATA[<p>Was mir bei meinen Basteleien immer auffällt, sind die &quot;Nachteile&quot; der lokal gehaltenen hInstance in der WinMain. Ich hab die mal global gemacht, also zusätzlich noch in eine globale Variable geschrieben. Hab mir dann auch Quellcodes von paar OpenSource-Projekten angesehen, wo das genau auch der Fall war.</p>
<p>Ist das eher etwas, das man vermeiden sollte, oder gängige Praxis?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/210062/generelles-problem-hinstance-ist-lokal-in-winmain</link><generator>RSS for Node</generator><lastBuildDate>Wed, 22 Apr 2026 04:54:37 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/210062.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 06 Apr 2008 20:07:18 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Generelles Problem: hInstance ist lokal in WinMain on Sun, 06 Apr 2008 20:07:18 GMT]]></title><description><![CDATA[<p>Was mir bei meinen Basteleien immer auffällt, sind die &quot;Nachteile&quot; der lokal gehaltenen hInstance in der WinMain. Ich hab die mal global gemacht, also zusätzlich noch in eine globale Variable geschrieben. Hab mir dann auch Quellcodes von paar OpenSource-Projekten angesehen, wo das genau auch der Fall war.</p>
<p>Ist das eher etwas, das man vermeiden sollte, oder gängige Praxis?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1487572</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1487572</guid><dc:creator><![CDATA[Frickelhans]]></dc:creator><pubDate>Sun, 06 Apr 2008 20:07:18 GMT</pubDate></item><item><title><![CDATA[Reply to Generelles Problem: hInstance ist lokal in WinMain on Sun, 06 Apr 2008 20:11:52 GMT]]></title><description><![CDATA[<p>kannst dir auch auf viele andere möglichkeiten holen, GetModuleHandle z.b. und irgendwo steht die auch noch drin, da hatte jochen mal irgendsoeinen coolen trick geposted, musste mal suchen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1487574</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1487574</guid><dc:creator><![CDATA[rofler]]></dc:creator><pubDate>Sun, 06 Apr 2008 20:11:52 GMT</pubDate></item><item><title><![CDATA[Reply to Generelles Problem: hInstance ist lokal in WinMain on Sun, 06 Apr 2008 23:23:22 GMT]]></title><description><![CDATA[<p>rofler schrieb:</p>
<blockquote>
<p>GetModuleHandle</p>
</blockquote>
<p>ohje...nich gut...</p>
<p>ich verweise hier mal auf Martin Richters Blog:</p>
<p><a href="http://blog.m-ri.de/index.php/2007/12/12/die-unsitte-immer-getmodulehandlenull-fuer-hinstance-in-createwindow-und-registerclass-zu-verwenden/" rel="nofollow">http://blog.m-ri.de/index.php/2007/12/12/die-unsitte-immer-getmodulehandlenull-fuer-hinstance-in-createwindow-und-registerclass-zu-verwenden/</a></p>
<p>dieser aufschlussreicher eintrag sollte denkanstöße geben <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
<p>es gibt wie immer bei der WinAPI (oder überhaupt) mehrere möglichkeiten, etwas zu tun. so auch mit dem Wert von hInstance.<br />
so schlimm ist in diesem fall eine globale variable nicht.<br />
du kannst sie allerdings auch jeder funktion, die sie brauch, übergeben, so sparst du dir auch die globale variable. oder du vermeidest den direkten zugriff auf die globale variable über eine funtion:</p>
<pre><code class="language-cpp">// WARNUNG: ohne jegliche fehlerüberprüfung

// data.h
void InitInstance(HINSTANCE hInst);
HINSTANCE MyGetInstance(void);
</code></pre>
<pre><code class="language-cpp">// data.c
HINSTANCE g_hInst = NULL;

void InitInstance(HINSTANCE hInst) {
    g_hInst = hInst;
}

HINSTANCE MyGetInstance(void) {
    return g_hInst;
}
</code></pre>
<p>InitInstance rufst du dann z.b. in WinMain auf ... vorteil dabei -&gt; egal ob du code für eine .exe oder .dll schreibst, oder code für eine .exe später in eine .dll auslagst, so du musst dich um keine globale variable kümmern, du musst nicht alle, aufrufe von GetModuleHandle(NULL), wie rofler erwähnte, ersetzen in der .dll. kurz: du hast eine einheitliche API.</p>
<p>grüße, xantus</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1487618</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1487618</guid><dc:creator><![CDATA[Xantus]]></dc:creator><pubDate>Sun, 06 Apr 2008 23:23:22 GMT</pubDate></item><item><title><![CDATA[Reply to Generelles Problem: hInstance ist lokal in WinMain on Sun, 06 Apr 2008 23:28:08 GMT]]></title><description><![CDATA[<p>doll das mag bestimmt richtig sein, nur wenn ich GetModuleHandle mit NULL aufrufe, kriege ich immer noch das module handle der exe datei, also dat was er will. dat steht so in der msdn und dat wird 100%ig das gleiche sein, was er in seiner WinMain kriegt. also was willst du mir damit sagen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1487620</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1487620</guid><dc:creator><![CDATA[rofler]]></dc:creator><pubDate>Sun, 06 Apr 2008 23:28:08 GMT</pubDate></item><item><title><![CDATA[Reply to Generelles Problem: hInstance ist lokal in WinMain on Mon, 07 Apr 2008 06:50:35 GMT]]></title><description><![CDATA[<p>Solange GetModuleHandle(NULL) immer in der EXE aufgerufen wird, ist nichts dagegen einzuwenden. Die Probleme fangen erst an, wenn die Aufrufe aus einer DLL erfolgen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1487647</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1487647</guid><dc:creator><![CDATA[sri]]></dc:creator><pubDate>Mon, 07 Apr 2008 06:50:35 GMT</pubDate></item><item><title><![CDATA[Reply to Generelles Problem: hInstance ist lokal in WinMain on Mon, 07 Apr 2008 12:38:04 GMT]]></title><description><![CDATA[<p>das wusste ich auch scho vor diesem thread</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1487864</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1487864</guid><dc:creator><![CDATA[rofler]]></dc:creator><pubDate>Mon, 07 Apr 2008 12:38:04 GMT</pubDate></item><item><title><![CDATA[Reply to Generelles Problem: hInstance ist lokal in WinMain on Mon, 07 Apr 2008 12:43:41 GMT]]></title><description><![CDATA[<p>d.h. ne war es nicht, die info war schon wissenswert</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1487872</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1487872</guid><dc:creator><![CDATA[rofler]]></dc:creator><pubDate>Mon, 07 Apr 2008 12:43:41 GMT</pubDate></item><item><title><![CDATA[Reply to Generelles Problem: hInstance ist lokal in WinMain on Mon, 07 Apr 2008 18:07:12 GMT]]></title><description><![CDATA[<p>Ich weiss garnicht was hier immer für eine Welle mit globalen Variblen gemacht wird. Ich speichere <strong>immer</strong> meine HINSTANCE beim Aufruf von WinMain, und wenn ich mein Hauptfenster und meine Childfenster erstelle bekommen die auch <strong>immer</strong> einen globalen Handle, mir fällt auch kein Grund ein warum man es anderst machen soll.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1488132</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1488132</guid><dc:creator><![CDATA[GlobalPlayerVistaStyle]]></dc:creator><pubDate>Mon, 07 Apr 2008 18:07:12 GMT</pubDate></item><item><title><![CDATA[Reply to Generelles Problem: hInstance ist lokal in WinMain on Mon, 07 Apr 2008 19:22:32 GMT]]></title><description><![CDATA[<p>lolrollo</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1488178</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1488178</guid><dc:creator><![CDATA[rofler]]></dc:creator><pubDate>Mon, 07 Apr 2008 19:22:32 GMT</pubDate></item><item><title><![CDATA[Reply to Generelles Problem: hInstance ist lokal in WinMain on Mon, 07 Apr 2008 19:39:12 GMT]]></title><description><![CDATA[<p>Frickelhans schrieb:</p>
<blockquote>
<p>Ist das eher etwas, das man vermeiden sollte, oder gängige Praxis?</p>
</blockquote>
<p>Hi Bruder !<br />
Wenn du bedenken hast, das etwas von ausserhalb an deiner globalen Variable knabbern könnte, dann mach die doch einfach static.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1488195</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1488195</guid><dc:creator><![CDATA[Big Brother]]></dc:creator><pubDate>Mon, 07 Apr 2008 19:39:12 GMT</pubDate></item></channel></rss>