AJA NTV2 SDK  17.1.1.1245
NTV2 SDK 17.1.1.1245
processimpl.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: MIT */
9 #include "ajabase/common/timer.h"
10 
11 
12 // class AJAProcessImpl
13 
15 {
16 }
17 
18 
20 {
21 }
22 
23 uint64_t
25 {
26  return GetCurrentProcessId();
27 }
28 
29 bool
30 AJAProcessImpl::IsValid(uint64_t pid)
31 {
32  HANDLE pH = OpenProcess(SYNCHRONIZE, false, (DWORD)pid);
33  if(INVALID_HANDLE_VALUE != pH && NULL != pH)
34  {
35  DWORD retValue = WaitForSingleObject(pH, 0);
36  CloseHandle(pH);
37  return retValue == WAIT_TIMEOUT;
38  }
39  return false;
40 }
41 
42 static HWND hFoundWindow = NULL;
43 
44 BOOL CALLBACK findFunc(HWND hWnd, LPARAM lParam)
45 {
46  bool bDone = false;
47  TCHAR* pTail = (TCHAR*)lParam;
48  TCHAR Title[500];
49  GetWindowText(hWnd, Title, NUMELMS(Title));
50 
51  if(strlen(Title) < strlen(pTail))
52  return TRUE;
53  TCHAR* pTitle = Title + strlen(Title) - strlen(pTail);
54  if(pTitle < Title)
55  return TRUE; // Don't do the compare, because it would overflow
56  int32_t len = min((int32_t)strlen(pTitle), (int32_t)strlen(pTail));
57  if(!_strnicmp(pTitle, pTail, len))
58  {
59  hFoundWindow = hWnd;
60  return FALSE; // Found it, we're done
61  }
62  return TRUE; // Keep going
63 }
64 
65 
66 bool
67 AJAProcessImpl::Activate(const char* pWindow)
68 {
69  // handle "ends in" by looking for * at beginning of string
70  const char* pName = pWindow;
71  bool bEnum = false;
72  if(*pName == '*')
73  {
74  pName++;
75  bEnum = true;
76  }
77  HWND hWindow = NULL;
78  bool bFound = false;
79  if(!bEnum)
80  {
81  hWindow = FindWindow(NULL, pWindow);
82  bFound = hWindow ? true : false;
83  }
84  else
85  {
87  bFound = !EnumWindows(findFunc, (LPARAM)pName);
88  hWindow = hFoundWindow;
89  }
90  if(!bFound)
91  return false;
92 
93  return Activate((uint64_t)hWindow);
94 }
95 
96 bool
97 AJAProcessImpl::Activate(uint64_t handle)
98 {
99  HWND hWnd = (HWND)handle;
100  if(!hWnd)
101  return false;
102  if(!IsWindow(hWnd))
103  return false;
104 
105  DWORD pid = 0;
106  DWORD oldThread = GetWindowThreadProcessId(hWnd, &pid);
107  if(!oldThread)
108  return false;
109 
110  DWORD currentThread = GetCurrentThreadId();
111 
112  WINDOWPLACEMENT wp;
113  wp.length = sizeof(WINDOWPLACEMENT);
114  GetWindowPlacement(hWnd, &wp);
115 
116  if(SW_MINIMIZE == wp.showCmd || SW_SHOWMINIMIZED == wp.showCmd)
117  ShowWindow(hWnd, SW_RESTORE);
118  else
119  {
120  AttachThreadInput(oldThread, currentThread, TRUE);
121  SetActiveWindow(hWnd);
122  SetForegroundWindow(hWnd);
123  BringWindowToTop(hWnd);
124  SetFocus(hWnd);
125  AttachThreadInput(oldThread, currentThread, FALSE);
126  }
127  RECT r;
128  GetClientRect(hWnd, &r);
129  InvalidateRect(hWnd, &r, true);
130  UpdateWindow(hWnd);
131  return true;
132 }
HANDLE
short HANDLE
Definition: ajatypes.h:315
processimpl.h
Declares the AJAProcessImpl class.
AJAProcessImpl::GetPid
static uint64_t GetPid()
Definition: processimpl.cpp:28
NULL
#define NULL
Definition: ntv2caption608types.h:19
NUMELMS
#define NUMELMS(aa)
Definition: types.h:430
findFunc
BOOL CALLBACK findFunc(HWND hWnd, LPARAM lParam)
Definition: processimpl.cpp:44
AJAProcessImpl::Activate
static bool Activate(uint64_t handle)
Definition: processimpl.cpp:43
timer.h
Declares the AJATimer class.
hFoundWindow
static HWND hFoundWindow
Definition: processimpl.cpp:42
AJAProcessImpl::IsValid
static bool IsValid(uint64_t pid)
Definition: processimpl.cpp:34
AJAProcessImpl::~AJAProcessImpl
virtual ~AJAProcessImpl()
Definition: processimpl.cpp:23
true
#define true
Definition: ntv2devicefeatures.h:26
INVALID_HANDLE_VALUE
#define INVALID_HANDLE_VALUE
Definition: ajatypes.h:329
AJAProcessImpl::AJAProcessImpl
AJAProcessImpl()
Definition: processimpl.cpp:18