tags:

views:

3503

answers:

5

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?

+2  A: 

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
Gulzar
+5  A: 

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
Pascal Paradis
+2  A: 

Solution that I have found works best uses a Sub Main, and checks if 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
Kris Erickson
+4  A: 

"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.

GSerg
A: 

Current directory for any program - including vb6 - can be changed in the properties of the shortcut. I've changed it to the root of my source tree, it makes using File-Open quicker.

Kaniu