c# - 如果多个应用程序实例正在运行,如何获取应用程序的活动/前台实例?
问题描述
我正在尝试与用户正在其中工作的 Visual Studio 的应用程序实例进行交互,即前台的应用程序实例。我正在使用GetActiveObject()来获取 VS 的实例。但是,如果有多个 VS 实例正在运行,它总是给出第一个实例(首先打开的那个)。
我尝试使用AccessibleObjectFromWindow()并使用 Spy++ 我将 VS 的 Window 类作为“HwndWrapper”,但“hr”值变得负数。
下面是代码:
if (hwnd != null)
{
EnvDTE80.DTE2 dte = null;
int hwndChild = 0;
EnumChildCallback cb = new EnumChildCallback(EnumVisualStudioChildProc);
EnumChildWindows(hwnd.ToInt32(), cb, ref hwndChild);
if (hwndChild != 0)
{
const uint OBJID_NATIVEOM = 0xFFFFFFF0;
Guid IID_IDispatch = new Guid("{00020400-0000-0000-C000-000000000046}");
int hr = AccessibleObjectFromWindow(hwndChild, OBJID_NATIVEOM, IID_IDispatch.ToByteArray(), out IDispatch ptr);
if (hr >= 0)
{
dte = (EnvDTE80.DTE2)ptr.GetType().InvokeMember("Application", BindingFlags.GetProperty, null, ptr, null);
}
else
{
Console.WriteLine("hr count " + hr + "\n");
}
}
else
{
Console.WriteLine("hwndChild count " + hwndChild + "\n");
dte = (EnvDTE80.DTE2)Marshal.GetActiveObject("VisualStudio.DTE." + VisualStudio.GetInstances());
}
}
public static bool EnumVisualStudioChildProc(int hWnd, ref int lParam)
{
StringBuilder buf = new StringBuilder(128);
GetClassName(hWnd, buf, 128);
if (buf.ToString().Contains("HwndWrapper"))
{
lParam = hWnd;
return false;
}
return true;
}
我尝试了类似的方法来查找 Word(类名:_Wwg)/Excel 的前台实例,它也在工作,我用来检索窗口的类名是否正确?
解决方案
一种解决方案是使用UI Automation。因此,您需要添加对UIAutomationClient
and的引用UIAutomationTypes
,然后使用类似于以下示例的代码:
// get the foreground window handle.
// here I used the Windows GetForegroundWindow function but you can use
// any function that defines what is the active/foreground window in your context
var foreground = GetForegroundWindow();
// get all Visual Studio main windows (from the desktop)
foreach (AutomationElement child in AutomationElement.RootElement.FindAll(
TreeScope.Children, new PropertyCondition(AutomationElement.AutomationIdProperty, "VisualStudioMainWindow")))
{
// note the unfortunate 32-bit that UI automation uses instead of IntPtr...
// in practise that shouldn't be a problem
if (child.Current.NativeWindowHandle == foreground.ToInt32())
{
// this is the foreground Visual Studio
// get its DTE instance
var obj = GetVisualStudioInstance(child.Current.ProcessId);
}
}
// see doc at https://docs.microsoft.com/en-us/previous-versions/ms228755(v=vs.140)
public static object GetVisualStudioInstance(int processId)
{
CreateBindCtx(0, out var ctx);
if (ctx == null)
return null;
ctx.GetRunningObjectTable(out var table);
table.EnumRunning(out var enumerator);
var monikers = new IMoniker[1];
while (enumerator.Next(1, monikers, IntPtr.Zero) == 0)
{
monikers[0].GetDisplayName(ctx, null, out var name);
if (Regex.Match(name, @"!VisualStudio.DTE\.[0-9]*\.[0-9]*:" + processId).Success)
{
table.GetObject(monikers[0], out var obj);
return obj;
}
}
return null;
}
[DllImport("user32")]
private static extern IntPtr GetForegroundWindow();
[DllImport("ole32")]
private static extern int CreateBindCtx(int reserved, out IBindCtx ppbc); // from System.Runtime.InteropServices.ComTypes
推荐阅读
- jquery - 单击按钮时重复的ajax请求
- python - Mindsdb 可以根据以前的行预测新行吗?
- r - 如何使用 R 中的 plot_grid 函数在绘图之间将 y 轴对齐 0?
- android - react native 0.55.2上的Android构建错误
- c++ - 如何使用 ProcMon 检查 Win32 CreateProcess() 失败的原因。排除 GetLastError()
- azure - 限制 Azure 函数只能从一个域调用
- android - 显示过多 Toast 时,Android 应用程序冻结/崩溃
- java - 在其他设备上完美运行时,相机意图未在我的 poco f1 手机中打开相机
- ios - 亚马逊 SNS 中不接受 React Native FCM 令牌作为设备令牌
- javascript - 表单的进度条