Current directory for an ActiveX EXE object is set to the location of the system directory and not to the location of the invoking client in Visual Basic .NET or Visual Basic 2005 (834060)



The information in this article applies to:

  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)
  • Microsoft Visual Basic 2005

SYMPTOMS

When the ActiveX EXE object is instantiated by a Microsoft Visual Basic .NET or Visual Basic 2005 client, the default directory that files are read from and that files are written to must be set to the location of the invoking client. Instead, the ActiveX EXE object uses the %windir%\System32 directory as the directory that files are read from and that files are written to.

WORKAROUND

To work around this problem, explicitly set the current working directory of the Visual Basic .NET or Visual Basic 2005 client at run time. To do this in the code of your ActiveX EXE project, use a public property to store the value of the current working directory. You can set this property in the Visual Basic .NET or Visual Basic 2005 client.

For example, in Microsoft Visual Basic 6.0, you can use the following code in your ActiveX EXE project:
' Declare a private variable to store the value of the current working directory.
Private sMyFilePath As String

Public Sub testMethod()
' Concatenate the name of your file with the value of the MyFilePath property
' before opening your file.
Open MyFilePath & "\output0.dat" For Output As #5
Write #5, "writing"
Close #5
End Sub

Private Sub Class_Terminate()
' Concatenate the name of your file with the value of the MyFilePath property
' before opening your file.
Open MyFilePath & "\output1.dat" For Output As #5
Write #5, "terminated"
Close #5
End Sub

' Declare a property get the accessor for the sMyFilePath private variable.
Public Property Get MyFilePath() As String
MyFilePath = sMyFilePath
End Property

' Declare a property let accessor for the sMyFilePath private variable.
Public Property Let MyFilePath(ByVal vNewValue As String)
sMyFilePath = vNewValue
End Property
In the Visual Basic .NET or Visual Basic 2005 client, add a reference to the ActiveX EXE object, add a reference to the System.Windows.Forms.dll assembly, and then use the following code to call the testMethod method of the ActiveX EXE object:
' Create an instance of the ActiveX EXE object.
Dim o As New Project1.Class1()
' Set the MyFilePath property of the ActiveX EXE object to the current working directory.
o.MyFilePath = System.Windows.Forms.Application.StartupPath
' Call the testMethod method of the Active EXE object.
o.testMethod()
' Release the Active EXE object.
o = Nothing

MORE INFORMATION

Steps to reproduce the problem

  1. Start Visual Basic 6.0.
  2. In the New Project window, click ActiveX EXE, and then click Open.
  3. Add the following code to the Class1.cls file:
    Public Sub testMethod()
        Open "output0.dat" For Output As #5
        Write #5, "writing"
        Close #5
    End Sub
    
    Private Sub Class_Terminate()
        Open "output1.dat" For Output As #5
        Write #5, "terminated"
        Close #5
    End Sub
  4. On the File menu, click Make Project1.exe.

    Note Save the Project1.exe file in C:\.
  5. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
  6. Create a Windows application that is named SampleWinApp in Visual Basic .NET or Visual Basic 2005.
  7. On the Project menu, click Add Reference.
  8. In the Add Reference window, click Browse.

    The Project1.exe file that you created in step 4 is added.
  9. In the Solution Explorer window, right-click SampleWinApp, point to Add, and then click Add Module to add a module that is named Module1.vb.
    Note In Visual Studio 2005, clickModule instead of Add Module.
  10. Replace the code in the Module1.vb file with the following code:
    Module Module1
        Sub Main()
            Dim o As New Project1.Class1
            o.testMethod()
            o = Nothing
        End Sub
    End Module
  11. In the Solution Explorer window, right-click Form1.vb, and then click View Code.
  12. Add the following code to the Form1_load event handler:
    Module1.Main()
  13. On the Debug menu, click Start to run the application.

    Notice that the Output0.dat file and the Output1.dat file are created in the %windir%\System32 directory instead of at the location of the invoking client

REFERENCES

For additional information, click the following article number to view the article in the Microsoft Knowledge Base:

315847 How to use ActiveX components in Visual Studio .NET with Visual Basic .NET

For additional information about using ActiveX Controls with Windows forms in Visual Studio .NET, visit the following Microsoft Developer Network (MSDN) Web site:

Modification Type:MinorLast Reviewed:10/3/2006
Keywords:kbvs2005swept kbvs2005applies kbCOMInterop kbprb KB834060 kbAudDeveloper