我已经为我记录了这个附加到进程(nunit.exe)的宏。现在,我想在我要附加的进程窗口上按一个按钮(名为"Run"),但是怎么做呢?我记录了这个宏:
Sub DebugNUnit()
DTE.ExecuteCommand("Tools.ExternalCommand6")
'need a short delay for nunit process to appear in process list:
Threading.Thread.Sleep(200)
Try
Dim dbg2 As EnvDTE80.Debugger2 = DTE.Debugger
Dim trans As EnvDTE80.Transport = dbg2.Transports.Item("Default")
Dim dbgeng(1) As EnvDTE80.Engine
dbgeng(0) = trans.Engines.Item("Managed (v4.0)")
Dim proc2 As EnvDTE80.Process2 = dbg2.GetProcesses(trans, "myPC").Item("nunit.exe")
proc2.Attach2(dbgeng)
Catch ex As System.Exception
MsgBox(ex.Message)
End Try
End Sub更新:--我已经根据下面的答案调整了脚本
Sub DebugNUnit()
DTE.ExecuteCommand("Tools.ExternalCommand6")
'need a short delay for nunit process to appear in process list:
Threading.Thread.Sleep(200)
Try
Dim dbg2 As EnvDTE80.Debugger2 = DTE.Debugger
Dim trans As EnvDTE80.Transport = dbg2.Transports.Item("Default")
Dim dbgeng(1) As EnvDTE80.Engine
dbgeng(0) = trans.Engines.Item("Managed (v4.0)")
Dim proc2 As EnvDTE80.Process2 = dbg2.GetProcesses(trans, "myPC").Item("nunit.exe")
proc2.Attach2(dbgeng)
'added this because we may need a short delay for nunit gui to load
'(would prefer to replace it with a wait on some loaded event or something)
Threading.Thread.Sleep(5000)
'use Process.Handle or Process.MainWindowHandle? anyway, both do not seem to work.. are the parameters incorrect?
Dim ButtonHandle As IntPtr = FindWindowEx(System.Diagnostics.Process.GetProcessById(proc2.ProcessID).Handle, 0, "Button", "Run")
'Dim ButtonHandle As IntPtr = FindWindowEx(System.Diagnostics.Process.GetProcessById(proc2.ProcessID).MainWindowHandle, 0, "Button", "Run")
SendMessage(ButtonHandle, &HF5, 0, 0)
Catch ex As System.Exception
MsgBox(ex.Message)
End Try
End Sub但是(由Tools.ExternalCommand6)启动的nunit用户界面上的"Run“按钮仍未按下。这是有意义的,因为按钮句柄似乎总是0..为什么?有谁能找到解决办法吗?
更新:--我已经根据编辑的答案对脚本进行了如下调整
Option Strict Off
Option Explicit Off
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a
Imports EnvDTE100
Imports System.Diagnostics
Imports System.Runtime.InteropServices
Imports System.Windows.Forms
Public Module RecordingModule
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Function PostMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Boolean
End Function
Sub DebugNUnit()
DTE.ExecuteCommand("Tools.ExternalCommand6")
'need a short delay for nunit processes to appear in process list:
Threading.Thread.Sleep(1000)
Try
Dim dbg2 As EnvDTE80.Debugger2 = DTE.Debugger
Dim trans As EnvDTE80.Transport = dbg2.Transports.Item("Default")
Dim dbgeng(1) As EnvDTE80.Engine
dbgeng(0) = trans.Engines.Item("Managed (v4.0)")
Dim proc2nunitagent As EnvDTE80.Process2 = dbg2.GetProcesses(trans, Environment.MachineName).Item("nunit-agent.exe")
Dim proc2nunit As EnvDTE80.Process2 = dbg2.GetProcesses(trans, Environment.MachineName).Item("nunit.exe")
proc2nunitagent.Attach2(dbgeng)
PostMessage(System.Diagnostics.Process.GetProcessById(proc2nunit.ProcessID).MainWindowHandle, &H100, Keys.Alt Or Keys.R, IntPtr.Zero)
Catch ex As System.Exception
MsgBox(ex.Message + Environment.NewLine + ex.StackTrace)
End Try
End Sub
End Module我使用nunit 2.5.7,为了附加和调试工作,您需要附加到nunit代理并在nunit gui上按下run,对于nunit gui的最新版本,您可以附加到nunit gui。
上面的脚本只允许使用Visual和nunit二进制文件从进行nunit调试。您只需将nunit设置为外部工具(以当前项目作为参数),创建自定义工具栏,并向使用上述脚本的工具栏添加按钮。这将启动nunit,附加到它,开始运行测试,并在您的断点中断。
还有一件事:在Tools -> Settings -> Tree显示中的nunit gui中禁用“保存每个项目的可视化状态”,否则nunit将记住最后一次运行测试,如果这不是您想要调试的测试,这将是很痛苦的。
可能的改进:在启动nunit之前进行构建。
发布于 2012-01-13 18:56:27
试试这个:
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
End Function
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
Private Shared Function FindWindowEx(ByVal parentHandle As IntPtr, ByVal childAfter As IntPtr, ByVal lclassName As String, ByVal windowTitle As String) As IntPtr
End Function
Dim ButtonHandle As IntPtr = FindWindowEx(YOURPROCESS.HANDLE, 0, "Button", "&Run")
SendMessage(ButtonHandle, &HF5, 0, 0)编辑:似乎NUint是一个.NET应用程序,这意味着您无法轻松获得按钮句柄。在Spy++的帮助下,您可以看到这一点。
我注意到按钮Run有ALT + R的键盘快捷方式,所以您可以使用以下代码:
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
Private Shared Function PostMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Boolean
End Function
PostMessage(MAIN_WINDOW_HANDLE, &H100, Keys.Alt Or Keys.R, IntPtr.Zero)我测试过这个,效果很好。
https://stackoverflow.com/questions/8853801
复制相似问题