visual c++ GetWindow member function

Hi, my question is regarding the visual c++ 'GetWindow' member function. An application I support has the ability to limit concurrent user sessions. It does this by counting the number of existing sessions on the desktop whenever a user attempts to open another session. It does this via a dll file which contains a function called uWindowCount. uWindowCount uses the c++ GetWindow member function and GW_HWNDFIRST to count any existing sessions and return the number back to the calling application (code below).

My problem is that when the application is run on a windows xp desktop; existing sessions that are minimized are not being counted, so as long as a user minimizes existing sessions before opening a new session they can open as many as they like. On a terminal server this does not occur, all sessions (minimized or not) are counted and the application is able to control a users concurrent sessions successfully.


#include "csutil.h"

#define MAX_PROCESS 100 // precautionary over-run count.
#define MAX_NAME 100 // buffer space to allocate for window names.

//XEXPORT(long) uWindowCount(void)
XEXPORT(long) UWINDOWCOUNT(void)
{
HWND hWndUniface, hWnd, hWndParent = 0;
int iLenName, iCount=0;
int iFound;
char szTitleWindow[MAX_NAME+2];
char szTitle[MAX_NAME+1];


iFound = 0;
szTitle[0] = 0;

UGETREGS(REG0,szTitle,sizeof(szTitle)-1);

// exit with error code if empty window title is detected.

if(strlen(szTitle) == 0)
{
UPUTMESS("uWindowCount: empty window title supplied.");
UPUTREG(REG1,-1L);
return SUCCESS;
}

/* Start with handle of Uniface Window */
hWndUniface = GetActiveWindow();

/* Get first window handle */
hWnd = GetWindow(hWndUniface, GW_HWNDFIRST);

/* loop through all windows */
while (hWnd)
{
/* control loop */
if (iCount++ > MAX_PROCESS)
break;

/* Get title of this window */
iLenName = GetWindowTextLength(hWnd);

if (iLenName > MAX_NAME)
iLenName = MAX_NAME ;

iLenName = GetWindowText(hWnd, szTitleWindow, iLenName+1);

/* See if window is the one we want */
if (strncmp(szTitleWindow, szTitle, strlen(szTitle)) == 0)
++iFound;

/* Get next window */
hWnd = GetWindow(hWnd, GW_HWNDNEXT);
}

UPUTREG(REG1,iFound);

return SUCCESS;
}
I know windows has an BOOL IsIconic(HWND hWnd) function to check whether a windows is minimized or not. Maybe that might be of some use.
Is the DLL loaded in the address space of the executable that you count windows of? If so, wouldn't it be better to count sessions via a more stable method? Maybe you can increase a counter whenever the DLL is loaded.

Or if you are bound to count windows, why not use a CBT hook that alerts you reliably whenever a window is created and destroyed? You can use Spy++ or the new one (forgot the name) that comes with the Windows 7 SDK to determine the window class name.
Topic archived. No new replies allowed.