<?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[CreateThread funktioniert nicht]]></title><description><![CDATA[<p>Hallo<br />
Ich will ein neues Thread erzeugen mit Hilfe von CreateThread()! Mein Quelltext wird Problemlos übersetzt aber wenn ich das Program ausführe wird die Funktion des Threads nicht ausgeführt und es erscheint eine Meldung das ein Fehler aufgetreten ist (.... Funktioniert nicht mehr! Es wird nach einer Lösung für das Problem gesucht!) Mein Code:</p>
<pre><code>#include &quot;stdafx.h&quot;
#include &quot;windows.h&quot;
#include &lt;iostream&gt;
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	HINSTANCE hDLL = LoadLibrary(&quot;...DLL.dll&quot;);
	if(hDLL!=NULL){
		cout &lt;&lt; &quot;DLL.dll geladen\n&quot;;
	}
	FARPROC funktion = GetProcAddress(hDLL, &quot;funktion&quot;);
	if(funktion!=NULL){
		cout &lt;&lt; &quot;Funktion geladen\n&quot;;
	}
/// Bis hierher funktionierts
	int i = 555;
	LPVOID p =&amp;i;
	CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) funktion, p, 0, NULL);

	FreeLibrary(hDLL);
	system(&quot;Pause&quot;);
	return 0;
}
</code></pre>
<p>Und die DLL:</p>
<pre><code>#include &lt;windows.h&gt;
#include &lt;iostream&gt;
#include &quot;DLL.h&quot;
using namespace std;

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
					 )
{
	switch (ul_reason_for_call)
	{
	case DLL_PROCESS_ATTACH:
		cout &lt;&lt; &quot;DLL_PROCESS_ATTACH&quot;;
		break;
	case DLL_THREAD_ATTACH:
		cout &lt;&lt; &quot;DLL_THREAD_ATTACH&quot;;
		break;
	case DLL_THREAD_DETACH:
		cout &lt;&lt; &quot;DLL_THREAD_DETACH&quot;;
		break;
	case DLL_PROCESS_DETACH:
		cout &lt;&lt; &quot;DLL_PROCESS_DETACH&quot;;
		break;
	}
	return TRUE;
}

EXPORT bool funktion(LPVOID i){
	cout &lt;&lt; i;
	return true;
}
</code></pre>
<p>EXPORT ist ein Makro und ist für c++ so definiert:</p>
<pre><code>#define EXPORT extern &quot;C&quot; __declspec(dllexport)
</code></pre>
<p>Wie kann ich LPVOID Variable einen int oder char wert zuweisen?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/257670/createthread-funktioniert-nicht</link><generator>RSS for Node</generator><lastBuildDate>Thu, 10 Sep 2026 03:38:13 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/257670.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 01 Jan 2010 15:56:45 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to CreateThread funktioniert nicht on Fri, 01 Jan 2010 15:56:45 GMT]]></title><description><![CDATA[<p>Hallo<br />
Ich will ein neues Thread erzeugen mit Hilfe von CreateThread()! Mein Quelltext wird Problemlos übersetzt aber wenn ich das Program ausführe wird die Funktion des Threads nicht ausgeführt und es erscheint eine Meldung das ein Fehler aufgetreten ist (.... Funktioniert nicht mehr! Es wird nach einer Lösung für das Problem gesucht!) Mein Code:</p>
<pre><code>#include &quot;stdafx.h&quot;
#include &quot;windows.h&quot;
#include &lt;iostream&gt;
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	HINSTANCE hDLL = LoadLibrary(&quot;...DLL.dll&quot;);
	if(hDLL!=NULL){
		cout &lt;&lt; &quot;DLL.dll geladen\n&quot;;
	}
	FARPROC funktion = GetProcAddress(hDLL, &quot;funktion&quot;);
	if(funktion!=NULL){
		cout &lt;&lt; &quot;Funktion geladen\n&quot;;
	}
/// Bis hierher funktionierts
	int i = 555;
	LPVOID p =&amp;i;
	CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) funktion, p, 0, NULL);

	FreeLibrary(hDLL);
	system(&quot;Pause&quot;);
	return 0;
}
</code></pre>
<p>Und die DLL:</p>
<pre><code>#include &lt;windows.h&gt;
#include &lt;iostream&gt;
#include &quot;DLL.h&quot;
using namespace std;

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
					 )
{
	switch (ul_reason_for_call)
	{
	case DLL_PROCESS_ATTACH:
		cout &lt;&lt; &quot;DLL_PROCESS_ATTACH&quot;;
		break;
	case DLL_THREAD_ATTACH:
		cout &lt;&lt; &quot;DLL_THREAD_ATTACH&quot;;
		break;
	case DLL_THREAD_DETACH:
		cout &lt;&lt; &quot;DLL_THREAD_DETACH&quot;;
		break;
	case DLL_PROCESS_DETACH:
		cout &lt;&lt; &quot;DLL_PROCESS_DETACH&quot;;
		break;
	}
	return TRUE;
}

