Solved

rewriting into a function; specific code

Posted on 2007-11-28
12
234 Views
Last Modified: 2010-04-01
#include <windows.h>
#include <iostream>
#include <vector>
#include <string.h>
using namespace std;

vector<HWND> vw; // array for HWND

BOOL CALLBACK EnumWindowsProc(HWND hwnd,LPARAM lParam)
{
      char buf [1024];
      GetWindowText(hwnd,buf,1024);

      if(!strstr(buf,(char*)lParam)) return true;

       vw.push_back(hwnd);

      return true;
}

int main()
{
      EnumWindows(EnumWindowsProc,(LPARAM)"Star Wars Galaxies");

      Sleep ( 1000 );
      
      for (vector<HWND>::iterator i = vw.begin(); i != vw.end(); ++i)
      {
      // *i   = hwnd
            
            

      }

   return 0;
}





I want to rewrite this into one function so that it returns  hwnd array



can I do this to receive it?


hwnd array[5];

array = return_handle();

when I know that the array handle it returns has less than 5 elements?
0
Comment
Question by:Troudeloup
  • 5
  • 5
  • 2
12 Comments
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 100 total points
ID: 20368141
Troudeloup ... weren't the answers we gave in your other thread good ?
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 100 total points
ID: 20368146
>> can I do this to receive it?
>> 
>> hwnd array[5];
>> 
>> array = return_handle();
>> 
>> when I know that the array handle it returns has less than 5 elements?

No. As we said: try using a vector instead.
0
 
LVL 86

Accepted Solution

by:
jkr earned 400 total points
ID: 20368150
In short - that won't work. You need the callback as a separate function, since it is actually called by Windows itself. What you could do would be

#include <windows.h>
#include <iostream>
#include <vector>
#include <string.h>
using namespace std;

vector<HWND> vw; // array for HWND

BOOL CALLBACK EnumWindowsProc(HWND hwnd,LPARAM lParam)
{
      char buf [1024];
      GetWindowText(hwnd,buf,1024);

      if(!strstr(buf,(char*)lParam)) return true;

       vw.push_back(hwnd);

      return true;
}

vector<HWND>& GetAllHWNDs(const char* pName)
{
    EnumWindows(EnumWindowsProc,(LPARAM)pName);

    return vw;
}

int main()
{
      vector<HWND> array = GetAllHWNDs("Star Wars Galaxies");

      Sleep ( 1000 );
     
      for (vector<HWND>::iterator i = vw.begin(); i != vw.end(); ++i)
      {
      // *i   = hwnd
           
           

      }

   return 0;
}
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:Troudeloup
ID: 20368171
can i do this?


function()
{
      vector<HWND> array = GetAllHWNDs("Star Wars Galaxies");

      Sleep ( 1000 );
     
      for (vector<HWND>::iterator i = vw.begin(); i != vw.end(); ++i)
      {
      // *i   = hwnd
           
           

      }
}
0
 

Author Comment

by:Troudeloup
ID: 20368210
actually there is a way to solve it,
0
 
LVL 86

Expert Comment

by:jkr
ID: 20368211
Yes, that's well possible, e.g.

#include <windows.h>
#include <iostream>
#include <vector>
#include <string.h>
using namespace std;

vector<HWND> vw; // array for HWND

BOOL CALLBACK EnumWindowsProc(HWND hwnd,LPARAM lParam)
{
      char buf [1024];
      GetWindowText(hwnd,buf,1024);

      if(!strstr(buf,(char*)lParam)) return true;

       vw.push_back(hwnd);

      return true;
}

vector<HWND>& GetAllHWNDs(const char* pName)
{
    EnumWindows(EnumWindowsProc,(LPARAM)pName);

    return vw;
}

void UseHWNDs(vector<HWND>& array)
{
      for (vector<HWND>::iterator i = array.begin(); i != array.end(); ++i)
      {
      // *i   = hwnd
           
           

      }
}

int main()
{
      vector<HWND> array = GetAllHWNDs("Star Wars Galaxies");

      UseHWNDs(array);

   return 0;
}
0
 
LVL 86

Expert Comment

by:jkr
ID: 20368221
... it is just that 'EnumWindowsProc()' has to be kept as a separate function, since this one is called by the operating system, not your code.
0
 

Author Comment

by:Troudeloup
ID: 20368232
yep, this is dealt with :D
0
 

Author Comment

by:Troudeloup
ID: 20368248
would you take look at my screenshot and keyboard problem?
0
 
LVL 86

Expert Comment

by:jkr
ID: 20368254
Need to do a little wor also, but I will try to ;o)
0
 

Author Comment

by:Troudeloup
ID: 20368260
what is wor?
0
 
LVL 86

Expert Comment

by:jkr
ID: 20368271
Sorry -> work
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

823 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question