🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: From Process to Window Handle

#4085
From: KL Chin
Date: Tue, 14 Mar 2006 05:45
119 lines
4148 bytes
Hi Gambit,

Thx for you suggestion & code.

I had no problem to do it with CreateProcess.
But I do have problem with ShellExecEx, bcoz I need to open
any kind document file (doc,txt,bmp) instead of exe file.

Regards
KL Chin


"Remy Lebeau (TeamB)" <no.spam@no.spam.com> wrote in message 
news:4415eab2@newsgroups.borland.com...
>
> "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: <44165869@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news3.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> <4415eab2@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: "KL Chin" <klchin@1asia.com.my>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <4414f5b4$1@newsgroups.borland.com> <4415eab2@newsgroups.borland.com>
Subject: Re: From Process  to Window Handle
Date: Tue, 14 Mar 2006 13:45:08 +0800
Lines: 121
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.2670
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670
X-RFC2646: Format=Flowed; Original
NNTP-Posting-Host: 60.49.242.118
Message-ID: <44165869@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1142315113 60.49.242.118 (13 Mar 2006 21:45:13 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news3.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