🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.nativeapi
27 messages
27 total messages Started by RaynorPao Thu, 03 Apr 2003 10:52
ShellExecute
#17214
Author: RaynorPao
Date: Thu, 03 Apr 2003 10:52
10 lines
1181 bytes
Hi all:

I can use API ShellExecute to execute notepad.

ShellExecute(handle, "open", "test.txt", NULL, NULL, SW_SHOWNORMAL);

But, how can I get the HWND of this notepad?

Thanks.
RaynorPao.
Re: ShellExecute
#17219
Author: Ed Mulroy [TeamB]
Date: Thu, 03 Apr 2003 15:47
52 lines
2103 bytes
You could do FindWindow for   filename - Notepad  and hope.

To get it without assuming anything, use the instance handle returned by
ShellExecute do an EnumWindows and for each HWND received compare the handle
to GetWindowLong(hwnd, GWL_HINSTANCE).


-----------------------
struct HwndInfo
  {
  HINSTANCE hinst;
  HWND hwnd;
  };

BOOL CALLBACK MyEnumProc)
    HWND hwnd, LPARAM lp)
  {
  HwndInfo *hi_ptr = (HwndInfo *) lp;

  if (GetWindowLong(hwnd, GWL_HINSTANCE) ==
     hi_ptr->hinst)
      {
      hi_ptr->hwnd = hwnd;
      return FALSE;
      }

  return TRUE;
  }
-----------------------

and in your code which looks up the window handle:

-----------------------
  HwndInfo hi = { 0 };

  EnumWindows(MyEnumProc, (LPARAM) &hi);
-----------------------

after which hi.hwnd will either be NULL if Notepad did not start or the HWND
of the Notepad window.

.  Ed

> RaynorPao wrote in message
>  news:3e8c0ea4@newsgroups.borland.com...
>
> I can use API ShellExecute to execute notepad.
>
> ShellExecute(handle, "open", "test.txt", NULL,
> NULL, SW_SHOWNORMAL);
>
> But, how can I get the HWND of this notepad?
Re: ShellExecute
#17220
Author: RaynorPao
Date: Thu, 03 Apr 2003 16:30
86 lines
2997 bytes
Hi Ed:

Thanks for your reply.
My code is as below:

HWND ghwnd;
BOOL CALLBACK MyEnumWndProc(HWND hWnd, LPARAM lParam);

BOOL CALLBACK MyEnumWndProc(HWND hWnd, LPARAM lParam)
{
   HINSTANCE hInst=(HINSTANCE)lParam;
   HINSTANCE hGetInst=(HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE);
   if(hInst==hGetInst)
   {
      ghwnd=hWnd;
      return FALSE;
   }
   return TRUE;
}

void __fastcall TForm1::Button3Click(TObject *Sender)
{
   HINSTANCE hinst=ShellExecute(NULL, "open", "test.txt", NULL, NULL,
SW_SHOWNORMAL);
   if(!EnumWindows((WNDENUMPROC)MyEnumWndProc, (LPARAM)hinst))
   {
      // It didn't execute to this block.
      char szCaption[256]={0};
      GetWindowText(ghwnd, szCaption, 256);
      ShowMessage(szCaption);
   }
}

But, it still can't find the window which is created with ShellExecute.
Is there something wrong what I do? Please help me.

Thanks.
RaynorPao.


"Ed Mulroy [TeamB]" <ed@mulroy.org> ���g��l��
news:3e8c5793$1@newsgroups.borland.com...
> You could do FindWindow for   filename - Notepad  and hope.
>
> To get it without assuming anything, use the instance handle returned by
> ShellExecute do an EnumWindows and for each HWND received compare the
handle
> to GetWindowLong(hwnd, GWL_HINSTANCE).
>
>
> -----------------------
> struct HwndInfo
>   {
>   HINSTANCE hinst;
>   HWND hwnd;
>   };
>
> BOOL CALLBACK MyEnumProc)
>     HWND hwnd, LPARAM lp)
>   {
>   HwndInfo *hi_ptr = (HwndInfo *) lp;
>
>   if (GetWindowLong(hwnd, GWL_HINSTANCE) ==
>      hi_ptr->hinst)
>       {
>       hi_ptr->hwnd = hwnd;
>       return FALSE;
>       }
>
>   return TRUE;
>   }
> -----------------------
>
> and in your code which looks up the window handle:
>
> -----------------------
>   HwndInfo hi = { 0 };
>
>   EnumWindows(MyEnumProc, (LPARAM) &hi);
> -----------------------
>
> after which hi.hwnd will either be NULL if Notepad did not start or the
HWND
> of the Notepad window.
>
> .  Ed
Re: ShellExecute
#17326
Author: Remy Lebeau (TeamB)
Date: Thu, 03 Apr 2003 18:08
37 lines
2182 bytes
"RaynorPao" <RaynorPao.nospam@nospam.pchome.com.tw> wrote in message
news:3e8c5dce@newsgroups.borland.com...

