Board index » delphi » API Call to FindWindow (can't do it)

API Call to FindWindow (can't do it)

Hi.

I am using Delphi on a Windows 95 Machine.  My goal in a
program is to see if another specific program is running and if
it is, stop it.  Basically, the program is PCAnyWhere 32, and I
need to get at the COM Port to dial a number.  If PCAnywhere is
loaded, then I can't use the modem.  I tried to use the
function 'FindWindow' and have failed over and over.  The
syntax for the FindWindow command is:

function FindWindow(Classname, WindowName: PChar): HWnd;

When pcAnywhere 32 is running, the caption says:
pcANYWHERE Waiting...

I did some test on an msdos prompt and the code segment
Hand:=FindWindow('tty','MS-DOS Prompt')
works ok.  Don't ask where I got the class name 'tty' from,
someone just told me to try it.  The same person told me, the
WindowName in the FindWindow function is what is in the
'caption' of the window you are trying to locate.

Can someone please tell me what parameter to pass to the
FindWindow command to give me the handle of the window.  This
is driving me nutzo.

Thanks a lot,
to...@guard.com
(Tony Thornton in real life)

 

Re:API Call to FindWindow (can't do it)


Quote
Tony Thornton <to...@headquarters.guard.com> wrote:
>Hi.

>I am using Delphi on a Windows 95 Machine.  My goal in a
>program is to see if another specific program is running and if
>it is, stop it.  Basically, the program is PCAnyWhere 32, and I
>need to get at the COM Port to dial a number.  If PCAnywhere is
>loaded, then I can't use the modem.  I tried to use the
>function 'FindWindow' and have failed over and over.  The
>syntax for the FindWindow command is:

>function FindWindow(Classname, WindowName: PChar): HWnd;

>When pcAnywhere 32 is running, the caption says:
>pcANYWHERE Waiting...

>I did some test on an msdos prompt and the code segment
>Hand:=FindWindow('tty','MS-DOS Prompt')
>works ok.  Don't ask where I got the class name 'tty' from,
>someone just told me to try it.  The same person told me, the
>WindowName in the FindWindow function is what is in the
>'caption' of the window you are trying to locate.

>Can someone please tell me what parameter to pass to the
>FindWindow command to give me the handle of the window.  This
>is driving me nutzo.

        I'm not sure from this what you've tried and what happened
when you tried it... Maybe the clue is that if you know the window
title but not the classname you can pass nil for the classname (similarly
you can use nil for the windowname if you don't know that.) Have you
tried FindWindow(nil,'pcANYWHERE Waiting...') ??? (And are you certain
you have the spacing, etc exactly right?)
--
David Ullrich
Don't you guys find it tedious typing the same thing
after your signature each time you post something?
I know I do, but when in Rome...

Re:API Call to FindWindow (can't do it)


In article <4d33cc$...@guava.epix.net>, Tony Thornton
<to...@headquarters.guard.com> writes
Quote
>Hi.

>I am using Delphi on a Windows 95 Machine.  My goal in a
>program is to see if another specific program is running and if
>it is, stop it.  Basically, the program is PCAnyWhere 32, and I
>need to get at the COM Port to dial a number.  If PCAnywhere is
>loaded, then I can't use the modem.  I tried to use the
>function 'FindWindow' and have failed over and over.  The
>syntax for the FindWindow command is:

>function FindWindow(Classname, WindowName: PChar): HWnd;

>When pcAnywhere 32 is running, the caption says:
>pcANYWHERE Waiting...

>I did some test on an msdos prompt and the code segment
>Hand:=FindWindow('tty','MS-DOS Prompt')
>works ok.  Don't ask where I got the class name 'tty' from,
>someone just told me to try it.  The same person told me, the
>WindowName in the FindWindow function is what is in the
>'caption' of the window you are trying to locate.

>Can someone please tell me what parameter to pass to the
>FindWindow command to give me the handle of the window.  This
>is driving me nutzo.

>Thanks a lot,
>to...@guard.com
>(Tony Thornton in real life)

Try the following bit of code.  MatchName is a callback function.  Call
FindWin with the title bar text you're looking for and it will return
with the window handle, or zero if it wasn't found.

function MatchName(h: hWnd; wh: PhWnd): Boolean; export;
begin
  GetWindowText(h,chbuf,127);
  if StrLIComp(winname,chbuf,winlength) <> 0 then
    result := true
  else
  begin
    wh^ := h;
    result := false;
  end;
end;

function FindWin(const name: string): Integer;
var
        handle: hWnd;
begin
        StrPCopy(winname,name);
        winlength := Length(name);
        handle := 0;
        EnumWindows(@MatchName,longint(@handle));
        FindWin := handle;
end;

--
Julian Moss                                      J M Technical Services
{*word*19}ermouth, Cumbria, UK                 Technical Writing and Software
(+44) 1900 826514                              jm...@jmtech.demon.co.uk

Re:API Call to FindWindow (can't do it)


to...@headquarters.guard.com (Tony Thornton)  wrote

Quote
> works ok.  Don't ask where I got the class name 'tty' from,
> someone just told me to try it.  The same person told me, the

You definitely will find the correct class name by using 'WINSIGHT.EXE'  
which ships with Delphi. Believe me, it works. :-)

Michael
--

Michael Suedkamp                                      m...@suedkm.franken.de

Re:API Call to FindWindow (can't do it)


Your problem may be that the function is supported differently in Win95.
From the Windows SDK:

FindWindow (2.x)

HWND FindWindow(lpszClassName, lpszWindow)

LPCSTR lpszClassName;   /* address of class-name string */
LPCSTR lpszWindow;      /* address of window-name string        */

The FindWindow function retrieves the handle of the window whose class
name and window name match the
specified strings. This function does not search child windows.

Parameter       Description

lpszClassName   Points to a null-terminated string that contains the
window's class name. If this parameter is
NULL, all class names match.
lpszWindow      Points to a null-terminated string that specifies the
window name (the window's title). If this
parameter is NULL, all window names match.

Returns

The return value is the handle of the window that has the specified class
name and window name if the function is
successful. Otherwise, it is NULL.

Example

The following example searches for the main window of Windows Control
Panel (CONTROL.EXE) and, if it does
not find it, starts Control Panel:

if (FindWindow("CtlPanelClass", "Control Panel") == NULL)
    WinExec("control.exe", SW_SHOWNA);

See Also

EnumWindows, GetWindow, WindowFromPoint

--
Alexander Medwedew
Computer Ventures, Inc.
compv...@tribeca.ios.com
http://tribeca.ios.com/~compvent/
CADVANCE LITE - Affordable CAD Software
http://tribeca.ios.com/~compvent/cadvlite.html

Other Threads