Click here to Skip to main content
15,900,725 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello !

I have a problem when enumerating all visible windows.
My calback function which is "doing the job" is shown below.
As you can see I am writing every caption i find together with its handle into a struct defined in handlestruct.h.

This works just fine when I call
EnumWindows(EnumVisiWindowTitles, NULL);
from a main which is stored in the same file.

BUT: I want to call EnumWindows(EnumVisiWindowTitles, NULL) from a main which is located in a different cpp in the project.
Depending on where I define MYHANDLES lumpi[10] it's known in the main OR in the callback function.
So my question is: I would like to call EnumWindows(EnumVisiWindowTitles, NULL) and hand over a pointer to lumpi[0]. But I dont know how to it over because it is no LPARAM... I've tried to typecast but with no sucess. Does anyone have any ideas? :doh:

Tahnk you for any help!


#include "handlestruct.h"
MYHANDLES lumpi[10];

using namespace std;
	
 BOOL CALLBACK EnumVisiWindowTitles(HWND hWnd, LPARAM lparam) 
{		
	TCHAR String[200];	
		
	if (!hWnd)
		return TRUE;// Not a window, return TRUE to Enumwindows in order to get the next handle
	if (!::IsWindowVisible(hWnd))
		return TRUE;// Not visible, return TRUE to Enumwindows in order to get the next handle	
	if (!SendMessageW(hWnd, WM_GETTEXT, sizeof(String), (LPARAM)String))
		return TRUE;// No window title, return TRUE to Enumwindows in order to get the next handle
		lumpi[lumpi[0].count].haendchen = hWnd;
		
			for (int n=0; n<201; n++)//copy the caption to lumpi struct
				{
					lumpi[lumpi[0].count].title[n] = String[n];
				}
			
			lumpi[0].count++;		//Increase counter
			wcout<<String<<'\n';
			return true;			//return true to get next handle			
}
Posted
Updated 14-Dec-10 10:45am
v2

class CEnumWindows
{
public:
  CEnumWindows(){}
  ~CEnumWindows(){ FreeAll(); }

  int	Collect(){ FreeAll(); return EnumWindows(&CEnumWindows::__fnEnum,(LPARAM)this); }

protected:
  long	GetWindowInfos(HWND hwnd)
  {
    TCHAR txt[512];
    int   len;
    ASSERT(IsWindow(hwnd)); // this should never happens
    if(len=GetWindowText(hwnd,txt,sizeof(txt)/sizeof(txt[0])))
      AddCaption(txt,len);
    return 1; // continue enum
  }
	
  void	AddCaption(const TCHAR* txt,const int len){ /* do anything... */ }
  void	FreeAll(){ /* free your storage */ }

private:
  static int FAR PASCAL __fnEnum(HWND hwnd,LPARAM p){ return p?((CEnumWindows*)p)->GetWindowInfos(hwnd):0; }
};
 
Share this answer
 
v3
Hi and thank you all for your help.
I did get the answer yesterday at

http://stackoverflow.com/questions/4460972/passing-a-pointer-as-lparam-to-enumwindowsproc-how[^]

but could not post it here due to some server error...

Thanks to everyone who spend time on my question.

Happy coding !
:laugh:
 
Share this answer
 
How exactly did you do your type cast? LPARAM and a pointer should be compatible; perhaps you tried to type cast MYHANDLES directly?
This:
(LPARAM) lumpi[0]

might not work (depending on how many bits MYHANDLES is), but this:
(LPARAM) &lumpi[0]

should be just fine.

The easy, less elegant, way would be to just add this:
extern MYHANDLES lumpi[10];

in the 'other' cpp file.
 
Share this answer
 
v2
Comments
stackhoover 15-Dec-10 16:10pm    
Hi !

Thank you for your help.
It got me on the way a bit...

Right now in my main it looks like this:

Collapse


BOOL CALLBACK EnumVisiWindowTitles(HWND hWnd, LPARAM lparam);

int _tmain(int argc, _TCHAR* argv[])
{

MYHANDLES lumpi[10];
EnumWindows(EnumVisiWindowTitles, (LPARAM) &lumpi[0]);


My Callback funktion I have changed to:

Collapse

BOOL CALLBACK EnumVisiWindowTitles(HWND hWnd, LPARAM lumpi)
{

bla bla

lumpi[lumpi[0].count].haendchen = hWnd;

bla bla
{


The compiler underlines [0] and says:Expression must have pointer to object type. I have tried everything I could imagine in referencing and dereferencing but ...naaaat Sigh
Maybe someone has the final hint for me.
Thanks
[no name] 15-Dec-10 16:18pm    
You pass &lumpi[0] to EnumVisiWindowTitles, and receive it there as LPARAM lumpi. So within that function, the variable named lumpi holds what was previously known as &lumpi[0] in _tmain.
In other words, within EnumVisiWindowTitles, you can use *lumpi to access your data.
If the problem is that one file has the variable and another doesn't an easy fix is to put
MYHANDLES lumpi[10]
in one file and
extern MYHANDLES lumpi[10]
in the other(s).

As for passing the handle via an LPARAM the problem is that lumpi[0] is the first element in lumpi, not a pointer to lumpi hence the error. Also &lumpi[0] is the address of the first element and is the same as lumpi so (LPARAM)lumpi should work fine;


For reference I use this code in one of my programs and it works quite well.
vector<hwnd> hWndVector;

...
EnumThreadWindows(Thread.th32ThreadID, (WNDENUMPROC)EnumThreadWndProc, (LPARAM)&hWndVector);
...

BOOL CALLBACK EnumThreadWndProc(HWND hwnd, LPARAM lParam)
{
    vector<HWND>* phWndVector = (vector<HWND>*)lParam;
    phWndVector->push_back(hwnd);
    return TRUE;
}</hwnd>
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900