如何在调试VB6应用程序时设置工作目录?

13

我正在调试一个VB6可执行文件。该可执行文件在运行时从当前目录加载dll和文件。但是,在调试器中运行时,当前目录似乎是VB6的目录。

如何设置VB6的工作目录?

5个回答

11

针对这件事,似乎没有一个“开箱即用”的解决方案。

引用自旧版 Joel On Software 论坛

无论如何...为了结束这个话题... 我的 VB6 解决方案如下: 在我的 VB 项目中定义两个符号“MPDEBUG”和“MPRELEASE”,并在应用程序入口函数中作为第一个操作调用以下函数。

Public Sub ChangeDirToApp()
#If MPDEBUG = 0 And MPRELEASE = 1 Then
  ' assume that in final release builds the current dir will be the location
  ' of where the .exe was installed; paths are relative to the install dir
  ChDrive App.path
  ChDir App.path
#Else
  ' in all debug/IDE related builds, we need to switch to the "bin" dir
  ChDrive App.path
  ChDir App.path & BackSlash(App.path) & "..\bin"
#End If
End Sub

8
我找到的解决方案是使用一个Sub Main,并检查程序是否在IDE中运行。
Dim gISIDE as Boolean

Sub Main()
    If IsIDE Then
        ChDrive App.Path
        ChDir   App.Path
    End If

    ' The rest of the code goes here...

End Sub

Public Function IsIDE() As Boolean '
        IsIDE = False
        'This line is only executed if running in the IDE and then returns True
        Debug.Assert CheckIDE 
        If gISIDE Then 
            IsIDE = True
        End If
End Function

Private Function CheckIDE() As Boolean ' this is a helper function for Public Function IsIDE() 
        gISIDE = True 'set global flag 
        CheckIDE = True 
End Function

8
只有当您使用“文件-打开”打开项目时,“当前目录似乎是VB6的目录”。
在IDE关闭的情况下,通过双击.vbp文件来打开它。

1

这个能行吗?

'Declaration
Private Declare Function SetCurrentDirectory Lib "kernel32" _
Alias "SetCurrentDirectoryA" (ByVal lpPathName As String) As Long

'syntax to set current dir
SetCurrentDirectory App.Path

2
但是,使用本地的 VB6 命令 ChDrive App.Path: ChDir App.Path 更简单。 - MarkJ
为什么要使用这个而不是内置函数? - StayOnTarget

1

任何程序(包括vb6)的当前目录都可以在快捷方式的属性中更改。我已将其更改为我的源代码树的根目录,这使得使用文件-打开更快。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接