processes of Teams question

Deng, Jianyu 21 Reputation points
2021-11-05T21:18:30.59+00:00

I want to track MST meeting by c#, Are there any difference(like process properties)on MST and MST meeting in the process ?My main idea is using Process class. The example I follow is https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process?view=net-5.0
I hope there is a process property that Teams has and Teams meeting doesn.t has. So I can identify the process I track is Teams or Teams meeting
Or Are there any ways, c# can identify if user is in the Teams meeting or user just have a chat in MST

Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.


Answer accepted by question author
Castorix31 91,976 Reputation points
2021-11-06T09:00:58.86+00:00

You can use UIAutomation to identify the main Team window

Test with version 1.3.00.28779 and Automation ID = "teams-app-bar" (check the ID with MS Inspect tool if not found)

// Add reference to : c:\Windows\system32\UIAutomationCore.dll
// Add : using UIAutomationClient
// Add : using System.Runtime.InteropServices;

Global

IUIAutomation pUIAutomation = new CUIAutomation();

Main code

IntPtr hWndMain = IntPtr.Zero;
int nPID = 0;
int nThreadID = 0;
Process[] procs = Process.GetProcessesByName("teams");
if (procs.Count() > 0)            
{
    foreach (Process proc in procs)
    {
        if ((proc.MainWindowHandle != IntPtr.Zero))
        {
            hWndMain = proc.MainWindowHandle;                        
            nPID = proc.Id;
            nThreadID = Process.GetProcessById(nPID).Threads[0].Id;
            break;
        }
    }
}
if (nThreadID != 0)
{ 
    EnumCallBack Callback = new EnumCallBack(ListWindows);
    EnumThreadWindows(nThreadID, Callback, IntPtr.Zero);
}

Callback

public bool ListWindows(IntPtr hWnd, ref IntPtr lParam)
{
    IUIAutomationElement teamAppbar = null;
    if (IsWindowVisible(hWnd))
    { 
        IUIAutomationElement windowElement = pUIAutomation.ElementFromHandle(hWnd);
        if (windowElement != null)
        {
            IUIAutomationElementArray elementArray = null;
            IUIAutomationCondition condition = null;
            condition = pUIAutomation.CreateTrueCondition();
            elementArray = windowElement.FindAll(TreeScope.TreeScope_Descendants, condition);                   
            if (elementArray != null)
            {
                int nNbItems = elementArray.Length; 
                for (int nItem = 0; nItem < nNbItems; nItem++)
                {
                    IUIAutomationElement element = elementArray.GetElement(nItem);
                    string sName = element.CurrentName;
                    string sAutomationId = element.CurrentAutomationId;
                    //Console.WriteLine("Name : {0} - AutomationId : {1}", sName, sAutomationId);
                    if (sAutomationId == "teams-app-bar")
                    {
                        teamAppbar = element;
                        break;
                    }
                }
            }
        }
        StringBuilder sbText = new StringBuilder(260);
        GetWindowText(hWnd, sbText, sbText.Capacity);
        string sWindow = null;
        if (teamAppbar != null)
            sWindow = "Main window";
        else
            sWindow = "Secondary window";
        Debug.WriteLine(string.Format("{0} - Window : {1}", sWindow, sbText.ToString()));
    }
    return true;
}

Declarations :

[DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern bool EnumThreadWindows(int dwThreadId, EnumCallBack lpfn, IntPtr lParam);

public delegate bool EnumCallBack(IntPtr handle, ref IntPtr lParam);

[DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

[DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool IsWindowVisible(IntPtr hWnd);

Was this answer helpful?

0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.