<?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[Timer in einer Klasse]]></title><description><![CDATA[<p>Hi!</p>
<p>Ich möchte die Windows-Timer gerne in eine C++ Klasse stecken, doch irgendwie verliere ich Daten und weiß nicht wieso <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>Hier mal der Code:</p>
<pre><code class="language-cpp">#include &lt;windows.h&gt;
#include &lt;stdio.h&gt;

// callback die von jedem timer der klasse &quot;timer&quot; aufgerufen wird.
void CALLBACK TTimerProc(HWND hwnd, UINT message, UINT ptr, DWORD dwTimer);

class foo
{
private:
    // adresse auf sich selbst.
    UINT address_to_this_;
    // daten des timers die ausgegeben werden sollen in jedem interval
    char data_[80];

    // meine eigene timer-proc.
    void CALLBACK my_proc (HWND hwnd, UINT message, UINT ptr, DWORD dwTimer);

public:
    foo (char* data, int interval);
    ~foo (void);

    // freundschaft zur globalen funktion.
    friend void CALLBACK TTimerProc (HWND hwnd, UINT message, UINT ptr, DWORD dwTimer);
};

foo::foo (char* data, int interval)
{   
    strcpy (data_, data);

    address_to_this_ = reinterpret_cast&lt;UINT&gt;(this);
    SetTimer (NULL, address_to_this_, interval, (TIMERPROC)TTimerProc);
}

foo::~foo (void)
{
    KillTimer (NULL, address_to_this_);
}

void foo::my_proc (HWND hwnd, UINT message, UINT ptr, DWORD dwTimer)
{
    printf (&quot;%s\n&quot;, data_);
}

void CALLBACK TTimerProc (HWND hwnd, UINT message, UINT ptr, DWORD dwTimer)
{
    reinterpret_cast&lt;foo*&gt;(ptr)-&gt;my_proc (hwnd, message, ptr, dwTimer);
}

void ProcessTimerMessages()
{
    MSG msg;			
    BOOL ret;
    while (ret=GetMessage(&amp;msg, NULL, NULL, NULL))
    {
        if (ret != -1)
        {
            TranslateMessage(&amp;msg); 
            DispatchMessage(&amp;msg);
        }
    }
}

int main(int argc, char* argv[])
{
    foo x(&quot;hallo&quot;, 250);
    foo y(&quot;welt&quot;, 500);

    ProcessTimerMessages();

    return 0;
}
</code></pre>
<p>die &quot;my_proc&quot; wird problemlos aufgerufen, doch &quot;data_&quot; und &quot;this&quot; ist ein badptr <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>Jemand eine Idee was ich falsch mache?</p>
<p>Viele Grüße,<br />
Euer C++'ler</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/242565/timer-in-einer-klasse</link><generator>RSS for Node</generator><lastBuildDate>Sat, 04 Apr 2026 21:08:36 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/242565.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 05 Jun 2009 09:25:00 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Timer in einer Klasse on Fri, 05 Jun 2009 09:25:00 GMT]]></title><description><![CDATA[<p>Hi!</p>
<p>Ich möchte die Windows-Timer gerne in eine C++ Klasse stecken, doch irgendwie verliere ich Daten und weiß nicht wieso <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>Hier mal der Code:</p>
<pre><code class="language-cpp">#include &lt;windows.h&gt;
#include &lt;stdio.h&gt;

// callback die von jedem timer der klasse &quot;timer&quot; aufgerufen wird.
void CALLBACK TTimerProc(HWND hwnd, UINT message, UINT ptr, DWORD dwTimer);

class foo
{
private:
    // adresse auf sich selbst.
    UINT address_to_this_;
    // daten des timers die ausgegeben werden sollen in jedem interval
    char data_[80];

    // meine eigene timer-proc.
    void CALLBACK my_proc (HWND hwnd, UINT message, UINT ptr, DWORD dwTimer);

public:
    foo (char* data, int interval);
    ~foo (void);

    // freundschaft zur globalen funktion.
    friend void CALLBACK TTimerProc (HWND hwnd, UINT message, UINT ptr, DWORD dwTimer);
};

foo::foo (char* data, int interval)
{   
    strcpy (data_, data);

    address_to_this_ = reinterpret_cast&lt;UINT&gt;(this);
    SetTimer (NULL, address_to_this_, interval, (TIMERPROC)TTimerProc);
}

foo::~foo (void)
{
    KillTimer (NULL, address_to_this_);
}

void foo::my_proc (HWND hwnd, UINT message, UINT ptr, DWORD dwTimer)
{
    printf (&quot;%s\n&quot;, data_);
}

void CALLBACK TTimerProc (HWND hwnd, UINT message, UINT ptr, DWORD dwTimer)
{
    reinterpret_cast&lt;foo*&gt;(ptr)-&gt;my_proc (hwnd, message, ptr, dwTimer);
}

void ProcessTimerMessages()
{
    MSG msg;			
    BOOL ret;
    while (ret=GetMessage(&amp;msg, NULL, NULL, NULL))
    {
        if (ret != -1)
        {
            TranslateMessage(&amp;msg); 
            DispatchMessage(&amp;msg);
        }
    }
}

int main(int argc, char* argv[])
{
    foo x(&quot;hallo&quot;, 250);
    foo y(&quot;welt&quot;, 500);

    ProcessTimerMessages();

    return 0;
}
</code></pre>
<p>die &quot;my_proc&quot; wird problemlos aufgerufen, doch &quot;data_&quot; und &quot;this&quot; ist ein badptr <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>Jemand eine Idee was ich falsch mache?</p>
<p>Viele Grüße,<br />
Euer C++'ler</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1721608</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1721608</guid><dc:creator><![CDATA[C++&#x27;ler]]></dc:creator><pubDate>Fri, 05 Jun 2009 09:25:00 GMT</pubDate></item><item><title><![CDATA[Reply to Timer in einer Klasse on Fri, 05 Jun 2009 10:12:08 GMT]]></title><description><![CDATA[<p>Gehts dir nur darum, dass TimerProc eine Methode ist?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1721654</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1721654</guid><dc:creator><![CDATA[Dada2]]></dc:creator><pubDate>Fri, 05 Jun 2009 10:12:08 GMT</pubDate></item><item><title><![CDATA[Reply to Timer in einer Klasse on Fri, 05 Jun 2009 12:22:28 GMT]]></title><description><![CDATA[<p>Nein.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1721730</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1721730</guid><dc:creator><![CDATA[C++&#x27;ler]]></dc:creator><pubDate>Fri, 05 Jun 2009 12:22:28 GMT</pubDate></item><item><title><![CDATA[Reply to Timer in einer Klasse on Fri, 05 Jun 2009 12:56:27 GMT]]></title><description><![CDATA[<p>die Beschreibung in der MSDN erscheint mir auch ein bisschen widersprüchlich. Die SetTimer Funktion gibt einen identifier für den Timer zurück und diesen Wert bekommst du auch in deiner TimerProc geliefert. Dieser Wert ist jedoch nicht identisch mit dem, den du als dritten Parameter an die Funktion SetTimer übergibst. check ich grad auch ned ganz <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1721747</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1721747</guid><dc:creator><![CDATA[fix0r]]></dc:creator><pubDate>Fri, 05 Jun 2009 12:56:27 GMT</pubDate></item><item><title><![CDATA[Reply to Timer in einer Klasse on Fri, 05 Jun 2009 12:57:26 GMT]]></title><description><![CDATA[<p>äh als zweiten parameter meinte ich, ned als dritten.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1721748</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1721748</guid><dc:creator><![CDATA[fix0r]]></dc:creator><pubDate>Fri, 05 Jun 2009 12:57:26 GMT</pubDate></item><item><title><![CDATA[Reply to Timer in einer Klasse on Fri, 05 Jun 2009 13:02:50 GMT]]></title><description><![CDATA[<p>oh hab was wichtiges überlesen^^</p>
<p>MSDN schrieb:</p>
<blockquote>
<p>If the call is not intended to replace an existing timer, nIDEvent should be 0 if the hWnd is NULL.</p>
</blockquote>
<p><img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f921.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--clown_face"
      title=":clown:"
      alt="🤡"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1721754</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1721754</guid><dc:creator><![CDATA[fix0r]]></dc:creator><pubDate>Fri, 05 Jun 2009 13:02:50 GMT</pubDate></item><item><title><![CDATA[Reply to Timer in einer Klasse on Sun, 07 Jun 2009 13:27:41 GMT]]></title><description><![CDATA[<p>und wie mache ich das jetzt? hat da jemand eine idee? <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>
]]></description><link>https://www.c-plusplus.net/forum/post/1722734</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1722734</guid><dc:creator><![CDATA[C++&#x27;ler]]></dc:creator><pubDate>Sun, 07 Jun 2009 13:27:41 GMT</pubDate></item><item><title><![CDATA[Reply to Timer in einer Klasse on Sun, 07 Jun 2009 14:20:15 GMT]]></title><description><![CDATA[<p>ach man lass dir halt was einfallen. wenns ned super viele timer sind kannste ja zb ne liste von objekt adressen und timer_id paaren anlegen und in der TTimerProc diese dann durchgehen um von einer timer_id auf ne objekt adresse zu kommen. da gibts doch wahrlich viele möglichkeiten</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1722754</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1722754</guid><dc:creator><![CDATA[fix0r]]></dc:creator><pubDate>Sun, 07 Jun 2009 14:20:15 GMT</pubDate></item><item><title><![CDATA[Reply to Timer in einer Klasse on Sun, 07 Jun 2009 14:44:17 GMT]]></title><description><![CDATA[<p>für jeden Timer ein unsichbares Fenster erstellen und dieses dann bei SetTimer angeben. mit SetWindowLongPtr den this pointer setzen und mit GetWindowLongPtr abfragen</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1722769</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1722769</guid><dc:creator><![CDATA[tztrh]]></dc:creator><pubDate>Sun, 07 Jun 2009 14:44:17 GMT</pubDate></item><item><title><![CDATA[Reply to Timer in einer Klasse on Sun, 07 Jun 2009 14:47:45 GMT]]></title><description><![CDATA[<p>@ fix0r<br />
du bist die misserabelste hilfe die man sich nur vorstellen kann</p>
<p>@tztrh<br />
danke, das klingt nach nem plan, ich probiers sofort aus.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1722771</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1722771</guid><dc:creator><![CDATA[C++&#x27;ler]]></dc:creator><pubDate>Sun, 07 Jun 2009 14:47:45 GMT</pubDate></item><item><title><![CDATA[Reply to Timer in einer Klasse on Sun, 07 Jun 2009 14:48:09 GMT]]></title><description><![CDATA[<p>ohne meine hilfe würde es dich gar nicht geben also maul</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1722772</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1722772</guid><dc:creator><![CDATA[fix0r]]></dc:creator><pubDate>Sun, 07 Jun 2009 14:48:09 GMT</pubDate></item><item><title><![CDATA[Reply to Timer in einer Klasse on Sun, 07 Jun 2009 20:09:12 GMT]]></title><description><![CDATA[<p>Ich möchte das die Mods die obrige Beleidigung von fix0r bitte entfernen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1722963</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1722963</guid><dc:creator><![CDATA[C++&#x27;ler]]></dc:creator><pubDate>Sun, 07 Jun 2009 20:09:12 GMT</pubDate></item><item><title><![CDATA[Reply to Timer in einer Klasse on Sun, 07 Jun 2009 20:10:06 GMT]]></title><description><![CDATA[<p>l0l</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1722966</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1722966</guid><dc:creator><![CDATA[fix0r]]></dc:creator><pubDate>Sun, 07 Jun 2009 20:10:06 GMT</pubDate></item></channel></rss>