EXPORT bool funktion(LPVOID i){
	cout &lt;&lt; i;
	return true;
}
</code></pre>
<p>EXPORT ist ein Makro und ist für c++ so definiert:</p>
<pre><code>#define EXPORT extern &quot;C&quot; __declspec(dllexport)
</code></pre>
<p>Wie kann ich LPVOID Variable einen int oder char wert zuweisen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1830614</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1830614</guid><dc:creator><![CDATA[Thread]]></dc:creator><pubDate>Fri, 01 Jan 2010 15:56:45 GMT</pubDate></item><item><title><![CDATA[Reply to CreateThread funktioniert nicht on Fri, 01 Jan 2010 16:17:55 GMT]]></title><description><![CDATA[<p>Errorcode?</p>
<p>Achja: <a href="http://www.c-plusplus.net/forum/viewforum-var-f-is-4.html" rel="nofollow">http://www.c-plusplus.net/forum/viewforum-var-f-is-4.html</a></p>
<blockquote>
<p>Wie kann ich LPVOID Variable einen int oder char wert zuweisen?</p>
</blockquote>
<p>LPVOID ist ein void Zeiger, mit dem kannst du auf alles zeigen.<br />
Wenn du viele Daten übergeben möchtest, würde es sich anbieten einen Zeiger auf eine Struktur mit Daten zu übergeben.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1830623</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1830623</guid><dc:creator><![CDATA[Icematix]]></dc:creator><pubDate>Fri, 01 Jan 2010 16:17:55 GMT</pubDate></item><item><title><![CDATA[Reply to CreateThread funktioniert nicht on Fri, 01 Jan 2010 16:26:59 GMT]]></title><description><![CDATA[<p>Kann es sein dass FreeLibrary dem Thread die Funktion unterm A*** wegreißt?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1830629</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1830629</guid><dc:creator><![CDATA[l&#x27;abra d&#x27;or]]></dc:creator><pubDate>Fri, 01 Jan 2010 16:26:59 GMT</pubDate></item><item><title><![CDATA[Reply to CreateThread funktioniert nicht on Fri, 01 Jan 2010 16:28:31 GMT]]></title><description><![CDATA[<p>Also ich hab mit getlasterror() abfragen lassen und hab folgendes ausgegeben bekommen:<br />
01244020<br />
Wo kann ich nachschaun was das bedeutet? Bei Google hab ich nichts gefunden!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1830630</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1830630</guid><dc:creator><![CDATA[Loader]]></dc:creator><pubDate>Fri, 01 Jan 2010 16:28:31 GMT</pubDate></item><item><title><![CDATA[Reply to CreateThread funktioniert nicht on Fri, 01 Jan 2010 16:49:22 GMT]]></title><description><![CDATA[<p>1. Warte bis der neu erzeugte Thread sich beendet hat bevor Du FreeLibrary(..) aufrufst. Das kannst Du z.B. mit WaitForSingleObject(..) tun.</p>
<p>2. CreateThread(..) gibt ein HANDLE auf den neuen Thread zurück. Das musst Du mit CloseHandle(..) schliessen, sobald Du es nicht mehr benötigst. Beachte, dass die Thread Funktion auch weiter ausgeführt wird, wenn bereits CloseHandle(..) aufgerufen wurde.</p>
<p>3. Die Signatur der Thread Funktion ist nicht korrekt, sie sollte wie folgt sein:</p>
<pre><code class="language-cpp">DWORD WINAPI myThreadFunc(LPVOID args);
</code></pre>
<p>Der cast nach LPTHREAD_START_ROUTINE darf nicht mehr nötig sein! Der cast ist ein Fehler!<br />
(Ev. kannst Du das so nicht exportieren, dann würde eine zwischen Funktion aushelfen.)</p>
<p>4. GetLastError(..) hilft nur, wenn eine Funktion fehlschlägt; z.B. CreateThread(..) gibt NULL zurück, dann kannst Du mit GetLastError(..) mehr Info rausholen.<br />
Die Error Codes können dann im VS unter Tools &gt; Error Lookup nachgeschaut werden, oder natürlich online: <a href="http://msdn.microsoft.com/en-us/library/ms681381(VS.85).aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/ms681381(VS.85).aspx</a></p>
<p>5. Lies Dir den Hinweis bezüglich CRT (C- Runtime) und CreateThread(..) bei der Doku zu CreateThread. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/27a1.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--right_arrow"
      title=":arrow_right:"
      alt="➡"
    /> _beginthread(..) / _beginthreadex(..)</p>
<p>6. Via dem LPVOID argument übergibt man dem Thread üblicherweise parameter für die Thread Funktion. Es einfach ein Zeiger, der durchgereicht wird. Achtung: Das Memory auf das er zeigt muss natürlich solange gültig sein, wie es in der Thread Funktion benötigt wird.</p>
<p>7. Generelle Frage:<br />
Warum machst Du überhaupt ein Thread. Beim gezeigten Code macht das jedenfalls keinen Sinn. Ist das nur ein Test Programm / Forumscode oder die &quot;richtige&quot; Applikation?</p>
<p>Hier noch die Doku zu CrateThread(..): <a href="http://msdn.microsoft.com/en-us/library/ms682453(VS.85).aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/ms682453(VS.85).aspx</a></p>
<p>Simon</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1830638</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1830638</guid><dc:creator><![CDATA[theta]]></dc:creator><pubDate>Fri, 01 Jan 2010 16:49:22 GMT</pubDate></item></channel></rss>