>    if(!EnumWindows((WNDENUMPROC)MyEnumWndProc, (LPARAM)hinst))
>    {
>       // It didn't execute to this block.

Nor should it.  You made the wrong assumption.  You assume that
EnumWindows() returns the same return value as the callback function.  It
doesn't.  The callback's return value simply tells EnumWindows() whether to
continue enumerating to the next window or not, whereas the return value of
EnumWindows() tells the calling code whether the enumeration itself was
successfully executed or not.

Try this instead:

    void __fastcall TForm1::Button3Click(TObject *Sender)
    {
        HINSTANCE hinst = ShellExecute(NULL, "open", "test.txt", NULL, NULL,
SW_SHOWNORMAL);
        if( hinst > 32 )
        {
            ghwnd = NULL;
            EnumWindows((WNDENUMPROC)MyEnumWndProc, (LPARAM)hinst);

            if( ghwnd != NULL )
            {
                char szCaption[256]={0};
                GetWindowText(ghwnd, szCaption, 256);
                ShowMessage(szCaption);
            }
        }
    }


Gambit
Re: ShellExecute
#17327
Author: nicolasr
Date: Thu, 03 Apr 2003 18:28
10 lines
1369 bytes
Hi,

I think the docs on ShellExecute are wrong. The return value is not the true
HINSTANCE. If you check the online help on Microsofts website you will see
that they corrected it. So the problem probably is that your comparison of
the instance handles never yields true.
You could use ShellExecuteEx, it returns the process handle. Don't know,
however, if that helps you much....

Nick
Re: ShellExecute
#17324
Author: RaynorPao
Date: Thu, 03 Apr 2003 18:48
68 lines
2902 bytes
Hi Remy:

I modified my code as below:

void __fastcall TForm1::Button3Click(TObject *Sender)
{
   HINSTANCE hinst=ShellExecute(NULL, "open", "test.txt", NULL, NULL,
SW_SHOWNORMAL);
   if((int)hinst>32)
   {
      ghwnd=NULL;
      EnumWindows((WNDENUMPROC)MyEnumWndProc, (LPARAM)hinst);
      if(ghwnd!=NULL)
      {
         char szCaption[256]={0};
         GetWindowText(ghwnd, szCaption, 256);
         ShowMessage(szCaption);
      }
   }
}

But, it still can't find the window which is created with ShellExecute.
Is there something wrong what I do? Please help me.

Thanks.
RaynorPao.

"Remy Lebeau (TeamB)" <gambit47@yahoo.com> ���g��l��
news:3e8c78bd$1@newsgroups.borland.com...
>
> "RaynorPao" <RaynorPao.nospam@nospam.pchome.com.tw> wrote in message
> news:3e8c5dce@newsgroups.borland.com...
>
>
> Nor should it.  You made the wrong assumption.  You assume that
> EnumWindows() returns the same return value as the callback function.  It
> doesn't.  The callback's return value simply tells EnumWindows() whether
to
> continue enumerating to the next window or not, whereas the return value
of
> EnumWindows() tells the calling code whether the enumeration itself was
> successfully executed or not.
>
> Try this instead:
>
>     void __fastcall TForm1::Button3Click(TObject *Sender)
>     {
>         HINSTANCE hinst = ShellExecute(NULL, "open", "test.txt", NULL,
NULL,
> SW_SHOWNORMAL);
>         if( hinst > 32 )
>         {
>             ghwnd = NULL;
>             EnumWindows((WNDENUMPROC)MyEnumWndProc, (LPARAM)hinst);
>
>             if( ghwnd != NULL )
>             {
>                 char szCaption[256]={0};
>                 GetWindowText(ghwnd, szCaption, 256);
>                 ShowMessage(szCaption);
>             }
>         }
>     }
>
>
> Gambit
>
>
Re: ShellExecute
#17328
Author: Remy Lebeau (TeamB)
Date: Thu, 03 Apr 2003 19:38
12 lines
1388 bytes
"nicolasr" <nicolasrNOSPAMATSIGNgmx.net> wrote in message
news:3e8c7d44@newsgroups.borland.com...

> You could use ShellExecuteEx, it returns the process handle.
> Don't know, however, if that helps you much....

Personally, I prefer to use CreateProcess() instead, and then compare the
numeric process IDs instead o the actual handles


Gambit
Re: ShellExecute
#17330
Author: nicolasr
Date: Thu, 03 Apr 2003 20:07
15 lines
1614 bytes
Hi,

> Personally, I prefer to use CreateProcess() instead, and then compare the
> numeric process IDs instead o the actual handles

if he used CreateProcess() he could compare the process IDs in his window
enumeration (using GetWindowThreadProcessId() for the currently enumerated
window). That could be a solution. But then I saw that he is opening a
document. Is this possible with CreateProcess(), assuming you don't know the
associated application?

Since ShellExecuteEx returns the process handle, is there a way to get
process IDs from process handles?

Nick
Re: ShellExecute
#17334
Author: Remy Lebeau (TeamB)
Date: Thu, 03 Apr 2003 20:38
18 lines
1621 bytes
"nicolasr" <nicolasrNOSPAMATSIGNgmx.net> wrote in message
news:3e8c9481$1@newsgroups.borland.com...

> But then I saw that he is opening a document. Is this possible
> with CreateProcess(), assuming you don't know the
> associated application?

You can always look up the application in the Registry first, then pass the
application path to CreateProcess() with the document filename as a
parameter.

> is there a way to get process IDs from process handles?

None that I know of.


Gambit
Re: ShellExecute
#17336
Author: nicolasr
Date: Thu, 03 Apr 2003 22:06
2 lines
1164 bytes
thanks, good to know.
Nick
Re: ShellExecute
#17343
Author: RaynorPao
Date: Fri, 04 Apr 2003 03:46
56 lines
2595 bytes
Hi Remy & nicolasr:

Thanks for your reply.
I modified my code as below:

HWND ghwnd;
BOOL CALLBACK MyEnumWndProc(HWND hWnd, LPARAM lParam);

BOOL CALLBACK MyEnumWndProc(HWND hWnd, LPARAM lParam)
{
   HINSTANCE hInst=(HINSTANCE)lParam;
   HINSTANCE hGetInst=(HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE);
   if(hInst==hGetInst)
   {
      ghwnd=hWnd;
      return FALSE;
   }
   return TRUE;
}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
   SHELLEXECUTEINFO ShlInfo;
   ZeroMemory(&ShlInfo, sizeof(SHELLEXECUTEINFO));
   ShlInfo.cbSize=sizeof(SHELLEXECUTEINFO);
   ShlInfo.fMask=SEE_MASK_NOCLOSEPROCESS;
   ShlInfo.hwnd=this->Handle;
   ShlInfo.lpVerb="open";
   ShlInfo.lpFile="test.txt";
   ShlInfo.lpParameters=NULL;
   ShlInfo.lpDirectory=NULL;
   ShlInfo.nShow=SW_SHOWNORMAL;
   ShellExecuteEx(&ShlInfo);

   if(!EnumWindows((WNDENUMPROC)MyEnumWndProc, (LPARAM)ShlInfo.hInstApp))
   {
      // This time, it can execute to this block, but got a wrong window
      char szCaption[256]={0};
      GetWindowText(ghwnd, szCaption, 256);
      ShowMessage(szCaption);
   }
}

