🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.nativeapi
11 messages
11 total messages Started by KL Chin Mon, 13 Mar 2006 01:59
From Process to Window Handle
#4171
Author: KL Chin
Date: Mon, 13 Mar 2006 01:59
24 lines
1665 bytes
Hi,

Which function should I used to find a window with a Handle Procees with
execture by ShellExecuteEx, so that I can postion the window.

   memset( &asei,0,sizeof(SHELLEXECUTEINFO) );
   asei.cbSize       = sizeof(SHELLEXECUTEINFO);
   asei.hwnd         = GetDesktopWindow( );
   asei.lpVerb       = "open";
   asei.lpFile       = sexe.c_str( );
   asei.lpParameters = sprm.c_str( );
   asei.lpDirectory  = spth.c_str( );
   asei.nShow        = SW_SHOWNORMAL;
   asei.hInstApp     = HInstance;

   brst = ShellExecute Ex( &asei );

  asei.hProcess   <-- from this hProcess

I had try using both GetWindowThreadProcessId and GetWindowThreadProcessId
during EnumWindwo, but not hope.

Regards
KL Chin 
Re: From Process to Window Handle
#4173
Author: Bob Gonder
Date: Mon, 13 Mar 2006 08:22
13 lines
1259 bytes
KL Chin wrote:

>   brst = ShellExecute Ex( &asei );
>
>  asei.hProcess   <-- from this hProcess

Read the description for that field in the help.
<edited>
This member is always NULL if fMask is *not* set to
SEE_MASK_NOCLOSEPROCESS.
</edited>

You didn't set the fMask at all.
Re: From Process to Window Handle
#4174
Author: KL Chin
Date: Mon, 13 Mar 2006 09:22
34 lines
1723 bytes
Hi Bob Gonder,

Thx for your hint, my actual app. do included the some masks.

asei.fMask = SEE_MASK_NO_CONSOLE | SEE_MASK_NOCLOSEPROCESS | 
SEE_MASK_FLAG_NO_UI;

My question, do I used the right function?

I used EnumProcesses - not hope also, I do not see any hProcess (value) in 
the replied list.

Regards,
KL Chin



"Bob Gonder" <notbg@notmindspring.invalid> wrote in message 
news:vmaa129qds4u796doibfh1d1b301qej33j@4ax.com...
> KL Chin wrote:
>
>>   brst = ShellExecute Ex( &asei );
>>
>>  asei.hProcess   <-- from this hProcess
>
> Read the description for that field in the help.
> <edited>
> This member is always NULL if fMask is *not* set to
> SEE_MASK_NOCLOSEPROCESS.
> </edited>
>
> You didn't set the fMask at all.
>
> 
Re: From Process to Window Handle
#4181
Author: 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
Re: From Process to Window Handle
#4182
Author: Remy Lebeau (TeamB)
Date: Mon, 13 Mar 2006 21:59
12 lines
1320 bytes
"KL Chin" <klchin@1asia.com.my> wrote in message
news:441539ce@newsgroups.borland.com...

> Thx for your hint, my actual app. do included the some masks.

Even if you do set the mask, ShellExecuteEx() only gives you a process
Handle, not a process ID.  There are no functions available to automatically
get the ID from a given handle.


Gambit
Re: From Process to Window Handle
#4085
Author: 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
>
> 
Re: From Process to Window Handle
#4084
Author: KL Chin
Date: Tue, 14 Mar 2006 05:50
45 lines
1971 bytes
Hi Gambit,

I understand there is not directly funtion to obtain the HWND,
but how?

What function should be used?

Do I used

Psapi.dll's funciton

  EnumProcessModules?

or

Kernell's function

  CreateToolhelp32Snapshot,
  Process32First & Process32Next
  Module32First & Module32Next

Or

Should it search the register for it right "exe" ?

Regards
KL Chin

"Remy Lebeau (TeamB)" <no.spam@no.spam.com> wrote in message 
news:4415eb13$1@newsgroups.borland.com...
>
> "KL Chin" <klchin@1asia.com.my> wrote in message
> news:441539ce@newsgroups.borland.com...
>
>> Thx for your hint, my actual app. do included the some masks.
>
> Even if you do set the mask, ShellExecuteEx() only gives you a process
> Handle, not a process ID.  There are no functions available to 
> automatically
> get the ID from a given handle.
>
>
> Gambit
>
> 
Re: From Process to Window Handle
#4086
Author: KL Chin
Date: Tue, 14 Mar 2006 06:37
141 lines
4751 bytes
Hi Gambit,

I'm not sure, is these any bugs?
Somehow, the STARTUPINFO is useless in CreateProcess to launch the 
NotePad.exe.

Before that I did the samething like your method, and I try again with your 
code, same
thing happended, the window did not place to the correct place, instead in 
start at the
previous location, any ideal what could the problem,

Regards,
KL Chin



"KL Chin" <klchin@1asia.com.my> wrote in message 
news:44165869@newsgroups.borland.com...
> 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
>>
>>
>
> 
Re: From Process to Window Handle
#4088
Author: Remy Lebeau (TeamB)
Date: Tue, 14 Mar 2006 07:02
10 lines
1237 bytes
"KL Chin" <klchin@1asia.com.my> wrote in message
news:441659bd$1@newsgroups.borland.com...

> What function should be used?

I already told you earlier what to use.


Gambit
Re: From Process to Window Handle
#4089
Author: Remy Lebeau (TeamB)
Date: Tue, 14 Mar 2006 07:03
14 lines
1373 bytes
"KL Chin" <klchin@1asia.com.my> wrote in message
news:44165869@newsgroups.borland.com...

> 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.

Use FindExecutable() to determine the .exe file for a document.  You can
then pass that filename to CreateProcess().


Gambit
Re: From Process to Window Handle
#4090
Author: KL Chin
Date: Tue, 14 Mar 2006 09:35
25 lines
1618 bytes
Hi Gambit,

Thx for your help.

Regards
KL Chin

"Remy Lebeau (TeamB)" <no.spam@no.spam.com> wrote in message 
news:44166a83$1@newsgroups.borland.com...
>
> "KL Chin" <klchin@1asia.com.my> wrote in message
> news:44165869@newsgroups.borland.com...
>
>> 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.
>
> Use FindExecutable() to determine the .exe file for a document.  You can
> then pass that filename to CreateProcess().
>
>
> Gambit
>
> 
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads