Getting excel process id

A

Asim

Hi,
I am working on excel automation. I want the get the handle and process
id for the excel process. Following is the way I am creating the excel
application.

Excel.Application xlApp = new Excel.Application();

There is one way of getting the handle (hwnd) to the excel process by making
the application visible, then setting a caption and getting the handle using
FindWindow method. But I don't want to make the application visible.
So is there any other way of doing it?
I am using Excel 2000, C# 2.0.

Regards,
Asim.
 
C

Chip Pearson

The Application object has an HWnd property, but I don't recall if it is
present in Excel 2000. You can use the FindWindow API call to find the HWnd.
In VB, this would be

Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long

Function GetXLHwnd() As Long
GetXLHwnd = FindWindow("XLMain", Application.Caption)
End Function

If you can be sure that you have only one instance of Excel running, you can
substitute vbNullString for Application.Caption. The Application need not be
visible for this to work.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel, 10 Years
Pearson Software Consulting
www.cpearson.com
(email on the web site)
 
A

Asim

Hi,
Thanks a lot. It works now.

Regards,
Asim.

Chip Pearson said:
The Application object has an HWnd property, but I don't recall if it is
present in Excel 2000. You can use the FindWindow API call to find the HWnd.
In VB, this would be

Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long

Function GetXLHwnd() As Long
GetXLHwnd = FindWindow("XLMain", Application.Caption)
End Function

If you can be sure that you have only one instance of Excel running, you can
substitute vbNullString for Application.Caption. The Application need not be
visible for this to work.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel, 10 Years
Pearson Software Consulting
www.cpearson.com
(email on the web site)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top