But, it still can't get the HWND which is created with ShellExecuteEx.
Please help me.

Thanks.
RaynorPao.


"nicolasr" <nicolasrNOSPAMATSIGNgmx.net> ���g��l��
news:3e8cb03c@newsgroups.borland.com...
> thanks, good to know.
> Nick
>
>
Re: ShellExecute
#17346
Author: Remy Lebeau (TeamB)
Date: Fri, 04 Apr 2003 07:14
12 lines
1634 bytes
"RaynorPao" <RaynorPao.nospam@nospam.pchome.com.tw> wrote in message
news:3e8cfc38@newsgroups.borland.com...

>       // This time, it can execute to this block, but got a wrong window

That doesn't tell us anything about the problem.  Please elaborate.  What is
the window that it is finding now, and what do you expect it to find
instead?


Gambit
Re: ShellExecute
#17350
Author: nicolasr
Date: Fri, 04 Apr 2003 11:23
99 lines
3896 bytes
Hi RaynorPao,

this is what the uptodate online help at MSDN says about the return value of
ShellExecute:

<quote>
Returns a value greater than 32 if successful, or an error value that is
less than or equal to 32 otherwise. The following table lists the error
values. The return value is cast as an HINSTANCE for backward compatibility
with 16-bit Windows applications. It is not a true HINSTANCE, however. The
only thing that can be done with the returned HINSTANCE is to cast it to an
int and compare it with the value 32 or one of the error codes below.
</quote>

