-2

I want to check on this code and see if you can help me implement it

if so , is there a way to query faster getting only process that starts with "a" and process starts with "b"

(is it by using the filter parameter )

could it be done via other method with parameters required by

EnumDesktopWindows(IntPtr.Zero, filter, IntPtr.Zero)

how can i call EnumDesktopWindows() with another maybe not only using native method - filtering as mentioned ... kind'a "helper" method

 private const string USER32 = "user32.dll";

    internal delegate bool EnumDelegate(IntPtr hWnd, int lParam);

    [DllImport(WindowsFunctions.USER32, EntryPoint = "GetWindowText", ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
    internal static extern int GetWindowText(IntPtr hWnd, StringBuilder lpWindowText, int nMaxCount);

    [DllImport(WindowsFunctions.USER32)]
    [return: MarshalAs(UnmanagedType.Bool)]
    internal static extern bool IsWindowVisible(IntPtr hWnd);

    [DllImport(WindowsFunctions.USER32)]
    internal static extern int GetWindowThreadProcessId(IntPtr hWnd, out int processId);

    [DllImport(WindowsFunctions.USER32)]
    internal static extern bool EnumDesktopWindows(IntPtr hDesktop, EnumDelegate lpfn, IntPtr lParam);







public class WindowData
{
    public string Title { get; set; }
    public long ProcessId { get; set; }
    public long ThreadId { get; set; }
    public IntPtr HWindow { get; set; }
}


        var collection = new List<WindowData>();

        EnumDelegate filter = delegate(IntPtr hWnd, int lParam)
        {
            StringBuilder strbTitle = new StringBuilder(255);
            int nLength = WindowsFunctions.GetWindowText(hWnd, strbTitle, strbTitle.Capacity + 1);
            string strTitle = strbTitle.ToString();

            if (IsWindowVisible(hWnd) && !string.IsNullOrEmpty(strTitle))
            {
                int pid;
                int threadId = WindowsFunctions.GetWindowThreadProcessId(hWnd, out pid);

                collection.Add(new WindowData()
                {
                    Title = strbTitle.ToString(),
                    ProcessId = pid,
                    ThreadId = threadId,
                    HWindow = hWnd
                });
            }

            return true;
        };



        if (EnumDesktopWindows(IntPtr.Zero, filter, IntPtr.Zero))
        {
            return collection.ToArray();
        }

        return null;
    }
LoneXcoder
  • 2,121
  • 6
  • 38
  • 76
  • 2
    Please ask a short and direct question. – David Heffernan Sep 01 '12 at 21:07
  • There's really no question here. Is your question really "how do I execute the final snippet of code?" If so remove **all** the rest of the question and ask just that. – David Heffernan Sep 01 '12 at 21:14
  • not entairly , i was asking about an elegant way to query for that "lost" hWnd how to fetch it in the most slick method Would u with your experience use unsafe code to get more performance out of the task , what approach would you choose , regarding that last one i posted thinking making use of pinvoke will do the job as i mentioned, with elegancy while performance optimized if this code(last) will do i woud then like to see how but the main isue was what would you choose to do how ? – LoneXcoder Sep 01 '12 at 21:20
  • I still can't understand this and still cannot see a real question. Sorry. – David Heffernan Sep 01 '12 at 23:15
  • @DavidHeffernan restripping it down to one as short as i can make Question – LoneXcoder Sep 01 '12 at 23:26
  • Why is "user32.dll" a constant. It won't change on widows. If it is about so the string only appears once in the executable, the compiler will do that for you. – Cole Tobin Sep 02 '12 at 00:04
  • @ColeJohnson i have stripped it down from a set of 3 classes trying to make it into one class i am not sure it supposed to be as it is now though , really if your comment had a some-weight of a question , i didnt make this code i am too far from it . but still i am looking for an implementation on it so if you could please do reply with correct answer regarding querying system diagnostics proccess via filtering on the Process.GetProccess or somthing similar that will fit-in the code there's in my post. thank you cole – LoneXcoder Sep 02 '12 at 00:33

1 Answers1

0

this is what i came up with as temporary solution

public static class MyProc
    {
        public static IntPtr WoWhWnd;
        public static string WoWProcName;
        public static string WowMainWinTitle;
        public static int WowID;
        public static IntPtr MyApphWnd;
        public static string MyAppProcName;
        public static string MyAppMainWinTitle;
        public static int MyAppID;
    }


    public void bring(string handletoFront)
    {
        switch (handletoFront)
        {
            default:

                SetForegroundWindow(MyProc.WoWhWnd);

                break;
            case "MyApp":

                SetForegroundWindow(MyProc.MyApphWnd);
                                    break;
        }


    }


           #region <<=========== Enumerating Processes   ============>>

        void  ShowP1(){

            IEnumerable<Process> processes = from p in Process.GetProcesses() where p.ProcessName.StartsWith(TBXSearchWindowTerm.Text) || p.ProcessName.StartsWith("WindowsF") orderby p.ProcessName select p;
            MyProc.MyApphWnd = processes.ElementAt(1).MainWindowHandle;
            MyProc.MyAppMainWinTitle = processes.ElementAt(1).MainWindowTitle;
            MyProc.MyAppID = processes.ElementAt(1).Id;
            MyProc.WoWhWnd = processes.ElementAt(0).MainWindowHandle;
            MyProc.WowMainWinTitle = processes.ElementAt(0).MainWindowTitle;
            MyProc.WowID = processes.ElementAt(0).Id;
            pause(350);
            SetForegroundWindow(MyProc.WoWhWnd);
            pause(850);
            SetForegroundWindow(MyProc.MyApphWnd);
        }

in one of the queryies i have used a text box value so it could be one constant search and a dynamic optional one , if you see somthing that i did wrong or i could do better , please do comment . thank you .

LoneXcoder
  • 2,121
  • 6
  • 38
  • 76