Click here to Skip to main content
15,905,874 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi My Frinds
I'm Sorry That I Do Not Have Good Written Language.
I Want TO Get Address MY Active Window in c#
for example:
The Window My Computer is active: -> C:\Program Files (x86)\Adobe\Photoshop CS
Now I Want a Program To Give This Address Window in C# OR Refer To This Window.
Please Help Me.
Thank You.

What I have tried:

Please help me with language c# only
If you have a project II will be grateful submit.
thank you.
Posted
Updated 26-Jun-16 3:29am
Comments
Afzaal Ahmad Zeeshan 25-Jun-16 18:31pm    
Those are called handles in Windows. But why you want that handle is pretty much unclear. Did you try to get the list of processes running and get one from there? Besides, where do you want to pass this. Besides, I only remember of the methods to open the directory in Windows Explorer, I don't recall how to get the active directory.
Sergey Alexandrovich Kryukov 26-Jun-16 4:34am    
Probably the inquirer means the path name of the mail executable module of the process presently showing active window.
—SA
Sergey Alexandrovich Kryukov 26-Jun-16 4:39am    
May I ask you: why? What would be the purpose of it?
—SA
Member 12599699 26-Jun-16 7:33am    
yes.
I plan to write code to address, for example:on active path copy a folder on this
Sergey Alexandrovich Kryukov 26-Jun-16 9:22am    
My question was: why? why do you need it? and why only a path to the module showing active windows? what do you want to do with it ans why? Being an active window has little to do with its functionality, especially for a foreign process.
—SA

You can get all processes and try to get a main window handle of each one using System.Diagnostics.Process.MainWindowHandle:
Process.GetProcesses Method (System.Diagnostics),
Process.MainWindowHandle Property (System.Diagnostics).

Pay attention: MainWindowHandle may throw and exception; so you have to catch it and just ignore this case.

For each handle, you can GetForegroundWindow the way you wrote in Solution 3 (but this is not a solution).

—SA
 
Share this answer
 
C#
unsafe
{
  var notepadProcs = Process.GetProcessesByName("notepad");
  if (notepadProcs.Length > 0)
  {
    // Main window handle in IntPtr
    var mainWindowHandle = notepadProcs[0].MainWindowHandle;

    // Main window handle in unmanaged pointer
    var mainWindowPtr = (byte*) mainWindowHandle.ToPointer();
  }
}
 
Share this answer
 
C#
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();

...
IntPtr activeWindow = GetForegroundWindow();
byte *mainWindowPtr = (byte*) activeWindow.ToPointer();
...
 
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