Solved

rewriting into a function; specific code

Posted on 2007-11-28
12
232 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
 

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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now