Here is a sketch for an alternative approach that uses CreateProcess(), as
Remy suggested. It uses FindExecutable() to find the associated application
for "test.txt" in the Registry.

hope this helps,
Nick


HWND ghwnd;
BOOL CALLBACK MyEnumWndProc(HWND hWnd, LPARAM lParam);

BOOL CALLBACK MyEnumWndProc(HWND hWnd, LPARAM lParam)
{
   DWORD targetID = lParam;
   DWORD curID = GetWindowThreadProcessId(hWnd, NULL);

   if(curID==targetID)
   {
      ghwnd=hWnd;
      return FALSE;
   }
   return TRUE;
}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
   const char* fileName = "test.txt";

   //adjust as needed
//################
   const char* filePath = "c:\\cppbuilder\\test\\";
   char exeName[MAX_PATH+1];


   int err = reinterpret_cast<int>(FindExecutable(fileName,
                                   filePath, exeName));

   if(err <= 32)
   {
      ShowMessage(SysErrorMessage(err));
      return;
   }

   STARTUPINFO si;
   ZeroMemory(&si, sizeof(STARTUPINFO));
   si.cb = sizeof(STARTUPINFO);

   PROCESS_INFORMATION pi;
   ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));

   err = CreateProcess(NULL,
                       (AnsiString(exeName) + " test.txt").c_str(),
                       NULL,
                       NULL,
                       FALSE,
                       0,
                       NULL,
                       NULL,
                       &si,
                       &pi
                       );

   if(err == 0)
   {
      ShowMessage(SysErrorMessage(GetLastError()));
      return;
   }

   //wait until the application is ready for input, otherwise the
   //window may still not have been created and you don't find it.
   WaitForInputIdle(pi.hProcess, 1000);

   CloseHandle(pi.hProcess);
   CloseHandle(pi.hThread);


   if(!EnumWindows((WNDENUMPROC)MyEnumWndProc, pi.dwThreadId))
   {
      char szCaption[256]={0};
      GetWindowText(ghwnd, szCaption, 256);
      ShowMessage(szCaption);
   }

}
Another Issue about ShellExecute
#17139
Author: Jo�o Pereira
Date: Mon, 07 Apr 2003 14:29
10 lines
1343 bytes
I'm trying to run 3 instances at same time with shellexecute on a 4 monitor
system. Like, opening a web page extended on the first 2 monitors and 2 text
files on the others 2 monitors.
I've tryed to use the shellexecutueex with SHELLEXECUTEINFO struct. But it
always open the documents arbitrary,i can't choose the output monitor.
Does anyone can help me with this.

Thanks
Regards
Jo�o Pereira
Re: Another Issue about ShellExecute
#17153
Author: Remy Lebeau (TeamB)
Date: Mon, 07 Apr 2003 17:43
16 lines
1736 bytes
As has already been told to you earlier, had you read the earlier replies,
you cannot directly specify which monitor to display a spawned application
onto.  You will just have to locate the new application's window and move
the window manually.


Gambit

"Jo�o Pereira" <joao@pupula.com> wrote in message
news:3e918b4d@newsgroups.borland.com...
> I'm trying to run 3 instances at same time with shellexecute on
> a 4 monitor system. Like, opening a web page extended on the
> first 2 monitors and 2 text files on the others 2 monitors.
> I've tryed to use the shellexecutueex with SHELLEXECUTEINFO
> struct. But it always open the documents arbitrary,i can't choose
> the output monitor. Does anyone can help me with this.
Re: Another Issue about ShellExecute
#17154
Author: Jo�o Pereira
Date: Mon, 07 Apr 2003 18:06
28 lines
2016 bytes
And how is it possible?
I've tryed the FindExecutable and i got a HINSTANCE, but i need an Handler
HWND to move the the window.
Can anyone help me with this.

