GetWindowThreadProcessId return 0
Hi !
I want to activate Visio, if there is number of instances of Visio is
running.
I am using following code to do this, but, sometimes it does not work
correctly , i mean Visio does not get activated.
//****Code*****//
void ActivateAndShowWnd(CWnd *pWnd)
{
DWORD tMe,tTop;
if(pWnd!= NULL)
{
tMe =GetWindowThreadProcessId(pWnd->m_hWnd, NULL); // sometimes
return zero
tTop=GetWindowThreadProcessId(GetForegroundWindow(),NULL);
if (tMe!=tTop)
{
AttachThreadInput(tTop,tMe,TRUE);
pWnd->SetForegroundWindow();
pWnd->SetFocus();
AttachThreadInput(tTop,tMe,FALSE);
}
else
{
pWnd->SetForegroundWindow();
pWnd->SetFocus();
}
}
}
BOOL CALLBACK EnumWindowCallBack(HWND hwnd, LPARAM lParam)
{
DWORD ProcID2;
CString Title;
GetWindowThreadProcessId ( hwnd, &ProcID2 );
char szBuf[256];
::GetClassName(hwnd,szBuf,sizeof(szBuf));
CString strWndClassName = szBuf;
strWndClassName.TrimLeft();
strWndClassName.TrimRight();
CWnd::FromHandle(hwnd)->GetWindowText(Title);
if(Title.GetLength() != 0 && strWndClassName.CompareNoCase("VISIOA")
== 0 )
{
WINDOWINFO WinInfo = {0};
WinInfo.cbSize = sizeof(WINDOWINFO);
BOOL ret = GetWindowInfo(hwnd, &WinInfo);
int left = WinInfo.rcWindow.left;
int top = WinInfo.rcWindow.top;
int right = WinInfo.rcWindow.right;
int bottom= WinInfo.rcWindow.bottom;
int h = bottom - top;
int w = right - left;
if(IsIconic(hwnd))
ret1 = ShowWindow(hwnd, SW_RESTORE);
else
ret1 = ShowWindow(hwnd, SW_SHOWNORMAL);
SetWindowPos(hwnd, HWND_TOP, left, top, w, h , SWP_SHOWWINDOW);
::SetForegroundWindow(hwnd);
ActivateAndShowWnd(CWnd::FromHandle(hwnd));
return false;
}
else
{
// Keep enumerating
return true;
}
return TRUE;
}
//****Code*****//
In function ActivateAndShowWnd(), tMe =GetWindowThreadProcessId(pWnd-
m_hWnd, NULL); // sometimes return zero
I get value of tMe as 0, what could be wrong here?
Any help will be appreciated.
Regards,
Vicky