Valhalla Legends Forums Archive | C/C++ Programming | Getting hWnd from inside a class

AuthorMessageTime
Spilled[DW]
How would I get the handle of my application from within one of my classes? I could use GetActiveWindow() but if the application is not in focus it would return the wrong hWnd wouldnt it?
September 19, 2006, 7:01 PM
Myndfyr
[quote author=Spilled link=topic=15748.msg158466#msg158466 date=1158692468]
How would I get the handle of my application from within one of my classes? I could use GetActiveWindow() but if the application is not in focus it would return the wrong hWnd wouldnt it?
[/quote]

Well, which handle are you trying to get?  The HINSTANCE of your application (which is what your first question was), or the HWND to your main window?

Also, are you using MFC or wxWidgets or something else?
September 19, 2006, 10:08 PM
Spilled[DW]
[quote author=MyndFyre[vL] link=topic=15748.msg158473#msg158473 date=1158703713]
[quote author=Spilled link=topic=15748.msg158466#msg158466 date=1158692468]
How would I get the handle of my application from within one of my classes? I could use GetActiveWindow() but if the application is not in focus it would return the wrong hWnd wouldnt it?
[/quote]

Well, which handle are you trying to get?  The HINSTANCE of your application (which is what your first question was), or the HWND to your main window?

Also, are you using MFC or wxWidgets or something else?
[/quote]

HWND to my main window because i need to Initiate a Timer from the class but right now im trying to Initiate a Timer with a TimerProc in the class so i can handle the timer inside the class but I am having problems with that... Here is some code:

[code]
void QueueShit::Send(std::string message)
{
    if(!Queue)
    {
        //Send the Message and start the timer;
        global->packet.Buffer.InsertNTString(message);
          if(!global->packet.Buffer.SendBNCSPacket(global->packet.Buffer.wSock, SID_CHATCOMMAND))
          {
                global->packet.connected = false;
                closesocket( global->packet.Buffer.wSock ); 
          }else{
              global->Queue << cWhite << "<" << global->Config.getUsername() << "> " << cGreen << message << "\n";
              Queue = true;
              SetTimer(NULL,187,2500, (TIMERPROC)MyTimerProc);
             
          }
    }else{
        SendQueue.push(message);
    }
}
[/code]

Here if the std::queue class has a message to send it sends it then starts a timer to prevent flooding... Underneat that i have my TimerProc like so:

[code]
VOID CALLBACK MyTimerProc(HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime )
{
    switch(uMsg)
    {
    case WM_TIMER:
          {
    MessageBox(NULL,"TICK","TICK",MB_OK);
                    if(SendQueue.empty())
                    {
                        //the Queue is empty
                        KillTimer(NULL,187);
                        Queue = false;
                    }else{
                          global->packet.Buffer.InsertNTString(SendQueue.front());
                          if(!global->packet.Buffer.SendBNCSPacket(global->packet.Buffer.wSock, SID_CHATCOMMAND))
                          {
                              global->packet.connected = false;
                              closesocket( global->packet.Buffer.wSock );
                          }
                          global->Queue << cWhite << "<" << global->Config.getUsername() << "> " << cGreen << SendQueue.front() << "\n";
                          SendQueue.pop();
                    }
          }
          break;
    }
}
[/code]

As of right now, it sends the first message i send to the Send() method then nothing. It is not making it to the TimerProc... I'm not too familiar with using a TimerProc sorry, I usually handle my timers in the WindowsProcedure .... Thanks for the quick reply MyndFyre
September 19, 2006, 10:19 PM
Myndfyr
[quote]
[code]
SetTimer(NULL,187,2500, (TIMERPROC)MyTimerProc);
[/code]
[/quote]
Have you read the function documentation of SetTimer?  Of particular interest is the second parameter, which is ignored if the first parameter is NULL.

Also, the return value is interesting:
[quote author=MSDN]An integer identifying the new timer indicates success. An application can pass this value, or the string identifier, if it exists, to the KillTimer function to destroy the timer. Zero indicates failure. To get extended error information, call GetLastError.[/quote]
What return value are you getting? 

Another thing that might be catching you is that you're using classes.  Is MyTimerProc a class function?  It doesn't look like it, so if that's the case (that it isn't), this won't be getting you in trouble.  If it is a class function, then you could be in trouble (you need to call the function on the right object).

Also, I noticed that your KillTimer call is wrong.  The second parameter isn't the identifier used to kill the timer - the return value from the SetTimer function is.  The second parameter of SetTimer (what you specified as 187) is used to create multiple timers for the same window and differentiate them.
September 19, 2006, 11:45 PM
Spilled[DW]
[quote author=MyndFyre[vL] link=topic=15748.msg158479#msg158479 date=1158709550]
[quote]
[code]
SetTimer(NULL,187,2500, (TIMERPROC)MyTimerProc);
[/code]
[/quote]
Have you read the function documentation of SetTimer?  Of particular interest is the second parameter, which is ignored if the first parameter is NULL.

Also, the return value is interesting:
[quote author=MSDN]An integer identifying the new timer indicates success. An application can pass this value, or the string identifier, if it exists, to the KillTimer function to destroy the timer. Zero indicates failure. To get extended error information, call GetLastError.[/quote]
What return value are you getting? 

Another thing that might be catching you is that you're using classes.  Is MyTimerProc a class function?  It doesn't look like it, so if that's the case (that it isn't), this won't be getting you in trouble.  If it is a class function, then you could be in trouble (you need to call the function on the right object).

Also, I noticed that your KillTimer call is wrong.  The second parameter isn't the identifier used to kill the timer - the return value from the SetTimer function is.  The second parameter of SetTimer (what you specified as 187) is used to create multiple timers for the same window and differentiate them.
[/quote]

Ok, I updated the SetTimer() and the KillTimer() as you said and according to msdn:

[code]
void QueueShit::Send(std::string message)
{
    if(!Queue)
    {
        //Send the Message and start the timer;
        global->packet.Buffer.InsertNTString(message);
          if(!global->packet.Buffer.SendBNCSPacket(global->packet.Buffer.wSock, SID_CHATCOMMAND))
          {
                global->packet.connected = false;
                closesocket( global->packet.Buffer.wSock ); 
          }else{
              global->Queue << cWhite << "<" << global->Config.getUsername() << "> " << cGreen << message << "\n";
              Queue = true;
              TimerIdent = SetTimer(NULL,0,2500, (TIMERPROC)MyTimerProc);
              if(TimerIdent == 0)
              {
                  global->Queue << cRed << "Failed to Create Timer!\n";
              } 
             
          }
    }else{
        SendQueue.push(message);
    }
}
VOID CALLBACK MyTimerProc(HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime )
{
    switch(uMsg)
    {
    case WM_TIMER:
          {
                    if(SendQueue.empty())
                    {
                        //the Queue is empty
                        KillTimer(NULL,TimerIdent);
                        Queue = false;
                    }else{
                          global->packet.Buffer.InsertNTString(SendQueue.front());
                          if(!global->packet.Buffer.SendBNCSPacket(global->packet.Buffer.wSock, SID_CHATCOMMAND))
                          {
                              global->packet.connected = false;
                              closesocket( global->packet.Buffer.wSock ); 
                          }
                          global->Queue << cWhite << "<" << global->Config.getUsername() << "> " << cGreen << SendQueue.front() << "\n";
                          SendQueue.pop();
                    }
          }
          break;
    }
}
[/code]

But I dont quite get what your saying about the function being part of the class... Should it be part of the class or not?
September 20, 2006, 12:26 AM
Myndfyr
No, it shouldn't be part of the class.
September 20, 2006, 12:38 AM
Spilled[DW]
Ok, it's not part of the class so i declared it as a prototype at the top of the cpp. Here is my code:

[code]
void QueueShit::Send(std::string message)
{
     if(!Queue)
     {
        //Send the Message and start the timer;
        global->packet.Buffer.InsertNTString(message);
           if(!global->packet.Buffer.SendBNCSPacket(global->packet.Buffer.wSock, SID_CHATCOMMAND))
           {
                global->packet.connected = false;
                closesocket( global->packet.Buffer.wSock );   
           }else{
               global->Queue << cWhite << "<" << global->Config.getUsername() << "> " << cGreen << message << "\n";
               Queue = true;
               TimerIdent = SetTimer(NULL,0,2500, (TIMERPROC)MyTimerProc);
               if(TimerIdent == 0)
               {
                  global->Queue << cRed << "Failed to Create Timer!\n";
               }else{
                  char b[2];
                  MessageBox(NULL,itoa(TimerIdent,b,10),"HEHE",MB_OK);
               }
               
           }
     }else{
        SendQueue.push(message);
     }
}
VOID CALLBACK MyTimerProc(HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime )
{
     switch(uMsg)
     {
     case WM_TIMER:
          {
                     if(SendQueue.empty())
                     {
                         //the Queue is empty
                         KillTimer(NULL,TimerIdent);
                         Queue = false;
                     }else{
                          global->packet.Buffer.InsertNTString(SendQueue.front());
                          if(!global->packet.Buffer.SendBNCSPacket(global->packet.Buffer.wSock, SID_CHATCOMMAND))
                          {
                              global->packet.connected = false;
                              closesocket( global->packet.Buffer.wSock ); 
                          }
                          global->Queue << cWhite << "<" << global->Config.getUsername() << "> " << cGreen << SendQueue.front() << "\n";
                          SendQueue.pop();
                     }
          }
          break;
     }
}
[/code]

Still not reaching the TimerProc, Anything else you can see that im doing wrong? It's still in the Classes cpp but not declared as part of the class... is that what you meant? because if i was to put the TimerProc in the main.cpp how would i address it?
September 20, 2006, 4:30 AM

Search