I am debugging a VB6 executable. The executable loads dlls and files from it's current directory, when running. When run in debugger, the current directory seems to be VB6's dir.
How do I set working directory for VB6?
Solution that I have found that works uses a Sub Main
, and checks if the program is running in the 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
It doesn't seems to be a "out of the box" solution for this thing.
Taken from The Old Joel On Software Forums
Anyways.. to put this topic to rest.. the following was my VB6 solution: I define 2 symbols in my VB project "MPDEBUG" and "MPRELEASE" and call the following function as the first operation in my apps entry point function.
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
"The current directory seems to be VB6's dir" only when you open a project using File-Open.
Open it by double clicking the .vbp file while having the IDE closed.
Will this work?
'Declaration
Private Declare Function SetCurrentDirectory Lib "kernel32" _
Alias "SetCurrentDirectoryA" (ByVal lpPathName As String) As Long
'syntax to set current dir
SetCurrentDirectory App.Path
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With