1///Return Type: DWORD->unsigned int
2///hWnd: HWND->HWND__*
3///lpdwProcessId: LPDWORD->DWORD*
4[System.Runtime.InteropServices.DllImportAttribute( "user32.dll", EntryPoint = "GetWindowThreadProcessId" )]
5public static extern int GetWindowThreadProcessId ( [System.Runtime.InteropServices.InAttribute()] System.IntPtr hWnd, out int lpdwProcessId );
6
7
8private int _ExcelPID = 0;
9Process _ExcelProcess;
10
11private Application _ExcelApp = new ApplicationClass();
12GetWindowThreadProcessId( new IntPtr(_ExcelApp.Hwnd), out _ExcelPID );
13_ExcelProcess = System.Diagnostics.Process.GetProcessById( _ExcelPID );
14
15...
16
17_ExcelProcess.Kill();