Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

The below code returns unique Id for the Excel.

C#
[DllImportAttribute("User32.dll")]
        private static extern int FindWindow(String ClassName, String WindowName);
int hWnd = FindWindow(null,"Micosoft Excel");



But i have opened one excel file name "Book1.xls" the below code return 0.

XML
<pre lang="cs">[DllImportAttribute(&quot;User32.dll&quot;)]
        private static extern int FindWindow(String ClassName, String WindowName);
int hWnd = FindWindow(null,""Book1"");</pre>



can you please help me to get unique id of my opened file.

Thanks,
Posted
Comments
Sergey Alexandrovich Kryukov 5-Apr-13 13:34pm    
It may depend on what .NET UI library do you use; please tag it. Also, "all windows" — in what scope? In one process/application, in the whole system session at this moment, what?
—SA

1 solution

If you really want to get all windows in the Windows session, you can first retrieve the HWMD of desktop and than get all its children. Please see:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms633504%28v=vs.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/ms633515%28v=vs.85%29.aspx[^].

However, I'm not sure you need exactly that. Instead, you can get all the executing processes and then try to get a main window handle of each (which it may or may not have, and this attempt can throw an exception, so be careful). Here is how:
http://msdn.microsoft.com/en-us/library/1f3ys1f9.aspx[^],
http://msdn.microsoft.com/en-us/library/x8b2hzk8.aspx[^],
http://msdn.microsoft.com/en-us/library/z3w4xdc9.aspx[^],
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.mainwindowhandle.aspx[^] (here, pay attention for the exceptions which can be thrown).

And finally, I'm not sure the whole idea is very useful. You cannot get 100% guarantee that a process or a window you find is exactly the one you really need. Both process and window title can accidentally be the same for some unrelated applications. Besides, you access to a separate process is quite limited, because the processes are well isolated. Chances are, you would need to use Office automation, or something else.

—SA
 
Share this answer
 
v2

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