🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: From Process to Window Handle

#4181
From: Remy Lebeau (TeamB)
Date: Mon, 13 Mar 2006 21:58
100 lines
3612 bytes
"KL Chin" <klchin@1asia.com.my> wrote in message
news:4414f5b4$1@newsgroups.borland.com...

> Which function should I used to find a window with a Handle
> Procees

There isn't any one specific function for that.  You will have to find the
window handle manually, using EnumWindows() and GetWindowProcessThreadId().

> with execture by ShellExecuteEx

I would suggest you use CreateProcess() instead of ShellExecuteEx().
ShellExecute() does not return the ID of the spawned process.
CreateProcess() does.  You need that ID so that you have something to
compare with GetWindowThreadProcessId().

> so that I can postion the window.

    struct sWndInfo
    {
        HWND hWnd;
        DWORD dwProcessId;
    };

    BOOL CALLBACK FindMainWindow(HWND hwnd, LPARAM lParam)
    {
        sWndInfo *pInfo = (sWndInfo*) lParam;

        DWORD dwProcessId;
        GetWindowThreadProcessId(hwnd, &dwProcessId);

        if( dwProcessId == pInfo->dwProcessId )
        {
            if( hwnd matches some criteria )
            {
                pInfo->hWnd = hwnd;
                return FALSE;
            }
        }


        return TRUE;
    }

    {
        AnsiString CmdLine = AnsiQuotedStr(sexe, '"') + " "  + sprm;

        STARTUPINFO si = {0};
        PROCESS_INFORMATION pi = {0};

        si.cb = sizeof(si);
        si.dwFlags = STARTF_USESHOWWINDOW;
        si.wShowWindow = SW_SHOWNORMAL;

        if( CreateProcess(NULL, CmdLine.c_str(), NULL, NULL, FALSE, 0, NULL,
spth.c_str(), &si, &pi);
        {
            WaitForInputIdle(pi.hProcess, INFINITE);
            CloseHandle(pi.hThread);
            CloseHandle(pi.hProcess);

            sWndInfo Info = {0};
            Info.dwProcessId = pi.dwProcessId;
            EnumWindows((WNDENUMPROC) FindMainWindow, (LPARAM) &Info);

            if( Info.hWnd )
                // use Info.hWnd as needed...
        }
    }


Even better, the lpStartupInfo parameter of CreateProcess() allows you to
pass in the desired position and size directly:

    {
        AnsiString CmdLine = AnsiQuotedStr(sexe, '"') + " "  + sprm;

        STARTUPINFO si = {0};
        PROCESS_INFORMATION pi = {0};

        si.cb = sizeof(si);
        si.dwFlags = STARTF_USESHOWWINDOW;
        si.dwX = ...;
        si.dwY = ...;
        si.dwXSize = ...;
        si.dwYSize = ...;
        si.wShowWindow = SW_SHOWNORMAL | STARTF_USEPOSITION |
STARTF_USESIZE;

        if( CreateProcess(NULL, CmdLine.c_str(), NULL, NULL, FALSE, 0, NULL,
spth.c_str(), &si, &pi);
        {
            CloseHandle(pi.hThread);
            CloseHandle(pi.hProcess);
        }
    }


Gambit
Technical Details
Message-ID: <4415eab2@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.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: <4414f5b4$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,e1b5a692dc7fc69d
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <4414f5b4$1@newsgroups.borland.com>
Subject: Re: From Process  to Window Handle
Date: Mon, 13 Mar 2006 13:58:20 -0800
Lines: 102
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1409
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409
NNTP-Posting-Host: 68.69.17.41
Message-ID: <4415eab2@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1142287026 68.69.17.41 (13 Mar 2006 13:57:06 -0700)
Path: arc1-ca.pugleaf.net!g2news1.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