Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 242
  • Last Modified:

rewriting into a function; specific code

#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
Troudeloup
Asked:
Troudeloup
  • 5
  • 5
  • 2
3 Solutions
 
Infinity08Commented:
Troudeloup ... weren't the answers we gave in your other thread good ?
0
 
Infinity08Commented:
>> 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
 
jkrCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
TroudeloupAuthor Commented:
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
 
TroudeloupAuthor Commented:
actually there is a way to solve it,
0
 
jkrCommented:
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
 
jkrCommented:
... 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
 
TroudeloupAuthor Commented:
yep, this is dealt with :D
0
 
TroudeloupAuthor Commented:
would you take look at my screenshot and keyboard problem?
0
 
jkrCommented:
Need to do a little wor also, but I will try to ;o)
0
 
TroudeloupAuthor Commented:
what is wor?
0
 
jkrCommented:
Sorry -> work
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now