🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

is the window handle always the window handle?

#10843
From: Mortz
Date: Wed, 10 Mar 2004 17:26
125 lines
5270 bytes
Hi everyone...

After a painful but educative proccess and thanks to a lot of help from this
news group, I could finally get the HWND from a spawned app (using create
process [thanks Gambit])

Now I'm trying to post messages or issue commands to those apps, however it
seems like the window handle referes to different objects in the window
trought time, how can I make sure to post the message to the App window
directly?

For some apps the code below works when they are minimized for others it
works when apps are not active and for others it opens a DDE server window.

I know I'm missing something but can anyone please give me a hint?

Here is the code (a bit extense)...

//this is part of a thread object wich monitors when the spawned app is
closed
void __fastcall ProcessWatcher::Execute()
{
     //A TTreeNode is passed to the constructor of the thread, it contains
information about the spawned app
     NodeInfo *dataPtr;
     dataPtr=(NodeInfo *)node->Data;
     while(!Terminated)
     {
          LookForWindowHandle(dataPtr); //this funciton is here in order to
keep the handle updated (is the only way it worked :S)
          if(WaitForSingleObject(dataPtr->HProcess(),10)!=WAIT_TIMEOUT)
               break;
     }
}
//--------------------------------------------------------------------------
-

HWND ProcessWatcher::LookForWindowHandle(NodeInfo *node)
{
     if (!EnumWindows((WNDENUMPROC)GetWinHandle,(LPARAM)node))
          return node->WindowHandle();
     else
          return 0;
}
//--------------------------------------------------------------------------
-
BOOL CALLBACK GetWinHandle(HWND hwnd,LPARAM lParam)
{
     NodeInfo* pInfo=(NodeInfo *)lParam;
     DWORD dwProcessId=0;
     GetWindowThreadProcessId(hwnd,&dwProcessId);
     if(dwProcessId==pInfo->ProcessID())
          pInfo->WindowHandle(hwnd);
     return (pInfo->WindowHandle()==NULL);
}

//=======MAIN FORM=======
void __fastcall TMainForm::TreeView1Click(TObject *Sender)
{
//...
 if (HT.Contains(htOnItem))
          TreeView1->Selected=TreeView1->GetNodeAt(tvX,tvY);
 else
          return;
///...
dataPtr=(NodeInfo*)TreeView1->Selected->Data; //when a node is created in
the TreeView its associated with a "NodeInfo" Structure

if(dataPtr->Active()) //if the document opened
          {
               if(IsIconic(dataPtr->WindowHandle())) //if the document is
minimized
                    PostMessage(dataPtr->WindowHandle(), WM_SYSCOMMAND,
SC_RESTORE, 0);
               else
                         ShowWindow(dataPtr->WindowHandle(),SW_SHOW);
          }
          else //if document is not active
          {
               OpenDocument(TreeView1->Selected);
          }
//...
}
//------------------------------------------------------------
void TMainForm::OpenDocument(TTreeNode *node)
{
     STARTUPINFO  StartupInfo;
     PROCESS_INFORMATION ProcessInfo;
     char szExecutable[MAX_PATH];
     int res;
     NodeInfo *dataPtr;
     ProcessWatcher *spawnedProcessWatcher;
     AnsiString tempStr;

     memset(&StartupInfo,0,sizeof(STARTUPINFO));
     StartupInfo.cb = sizeof(STARTUPINFO);
     szExecutable[0]=NULL;
     dataPtr=(NodeInfo *)node->Data;
     tempStr=ExtractFileDir(dataPtr->SystemString());
     tempStr="\""+tempStr+"\"";
     res=
(int)FindExecutable(dataPtr->SystemString().c_str(),tempStr.c_str(),szExecut
able);
     tempStr=AnsiString(szExecutable)+" \""+dataPtr->SystemString()+"\"";
     if(res>32)
     {

if(CreateProcess(NULL,tempStr.c_str(),NULL,NULL,TRUE,NORMAL_PRIORITY_CLASS,N
ULL,NULL,&StartupInfo,&ProcessInfo))
               {
               dataPtr->HProcess(ProcessInfo.hProcess);
               dataPtr->ProcessID(ProcessInfo.dwProcessId);
               TreeView1->Selected->Text=TreeView1->Selected->Text+"
<Opened>";
               dataPtr->Active(true);
               spawnedProcessWatcher=new
ProcessWatcher(true,TreeView1->Selected);
               spawnedProcessWatcher->OnTerminate=ProcessTerminated;
               SpawnedProcessList->Add(spawnedProcessWatcher);
               spawnedProcessWatcher->Resume();
               //if I look for the windows handle here the app may not be
completeley initialized and so the handle can be missed
               }
     }
}
Technical Details
Message-ID: <404f4eb5$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10cc67,6dc76ae9466e5f5,start
X-Google-Attributes: gid10cc67,public
X-Google-ArrivalTime: 2004-03-10 09:40:35 PST
Reply-To: "Mortz" <rmorteo@hotmail.com>
From: "Mortz" <rmorteo@hotmail.com>
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: is the window handle always the window handle?
Date: Wed, 10 Mar 2004 09:26:01 -0800
Lines: 127
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
NNTP-Posting-Host: 200.38.16.98
Message-ID: <404f4eb5$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1078939317 200.38.16.98 (10 Mar 2004 09:21:57 -0800)
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail