Please help with EnumWindow function  
Author Message
Olyx





PostPosted: Visual C++ Language, Please help with EnumWindow function Top

code:

#pragma once

#include <TCHAR.H >

#include <windows.h>

#include <vector>

// Type declarations

struct TWindowItem {

HWND Descriptor;

char Caption;

char ClassName;

};

//Function declaretions

BOOL EnumWindowsProc(HWND WHandle, long lParam);

.......

namespace OoMetaPanel2 {

vector<TWindowItem> WindowList (0);

...................................

}

.........................

private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)

{

WindowList.clear();

EnumWindows( (WNDENUMPROC)EnumWindowsProc, 0 );

for each (TWindowItem item in WindowList)

{

listBox1->Items->Add(item.Caption);

};

}

.......................

BOOL EnumWindowsProc(HWND WHandle, long lParam)

{

LPWSTR Buff;

LPWSTR NameOfClass;

GetWindowText(WHandle, Buff, 254);

GetClassName(WHandle, NameOfClass, 254);

TWindowItem tempitem;

if (Buff)

{

char* cpt = new char [wcslen(Buff) + 1];

wsprintfA ( cpt, "%S", Buff);

tempitem.Caption = *cpt;

}

if (NameOfClass)

{

char* noc = new char [wcslen(NameOfClass) + 1];

wsprintfA ( noc, "%S", NameOfClass);

tempitem.ClassName = *noc;

}

OoMetaPanel2::WindowList.push_back(tempitem);

return TRUE;

};

This code should display captions of all available windows. But I have some problems that I can`n solve:

1. Only some windows are opened but EnumWindowsProc executes lot of times until application crashes

2. Seems that EnumWindowsProc gets incorrect window handles, because GetWindowText and getClassName always returns a garbage

3. Before first EnumWindowsProc execution time array WindowList is empty. After first OoMetaPanel2::WindowList.push_back(tempitem); it is full (more than 100000 recordsd) with garbage.

Please help! Where is the mistake or why I got such results.




Visual C++2  
 
 
Marius Bancila





PostPosted: Visual C++ Language, Please help with EnumWindow function Top

You are doing a lot of error. I believe you don't understand the data types.

This is the first problem:

struct TWindowItem {

HWND Descriptor;

char Caption;

char ClassName;

};

Both Caption and ClassName should be strings (array of chars) not char. Should be something like this:

struct TWindowItem {

HWND Descriptor;

std::string Caption;

std::string ClassName;

};

Second, this is another big mistake:

LPWSTR Buff;

LPWSTR NameOfClass;

GetWindowText(WHandle, Buff, 254);

GetClassName(WHandle, NameOfClass, 254);

LPWSTR is a wchar_t*. What you do with Buff and NameOfClass is to declare to wchar_t pointers, but you never initialize memory for them. So of course, you get garbage and your application crashes.

I would do something like:

BOOL EnumWindowsProc(HWND WHandle, long lParam)

{

TCHAR Buff[255] = {0};

TCHAR NameOfClass[255] = {0};

GetWindowText(WHandle, Buff, 255);

GetClassName(WHandle, NameOfClass, 255);

TWindowItem tempitem;

tempitem.Descriptor = WHandle;

tempitem.Caption = std::string(Buff);

tempitem.ClassName = std::string(NameOfClass);

OoMetaPanel2::WindowList.push_back(tempitem);

return TRUE;

Of course, if you build for UNICODE, you have to use std::wstring instead of std::string.



 
 
Olyx





PostPosted: Visual C++ Language, Please help with EnumWindow function Top

Thank Yoy, Marius, for Your advice. I used this information, but I got

"cannot convert from 'TCHAR [255]' to 'std::string' " error. And other problems with GetWindowText, GetClassName and garbages in array still occurs.



 
 
Marius Bancila





PostPosted: Visual C++ Language, Please help with EnumWindow function Top

Do you build your application for UNICODE Then you have to use std::wstring. Please see my last remark in my previous post.

 
 
Olyx





PostPosted: Visual C++ Language, Please help with EnumWindow function Top

You are right! I used wstring in "tempitem.Caption = std::wstring(Buff);" but forgot to do it in structure definition. But in didn`t solve problems with array and Api functions.



 
 
Marius Bancila





PostPosted: Visual C++ Language, Please help with EnumWindow function Top

What do you mean it didn't Do you still get errors

 
 
Damien Watkins - MSFT





PostPosted: Visual C++ Language, Please help with EnumWindow function Top

Hello

Re: Please help with EnumWindow function

I am going to mark this thread as answered since you have not followed up with any further information on your problem as requested - I assume you solved the problem yourself or one of the suggestions in this thread helped you solved the problem. If you have a solution you could post it so others can find it. If you do not have a solution then please submit further details and then mark the thread as unanswered.

Thanks

Damien