Thx


"Remy Lebeau (TeamB)" <gambit47@yahoo.com> wrote in message
news:3e91b8e8$1@newsgroups.borland.com...
> As has already been told to you earlier, had you read the earlier replies,
> you cannot directly specify which monitor to display a spawned application
> onto.  You will just have to locate the new application's window and move
> the window manually.
>
>
> Gambit
>
> "Jo�o Pereira" <joao@pupula.com> wrote in message
> news:3e918b4d@newsgroups.borland.com...
> > I'm trying to run 3 instances at same time with shellexecute on
> > a 4 monitor system. Like, opening a web page extended on the
> > first 2 monitors and 2 text files on the others 2 monitors.
> > I've tryed to use the shellexecutueex with SHELLEXECUTEINFO
> > struct. But it always open the documents arbitrary,i can't choose
> > the output monitor. Does anyone can help me with this.
>
>
Re: Another Issue about ShellExecute
#17155
Author: Remy Lebeau (TeamB)
Date: Mon, 07 Apr 2003 18:39
13 lines
1456 bytes
That was also explained in the earlier replies.  Did you not read then
earlier at all?

Use CreateProcess() instead, which returns the ProcessID of the new process.
Then use EnumWindows() and GetWindowThreadProcessId() to find windows
belonging to the new process, and then move them as desired.


Gambit

"Jo�o Pereira" <joao@pupula.com> wrote in message
news:3e91be11$1@newsgroups.borland.com...
> And how is it possible?
Re: Another Issue about ShellExecute
#17172
Author: Jo�o Pereira
Date: Tue, 08 Apr 2003 15:43
26 lines
1878 bytes
Sorry about that,
but could you make a example explaining how do you can move and positionate
a window, with CreateProcess(),EnumWindows() and GetWindowThreadProcessId().

And thx again you have been very helpful



"Remy Lebeau (TeamB)" <gambit47@yahoo.com> wrote in message
news:3e91c5c6$1@newsgroups.borland.com...
> That was also explained in the earlier replies.  Did you not read then
> earlier at all?
>
> Use CreateProcess() instead, which returns the ProcessID of the new
process.
> Then use EnumWindows() and GetWindowThreadProcessId() to find windows
> belonging to the new process, and then move them as desired.
>
>
> Gambit
>
> "Jo�o Pereira" <joao@pupula.com> wrote in message
> news:3e91be11$1@newsgroups.borland.com...
> > And how is it possible?
>
>
Re: Another Issue about ShellExecute
#17079
Author: Ed Mulroy [TeamB]
Date: Tue, 08 Apr 2003 17:14
81 lines
3131 bytes
> Jo�o Pereira wrote in message
> news:3e92ee2f@newsgroups.borland.com...
>
> but could you make a example explaining how do you
> can move and positionate a window, with
> CreateProcess(), EnumWindows() and
> GetWindowThreadProcessId().

Exactly the same functionality as someone asked me for the other day.

Create a text file named "TEST.TXT" in the project directory and then
compile and run this.  It's an API-only program so if you build it from
within the IDE then use the console wizard with the GUI checkbox checked and
the VCL one not checked.

-----------------------
#include <windows.h>

HWND child_hwnd;

BOOL CALLBACK MyEnumProc(HWND hwnd, LPARAM lp)
  {
  DWORD hwnd_pid;

  // get process id for the window
  GetWindowThreadProcessId(hwnd, &hwnd_pid);

  // if they match, save the value, end the enumeration
  if (hwnd_pid == (DWORD) lp)
    {
    child_hwnd = hwnd;
    return FALSE;
    }

  return TRUE;
  }

// move the window down and to the left 2 pixels
// then wait 10 millisec
void MoveDelay()
  {
  RECT r;

  GetWindowRect(child_hwnd, &r);
  MoveWindow(child_hwnd, r.left + 2, r.top + 2,
             r.right - r.left, r.bottom - r.top, TRUE);
  Sleep(10);
  }

