?
Solved

rewriting into a function; specific code

Posted on 2007-11-28
12
Medium Priority
?
239 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 2
12 Comments
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 400 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 400 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 1600 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

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!

Question has a verified solution.

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.
Suggested Courses

765 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