Click here to Skip to main content
15,903,012 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to get full path of window which is on top.
My application is in background and when user press specific key then i want to know the path of current window which is On top.

I try following code but this code is showing my own application path.

What I have tried:

C#
protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            if (m.Msg == 0x0312)
            {
                Process currentProcess = Process.GetCurrentProcess();
                MessageBox.Show(currentProcess.MainModule.FileName);
            }
        }
Posted
Updated 5-May-16 2:16am
v2
Comments
Sergey Alexandrovich Kryukov 4-May-16 10:11am    
There is no such thing as "path of window".
—SA

Well...yes, it will.
There are two problems here. The first is that in order for you application to be activated by a key to find out which window is on top, it has to have the input focus. which means...it's on top...:laugh:

Secondly, if you look at the documentation for Process.GetCurrentProcess Method (System.Diagnostics)[^] it returns a process associated withteh currently executing code: i.e. the code that is trying to find out which process is tip most - this will always be your application!

Pretty much, you can't do what you want - though the link will show you how to get all processes on your system - unless you can install a Global Hook which can trap out the key at a level much, much higher than your application. While this can be done (Processing Global Mouse and Keyboard Hooks in C#[^] shows how) it's not something for a beginner to attempt - mistakes can leave your system seriously unstable!
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900