本人用asp.net做网页,遇到了调用excel不能关闭的问题。因为vb.net水平较低还比知道怎样调用api关闭线程,只找到以下c#代码,只要把excel的application对象传递到kill方法就行了。求高手能把代码翻译一下,以便我在项目中调用,谢谢。using System.Runtime.InteropServices;
public class PublicMethod
{
public PublicMethod()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
public static void Kill(Excel.Application excel)
{
IntPtr t = new IntPtr(excel.Hwnd); //得到这个句柄,具体作用是得到这块内存入口 int k = 0;
GetWindowThreadProcessId(t, out k); //得到本进程唯一标志k
System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k); //得到对进程k的引用
p.Kill(); //关闭进程k
}}
public class PublicMethod
{
public PublicMethod()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
public static void Kill(Excel.Application excel)
{
IntPtr t = new IntPtr(excel.Hwnd); //得到这个句柄,具体作用是得到这块内存入口 int k = 0;
GetWindowThreadProcessId(t, out k); //得到本进程唯一标志k
System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k); //得到对进程k的引用
p.Kill(); //关闭进程k
}}
Private Shared Function GetWindowThreadProcessId(ByVal hwnd As IntPtr, _
ByRef lpdwProcessId As Integer) As Integer
End Function
If objAcc Is Nothing Then
objAcc = New Application
Dim lngPid As Integer
Dim lngAccessHwnd As IntPtr = New IntPtr(objAcc.hWndAccessApp)
GetWindowThreadProcessId(lngAccessHwnd, lngPid)
End If
其它和C#应该一样来自http://www.pinvoke.net/default.aspx/user32/getwindowthreadprocessid.html