Microsoft KB Archive/834060

= 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 =

Article ID: 834060

Article Last Modified on 11/27/2007

-

APPLIES TO


 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * 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 & &quot;\output0.dat&quot; For Output As #5 Write #5, &quot;writing&quot; 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 & &quot;\output1.dat&quot; For Output As #5 Write #5, &quot;terminated&quot; 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



Steps to reproduce the problem
 Start Visual Basic 6.0. In the New Project window, click ActiveX EXE, and then click Open.  Add the following code to the Class1.cls file: Public Sub testMethod Open &quot;output0.dat&quot; For Output As #5 Write #5, &quot;writing&quot; Close #5 End Sub

Private Sub Class_Terminate Open &quot;output1.dat&quot; For Output As #5 Write #5, &quot;terminated&quot; Close #5 End Sub  On the File menu, click Make Project1.exe.

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

The Project1.exe file that you created in step 4 is added.</li> 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.</li>  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 </li> In the Solution Explorer window, right-click Form1.vb, and then click View Code.</li>  Add the following code to the Form1_load event handler: Module1.Main </li> 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</li></ol>

<div class="references_section">