int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
  {
  STARTUPINFO         starti;
  PROCESS_INFORMATION pi;

  // start notepad with the text file as a cmd line argument
  ZeroMemory(&starti, sizeof(starti));
  starti.cb = sizeof(starti);
  CreateProcess(NULL, "notepad.exe test.txt",
          NULL, NULL, TRUE, NORMAL_PRIORITY_CLASS,
          NULL, NULL, &starti, &pi);

  // wait until notepad has initialized
  WaitForInputIdle(pi.hProcess, 4000U);

  // find the notepad window handle
  EnumWindows(MyEnumProc, (LPARAM) pi.dwProcessId);

  // if found, then move it
  if (child_hwnd)
    {
    for (int i = 0; i < 100; ++i)
      {
      MoveDelay();
      }
    }

  return 0;
  }
-----------------------

.  Ed
Re: Another Issue about ShellExecute
#17080
Author: Jo�o Pereira
Date: Tue, 08 Apr 2003 17:57
104 lines
3896 bytes
Thx again!
But i got this error message:
[C++ Error] Unit1.cpp(64): E2034 Cannot convert 'int (__stdcall *)(void
*,long)' to 'int (__stdcall *)()'
[C++ Error] Unit1.cpp(64): E2342 Type mismatch in parameter 'lpEnumFunc'
(wanted 'int (__stdcall *)()', got 'int (__stdcall *)(void *,long)')
on
EnumWindows(MyEnumProc, (LPARAM) pi.dwProcessId);

could you solve it?
And how i'll put this code in my project instead of a Console Wizard.
Thanx very much for the help






"Ed Mulroy [TeamB]" <ed@mulroyREMOVETHESECHARS.org> wrote in message
news:3e93038d@newsgroups.borland.com...
> > Jo�o Pereira wrote in message
> > news:3e92ee2f@newsgroups.borland.com...
> >
> > but could you make a example explaining how do you
> > can move and positionate a window, with
> > CreateProcess(), EnumWindows() and
> > GetWindowThreadProcessId().
>
> Exactly the same functionality as someone asked me for the other day.
>
> Create a text file named "TEST.TXT" in the project directory and then
> compile and run this.  It's an API-only program so if you build it from
> within the IDE then use the console wizard with the GUI checkbox checked
and
> the VCL one not checked.
>
> -----------------------
> #include <windows.h>
>
> HWND child_hwnd;
>
> BOOL CALLBACK MyEnumProc(HWND hwnd, LPARAM lp)
>   {
>   DWORD hwnd_pid;
>
>   // get process id for the window
>   GetWindowThreadProcessId(hwnd, &hwnd_pid);
>
>   // if they match, save the value, end the enumeration
>   if (hwnd_pid == (DWORD) lp)
>     {
>     child_hwnd = hwnd;
>     return FALSE;
>     }
>
>   return TRUE;
>   }
>
> // move the window down and to the left 2 pixels
> // then wait 10 millisec
> void MoveDelay()
>   {
>   RECT r;
>
>   GetWindowRect(child_hwnd, &r);
>   MoveWindow(child_hwnd, r.left + 2, r.top + 2,
>              r.right - r.left, r.bottom - r.top, TRUE);
>   Sleep(10);
>   }
>
> int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
>   {
>   STARTUPINFO         starti;
>   PROCESS_INFORMATION pi;
>
>   // start notepad with the text file as a cmd line argument
>   ZeroMemory(&starti, sizeof(starti));
>   starti.cb = sizeof(starti);
>   CreateProcess(NULL, "notepad.exe test.txt",
>           NULL, NULL, TRUE, NORMAL_PRIORITY_CLASS,
>           NULL, NULL, &starti, &pi);
>
>   // wait until notepad has initialized
>   WaitForInputIdle(pi.hProcess, 4000U);
>
>   // find the notepad window handle
>   EnumWindows(MyEnumProc, (LPARAM) pi.dwProcessId);
>
>   // if found, then move it
>   if (child_hwnd)
>     {
>     for (int i = 0; i < 100; ++i)
>       {
>       MoveDelay();
>       }
>     }
>
>   return 0;
>   }
> -----------------------
>
> .  Ed
>
>
Re: Another Issue about ShellExecute
#17082
Author: Ed Mulroy [TeamB]
Date: Tue, 08 Apr 2003 18:35
31 lines
2552 bytes
> But i got this error message:
> [C++ Error] Unit1.cpp(64): E2034 Cannot convert
>  'int (__stdcall *)(void *,long)' to
> 'int (__stdcall *)()'
> [C++ Error] Unit1.cpp(64):
> E2342 Type mismatch in parameter 'lpEnumFunc'
> (wanted 'int (__stdcall *)()', got 'int (__stdcall *)(void *,long)')
> on EnumWindows(MyEnumProc, (LPARAM) pi.dwProcessId);

