🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #2583

Re: How do I get HWND?

#2583
From: poojo hackma
Date: Fri, 16 Feb 2007 21:02
45 lines
2150 bytes
A search pulled up the code below, but that seems like a lot of work just to 
get the HWND.

Isn't there something basic?

Code:
// -----------------------------------------
HWND  g_hwnd;
int  g_nFound;

BOOL CALLBACK FindHwndFromPID( HWND hwnd, LPARAM lParam) :

HWND GetHwndFromPID ( DWORD dwProcessId)
{
 g_hwnd = NULL;
 g_nFound = 0;
 EnumWindows (  FindHwndFromPID,    ( LPARAM) dwProcessId));
 if ( g_hwnd)  // we found one...
      return ( g_hwnd);
 // nothing found :-(
 return (NULL);
}

BOOL CALLBACK FindHwndFromPID( HWND hwnd, LPARAM lParam)
{
    DWORD   dwPID2Find  =   ( DWORD) lParam;
    DWORD   dwPID       =   0;
    if  (   GetWindowThreadProcessId    (   hwnd,   &dwPID))
        {
            if  (   dwPID   ==  dwPID2Find)
                {
                    g_hwnd  =   hwnd;
                    return  (   FALSE);
                }
        }
    return  (   TRUE);
}

And call 'GetHwndFromPID()' like

Usage:
// -----------------------------------------
HWND hwnd = GetHwndFromPID(GetCurrentProcessId());

which will retrieve the top-level window handle for your app. 
Technical Details
Message-ID: <45d61bc8$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <45d61341$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,b586f03571cff47c
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "poojo hackma" <poojo.com/mail>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <45d61341$1@newsgroups.borland.com>
Subject: Re: How do I get HWND?
Date: Fri, 16 Feb 2007 15:02:20 -0600
Lines: 47
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.3028
x-mimeole: Produced By Microsoft MimeOLE V6.00.2900.3028
X-RFC2646: Format=Flowed; Response
X-Antivirus: avast! (VPS 000714-2, 02/16/2007), Outbound message
X-Antivirus-Status: Clean
NNTP-Posting-Host: 12.191.89.68
Message-ID: <45d61bc8$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1171659720 12.191.89.68 (16 Feb 2007 13:02:00 -0800)
Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail