views:

126

answers:

1

I want to look in the list of processes to determine if OpenOffice Calc is running or if OpenOffice Writer is running. With QuickStart off, I get a scalc.exe and an swriter.exe so its simple.

However when the quick start is on I just get soffice.bin and soffice.exe

Is there a way of asking those processes which application is running?

+1  A: 

I think there is no way around checking the window title. You would have to enumerate all windows and check whether the title ends with "- OpenOffice.org Writer" or "- OpenOffice.org Calc" etc.

The following short sample would check whether Writer is running:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;

namespace Sample
{
    public class Window
    {
        public string Title { get; set; }
        public int Handle { get; set; }
        public string ProcessName { get; set; }
    }

    public class WindowHelper
    {
        /// <summary>
        /// Win32 API Imports
        /// </summary>
        [DllImport("user32.dll")]
        private static extern int GetWindowText(IntPtr hWnd, StringBuilder title, int size);
        [DllImport("user32.dll")]
        private static extern int EnumWindows(EnumWindowsProc ewp, int lParam);
        [DllImport("user32.dll")]
        private static extern bool IsWindowVisible(IntPtr hWnd);
        [DllImport("user32.dll", SetLastError = true)]
        static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);

        public delegate bool EnumWindowsProc(IntPtr hWnd, int lParam);

        private List<Window> _openOfficeWindows;

        public List<Window> GetOpenOfficeWindows()
        {
            _openOfficeWindows = new List<Window>();
            EnumWindowsProc ewp = new EnumWindowsProc(EvalWindow);
            EnumWindows(ewp, 0);

            return _openOfficeWindows;
        }

        private bool EvalWindow(IntPtr hWnd, int lParam)
        {
            if (!IsWindowVisible(hWnd))
                return (true);

            uint lpdwProcessId;
            StringBuilder title = new StringBuilder(256);

            GetWindowThreadProcessId(hWnd, out lpdwProcessId);
            GetWindowText(hWnd, title, 256);

            Process p = Process.GetProcessById((int)lpdwProcessId);
            if (p != null && p.ProcessName.ToLower().Contains("soffice"))
            {
                _openOfficeWindows.Add(new Window() { Title = title.ToString(), Handle = hWnd.ToInt32(), ProcessName = p.ProcessName });
            }

            return (true);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            WindowHelper helper = new WindowHelper();

            List<Window> openOfficeWindows = helper.GetOpenOfficeWindows();

            foreach (var item in openOfficeWindows)
            {
                if (item.Title.EndsWith("- OpenOffice.org Writer"))
                {
                    Console.WriteLine("Writer is running");
                }
            }
        }
    }
}
0xA3