Around 1992 Microsoft added the define STRICT for use with Windows header
files to differentiate between code from their compilers which supported
function prototypes and later ones.  The Windows header files expand
differently if it is defined, mainly by showing the argument list in
callback function arguments.  Try defining STRICT.

> And how i'll put this code in my project instead of a
> Console Wizard.

The code can be put into your project.  It is just not VCL code so you'd
need to either insert the WinMain code into the WinMain the compiler wrote
or rename the function and have the WinMain function call it.  I'd prefer to
have the compiler-written WinMain call so that I'm altering code generated
by the compiler's funky ways of doing things.

I don't understand what the console wizard problem is.  It is the only way
provided to create a console or GUI project written only with C or C++.

.  Ed

> Jo�o Pereira wrote in message
> news:3e930d9c@newsgroups.borland.com...
Re: Another Issue about ShellExecute
#17085
Author: Remy Lebeau (TeamB)
Date: Tue, 08 Apr 2003 19:55
17 lines
1475 bytes
"Jo�o Pereira" <joao@pupula.com> wrote in message
news:3e930d9c@newsgroups.borland.com...

> But i got this error message:
<snip>

Change this line:

    EnumWindows(MyEnumProc, (LPARAM) pi.dwProcessId);

To this instead:

    EnumWindows((WNDENUMPROC) MyEnumProc, (LPARAM) pi.dwProcessId);


Gambit
Re: ShellExecute
#17119
Author: nicolasr
Date: Thu, 10 Apr 2003 01:48
14 lines
1683 bytes
Hi Remy,

> > is there a way to get process IDs from process handles?
>
> None that I know of.

I just tested to launch a program with ShellExecute() and close it
with TerminateProcess() which needs the process handle. I used
FindWindow() to get the handle of the programs window, then
GetWindowThreadProcessId() to obtain the process ID. Then I stumbled
across the OpenProcess() API and this seems to return the process
handle. At least the call to TerminateProcess() worked...

Nick
Re: ShellExecute
#17120
Author: Remy Lebeau (TeamB)
Date: Thu, 10 Apr 2003 03:02
19 lines
1910 bytes
"nicolasr" <nicolasrNOSPAMATSIGNgmx.net> wrote in message
news:3e94cd3c$1@newsgroups.borland.com...

> I just tested to launch a program with ShellExecute() and close it
> with TerminateProcess() which needs the process handle. I used
> FindWindow() to get the handle of the programs window, then
> GetWindowThreadProcessId() to obtain the process ID. Then
> I stumbled across the OpenProcess() API and this seems to
> return the process handle. At least the call to TerminateProcess()
> worked...

Had you used CreateProcess() instead of ShellExecute(), you would have
already had both the process ID and process handle immediately, as
CreateProcess() returns both of them directly, instead of you needing to
"hunt" for them manually.


Gambit
Re: ShellExecute
#17121
Author: nicolasr
Date: Thu, 10 Apr 2003 03:41
6 lines
1424 bytes
Hi,
sorry if I didn't make myself clear. I just tried to check if there's
really no way to generally get the process handle from the ID
whereever it came from, so for example from ShellExecute().
regards,
Nick
Re: ShellExecute
#17122
Author: Remy Lebeau (TeamB)
Date: Thu, 10 Apr 2003 05:07
14 lines
1698 bytes
"nicolasr" <nicolasrNOSPAMATSIGNgmx.net> wrote in message
news:3e94e7ca$1@newsgroups.borland.com...

> I just tried to check if there's really no way to generally
> get the process handle from the ID whereever it came from

Of course there is, and you already found it - OpenProcess().  However, that
is not what you asked originally.  Earlier, you asked the opposite - if it
was possible to get a process ID from a process handle.  It is not, at least
not directly.


Gambit
Re: ShellExecute
#17030
Author: nicolasr
Date: Thu, 10 Apr 2003 14:22
6 lines
1405 bytes
> Of course there is, and you already found it - OpenProcess().
However, that
> is not what you asked originally.

Indeed. Sorry, messed things up.
Nick
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