【问题标题】:How to get PID (Process ID) of Word instance or Hwnd property?如何获取 Word 实例或 Hwnd 属性的 PID(进程 ID)?
【发布时间】:2017-05-22 02:17:50
【问题描述】:

我正在寻找一种更好的编码方式,以便在我的应用程序中正确关闭 WINWORD.EXE 或 word 进程,方法是获取其 PID(进程 ID),就像在 Excel 中获取 PID 一样。

这是我使用 Hwnd 属性和 GetProcessID 类获取 Excel PID 的示例。

Imports System.Diagnostics

Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Integer, ByRef lpdwProcessId As IntPtr) As IntPtr

Public Sub CreateNewFromTemplate_Excel()
        ' Create Excel Application, Workbook, and WorkSheets
        Dim xlExcel As Excel.Application = New Excel.Application

        'Getting the process ID of excel application
        Dim processId As IntPtr = Nothing
        GetWindowThreadProcessId(xlExcel.Hwnd, processId)
        Dim excelProcess As Process = Process.GetProcessById(processId.ToInt32())

       Dim blError As Boolean = False

        Try
                [Perform Excel generation here.]
                ' Make sure all objects are disposed
                xlBook.Close()
                xlExcel.Quit()
        Catch ex As Exception
              blError = True
              If Not excelProcess.HasExited Then excelProcess.Kill() ' End the specific Excel Process ID if not yet closed.
              Throw
       Finally
              If Not blError Then
                     If Not excelProcess.HasExited Then excelProcess.Kill() ' End the specific Excel Process ID if not yet closed.
              End If
       End Try
End Sub

这是我用于关闭 Word Process/WINWORD.EXE 的代码,但不幸的是,它在服务器端不起作用,因此我正在寻找一种方法和代码来获取 Word Process ID 以正确关闭它。

Marshal.ReleaseComObject(rng)

            Dim strFileName As String = comm.GenerateFilename(strUser_Id, strVPN) & ".docx"

            ' Save the document.
            Dim filename As Object = Path.GetFullPath(strNewFilePath & strFileName)
            newDoc.SaveAs(FileName:=filename)

            ' Close.
            Dim save_changes As Object = False
            newDoc.Close(save_changes)
            WordApp.Quit(save_changes)

            Marshal.ReleaseComObject(newDoc)
            Marshal.ReleaseComObject(WordApp)

            rng = Nothing
            newDoc = Nothing
            WordApp = Nothing

            ' Let GC know about it 
            GC.Collect()
            GC.WaitForPendingFinalizers()
            GC.Collect()
            GC.WaitForPendingFinalizers()

            ' save the file to database
            SaveFormat(filename, strUser_Id, strFamilyId.Trim, strVPN.Trim, DateTime.Now.ToString(), "application/vnd.ms-word", br_id, "doc")

            If File.Exists(filename) Then
                File.Delete(filename)
            End If

任何人都可以建议关于如何获取 Word 实例的 PID 或 Hwnd 属性的最佳编码方式或最佳实践。谢谢:)

【问题讨论】:

  • 在此处查看this,它是一本好书,您会找到答案的。
  • 我居然遇到了这个帖子。我不太了解编写的代码,但我会再试一次,我希望它会起作用。顺便谢谢:)

标签: asp.net vb.net ms-word interop hwnd


【解决方案1】:

您尝试做的事情可能不会像预期的那样可靠。如果您需要处理来自服务器的文档和电子表格,我强烈建议您切换到 OpenXML 或其他一些库一边。

我倾向于使用 EPPlus,它工作得非常好,并且与 VBA/Interop 的代码几乎相同。 http://epplus.codeplex.com/

在继续之前,我会阅读以下来自 Microsoft 的警告和文章:

所有当前版本的 Microsoft Office 都经过设计、测试和配置,可作为最终用户产品在客户端工作站上运行。他们假设一个交互式桌面和用户配置文件。它们不提供满足设计为无人值守运行的服务器端组件需求所需的重入性或安全性级别。

Microsoft 目前不推荐也不支持从任何无人值守的非交互式客户端应用程序或组件(包括 ASP、ASP.NET、DCOM 和 NT 服务)自动化 Microsoft Office 应用程序,因为 Office在此环境中运行 Office 时可能会出现不稳定的行为和/或死锁。

https://support.microsoft.com/en-us/help/257757/considerations-for-server-side-automation-of-office

【讨论】:

  • What you are trying to do is not going to work.,这是一个 ludacris 声明。我可以证明这个陈述是错误的,你能提供什么案例来验证你的陈述?请通知我们?
  • @Zaggler 微软官方链接不够吗?这个过程不可靠,显然不是最佳实践。如果 OP 真的想在 ASP.NET 上实现 Interop,那是他的特权,但显然根据 Microsoft 的说法,出于各种原因,他应该避开。我确信有很多关于 Stack Overflow 的文章。
  • 我理解并同意best practice,但您还没有回答我的问题。您能否提供一个明确的测试用例说明为什么这不起作用?到目前为止,我还没有看到一个。
  • @Zaggler 我编辑了我的答案,因为该过程可能有效,但它会可靠地工作吗?由于可变性,这里没有人会回答这个问题。我没有明确的测试用例,但是在 SO 上使用无人值守的 Interop 存在许多问题。做研究。对我来说,这和“不工作”一样好。 OP 应该完全清楚,因为有更好的方法可以在服务器上生成电子表格。
猜你喜欢
  • 2012-05-27
  • 1970-01-01
  • 1970-01-01
  • 2012-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-01
  • 2011-09-07
相关资源
最近更新 更多