• Welcome to Valhalla Legends Archive.
 

Getting hWnd from inside a class

Started by Spilled, September 19, 2006, 02:01 PM

Previous topic - Next topic

Spilled

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?

MyndFyre

Quote from: Spilled on September 19, 2006, 02:01 PM
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?

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?
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Spilled

Quote from: MyndFyre[vL] on September 19, 2006, 05:08 PM
Quote from: Spilled on September 19, 2006, 02:01 PM
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?

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?

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:


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);
     }
}


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:


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;
     }
}


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

MyndFyre

Quote

SetTimer(NULL,187,2500, (TIMERPROC)MyTimerProc);

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 from: MSDNAn 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.
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.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Spilled

Quote from: MyndFyre[vL] on September 19, 2006, 06:45 PM
Quote

SetTimer(NULL,187,2500, (TIMERPROC)MyTimerProc);

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 from: MSDNAn 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.
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.

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


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;
     }
}


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?

MyndFyre

No, it shouldn't be part of the class.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Spilled

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:


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;
     }
}


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?