NO

Author Topic: How to find a window class name from a caption  (Read 5960 times)

Rainbow Sally

  • Guest
How to find a window class name from a caption
« on: December 30, 2004, 01:46:18 PM »
Do get the sdk docs from the url posted by Vortex (probably still named win32hlp.zip).  There are others around but if it doesn't unpack to around 24 megs it's not the good one.

You can link it into your ide so everything is right there when you want it.

How to find a class name from a caption
~~~~~~~~~~~~~~~~~~~~~~~~~~

This started out to be an experiment to find the class name for a console.  All  consoles have a class name of "tty".  And here's how we know.

Code: [Select]


// find-console.c

#include <windows.h> // WinMain etc.
#include <stdio.h> // sprintf

// compiler: __stdcall
// macros:   -subsystem:windows

// change this to find other dos boxes after you test
// this one.  Select RUN in the Start menu and look
// use "MS-DOS Prompt" for the WindowTitle.  What is
// it's class name?

char * WindowTitle = "wait4key";
//char * WindowTitle = "MS-DOS Prompt";

//int main(void)
int WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR cmdLine, int cmdShow)
{
HANDLE hWnd;
char *tmpStr1=malloc(260);
char *tmpStr2=malloc(260);
    hWnd = FindWindow( NULL, WindowTitle);
GetClassName(
    hWnd, // handle of window
    tmpStr1, // address of buffer for class name
    260 // size of buffer, in characters
   );
if(tmpStr1[0] == 0) sprintf(tmpStr1, "%s", "NO CONSOLE FOUND");
sprintf(tmpStr2, "The window with the caption '%s'\nhas the class name '%s'", WindowTitle, tmpStr1);
MessageBox(NULL, tmpStr2, "GetClassName", MB_OK);
    free(tmpStr1);
    free(tmpStr2);
return 0;
}




If you don't like to download files from strangers, here is how to make the test file (a batch file) and the project file.  If you have trouble changing the file extensions or even seeing them, with your computer you know why some of us upgraded to win 98.

Code: [Select]


// copy the following text to new files
// with the names as shown.  The bat file
// must be running before you run
// find-console.exe file or it won't find
// the window.  (Don't add either of them
// to your project).

/* wait4key.bat -----------------------

@echo off
echo.
echo Press any key to end...
echo.
pause > nul
cls

wait4key.PPJ -----------------------

#
# PROJECT FILE generated by "Pelles C for Windows, version 2.73".
# NOTE! Manual changes of this file is done at your own risk.
#

POC_PROJECT_VERSION = 1.00#
POC_PROJECT_TYPE = 3#
POC_PROJECT_PATH = .#
POC_PROJECT_ARGUMENTS = #
POC_PROJECT_WORKPATH = #
POC_PROJECT_EXECUTOR = #
CC = pocc.exe#
RC = porc.exe#
LINK = polink.exe#
CCFLAGS = -W1 -Ot -Gz -Ze -Tx86-coff#
RCFLAGS = #
LINKFLAGS = -release /section:.rdata,rwe -machine:ix86 -subsystem:windows kernel32.lib user32.lib advapi32.lib delayimp.lib msvcrt.lib#

.SILENT:  
 

#
# Build find-console.EXE.
#
"$(POC_PROJECT_PATH)\find-console.EXE": \
  "$(POC_PROJECT_PATH)\output\find-console.OBJ"  
  $(LINK) $(LINKFLAGS) -out:"$@" $**

#
# Build find-console.OBJ.
#
"$(POC_PROJECT_PATH)\output\find-console.OBJ": \
  "$(POC_PROJECT_PATH)\find-console.c"  
  $(CC) $(CCFLAGS) "$!" -Fo"$@"

-------------------------------------- */



The attached zip includes source and project files for this simple experiment and also another one to enumerate all open consoles which could be adapted to other purposes.  (It's a zip inside the zip)