我正在對使用Visual Basic 6編寫的舊應用程序之一進行一些維護工作,由於各種原因,我們只有一部分代碼只需要運行if我們通過VB6 IDE運行程序(即,調試器已連接)。檢測調試器是否附加在VB6中
在VB.NET中,您可以通過使用System.Diagnostics.Debugger.IsAttached()
屬性來完成此操作,但在Google上找不到類似的VB6。
是否有一些簡單的方法可以將這些信息輸出?
我正在對使用Visual Basic 6編寫的舊應用程序之一進行一些維護工作,由於各種原因,我們只有一部分代碼只需要運行if我們通過VB6 IDE運行程序(即,調試器已連接)。檢測調試器是否附加在VB6中
在VB.NET中,您可以通過使用System.Diagnostics.Debugger.IsAttached()
屬性來完成此操作,但在Google上找不到類似的VB6。
是否有一些簡單的方法可以將這些信息輸出?
這是我一直在使用的功能:
Private Function RunningInIde() As Boolean
On Error GoTo ErrHandler
Debug.Print 1/0
ErrHandler:
RunningInIde = (Err.Number <> 0)
End Function ' RunningInIde
下面是我們使用的是沒有任何副作用的
Public Property Get InIde() As Boolean
Debug.Assert pvSetTrue(InIde)
End Property
Private Function pvSetTrue(bValue As Boolean) As Boolean
bValue = True
pvSetTrue = True
End Function
無副作用海報意味着,如果你打開了「打破所有錯誤」,這將不會像@Jay Riggs所接受的答案那樣停止程序的執行(如果你全部使用InIde,這可能會非常煩人 – 2012-03-02 16:17:08
@Kris:那麼,如果使用'On Error Resume Next'' +'Err.Number <> 0'風格的錯誤檢查,不會重置'Err'對象。順便說一句,從來沒有見過任何人使用'Break On Unhandled錯誤「(除非對這些IDE設置無能爲力) – wqw 2012-03-03 09:40:20
@Kris:在您的原始評論發佈兩年後,我確信」突破所有錯誤「是進行任何遠程專業項目的方式,它耗費我一隻手,一條腿重構我所有的「代碼」,但它是值得的,感謝大開眼界! – wqw 2014-07-09 12:25:51
我寫了這樣的一段時間回來,並可以」找到它,並再次需要它。所以我只是寫了一遍,我想我說得對:
Public Function IsRunningInIde() As Boolean
Static bFlag As Boolean
bFlag = Not bFlag
If bFlag Then Debug.Assert IsRunningInIde()
IsRunningInIde = Not bFlag
bFlag = False
End Function
沒有錯誤得到提出。
沒有重新設置Err。
只是一個功能。
第1行:「bFlag」的「靜態」聲明會導致bFlag的值跨越多次調用「IsRunningInIde」。我們需要這個,因爲我在它自己內部調用了這個函數,並且我不想用用戶不需要的輸入參數來拋棄函數。
第3行:未在IDE中運行時調用「Debug.Assert」。所以只有在IDE中「IsrunningInIde」被遞歸調用。
第2行:如果不在遞歸調用中,bFlag會啓動false,並將其設置爲true。如果在遞歸調用中(僅在IDE中運行時發生),它將從true開始,並被設置爲false。
第3行:只要調用「IsRunningInIde」,如果它不是已經在這個函數中遞歸地檢查bFlag是否爲真。第4行:如果在遞歸調用中,總是返回True,這並不重要,但不會導致Assert失敗。如果不是遞歸調用,則返回「Not bFlag」,如果遞歸調用IsRunningInIde,則bFlag現在爲「False」,如果不是遞歸調用,則bFlag爲「True」。所以基本上,如果它在IDE中運行,Not bFlag將返回「True」。
第5行:清除bFlag,以便在下次調用此函數時始終爲「False」。
這很難解釋,在這兩種情況下,最好在腦海中逐步穿過它。
如果您想更簡單地理解代碼,請不要使用它。
如果這段代碼有問題,我很抱歉,讓我知道,所以我可以修復它。
請解釋它爲什麼這個工程 – 2012-12-26 19:17:38
好吧,我更新瞭解釋,我意識到這是混淆邏輯。 – 2012-12-26 19:32:45
這是我的功能,類似於Josh的一個,但更簡單的閱讀(見註釋裏面)。
我用了這麼久,我忘了,我從借來的......
Public Function InDesign() As Boolean
' Returns TRUE if in VB6 IDE
Static CallCount As Integer
Static Res As Boolean
CallCount = CallCount + 1
Select Case CallCount
Case 1 ' Called for the 1st time
Debug.Assert InDesign()
Case 2 ' Called for the 2nd time: that means Debug.Assert
' has been executed, so we're in the IDE
Res = True
End Select
' When Debug.Assert has been called, the function returns True
' in order to prevent the code execution from breaking
InDesign = Res
' Reset for further calls
CallCount = 0
End Function
Public Function InIDE() As Boolean
On Error GoTo ErrHandler
Debug.Print 1 \ 0 'If compiled, this line will not be present, so we immediately get into the next line without any speed loss
InIDE = False
Exit Function
ErrHandler:
InIDE = True 'We'll get here if we're in the IDE because the IDE will try to output "Debug.Print 1 \ 0" (which of course raises an error).
End Function
參見問題[在VB6調試模式(http://stackoverflow.com/questions/ 9052024/debug-mode-in-vb-6) – MarkJ 2012-02-29 20:21:24
在IDE中運行與附加調試器不一樣,當然也可以完成,但這是不同的事情。 – Bob77 2012-03-01 12:51:18