#include <windows.h>
#include <stdio.h>

static char *sFmt   = "%6d  %8d  %s  (%s)\n" ;
static char *sTitle = "%6s  %8s  %s\n" ;
static int all = 0;

static WINAPI
EnumProc(
    HWND    hWindow,
    void    *pvParam
){
    char    sTitle[256] ;
    char    sClass[256] ;
    unsigned long Pid ;

    strcpy(sClass, "???");
    GetWindowThreadProcessId(hWindow,&Pid) ;
    GetWindowText(hWindow,sTitle,sizeof sTitle-1) ;
    if (!all && sTitle[0] == 0) 
        return(TRUE) ;
    GetClassName(hWindow,sClass,sizeof sClass-1) ;
    if (sTitle[0] == 0)
        strcpy(sTitle, "???");
    if (sClass[0] == 0)
        strcpy(sClass, "???");
    printf(sFmt,hWindow,Pid,sTitle,sClass) ;
    return(TRUE) ;
}

main(
    int  argc,
    char *argv[]
){
    if (argc > 1 && !strcmp(argv[1], "-l"))
        all = 1;
    printf(sTitle," WinID","     PID","Window Name") ;
    EnumWindows(EnumProc,0) ;
    return(0) ;
}
