PRB: Cannot Simultaneously Write to Disk File and Have It Opened in ActiveX DLLID: Q214674
|
When you attempt to write to the file number of a file that was opened in an ActiveX DLL or vice versa, the following run-time error is generated:
Error '52' Bad File name or number
The file number created when a file is opened cannot be shared between separate projects or executable modules.
If a file must be accessed by both an EXE and an associated DLL, wrap all of the file access activities in a class module in the ActiveX DLL.
This behavior is by design.
Option Explicit
Private m_Fn As Integer
Public Sub WriteFile(Fn As Variant)
Write #Fn, "This is a text from the DLL."
End Sub
Public Sub OpenFile()
m_Fn = FreeFile
Open "C:\DllTEST.txt" For Append As #m_Fn
End Sub
Private Sub Class_Initialize()
m_Fn = 0
End Sub
Public Property Get DLLFileNum() As Variant
DLLFileNum = m_Fn
End Property
Public Sub CloseFile()
If m_Fn <> 0 Then
Close #m_Fn
m_Fn = 0
End If
End Sub
Option Explicit
Private Sub Command1_Click()
Dim objvar As New DllFileNumber.Class1
Dim sVar As String
sVar = "This is text from the main program."
objvar.OpenFile
objvar.WriteFile objvar.DLLFileNum
Write #objvar.DLLFileNum, sVar 'Error 52 occurs here.
objvar.CloseFile
End Sub
Private Sub Command2_Click()
Dim FileNumber As Variant
Dim objvar As New DllFileNumber.Class1
FileNumber = FreeFile
Open "C:\TEST.txt" For Append As #FileNumber
Write #FileNumber, "This is text from the main program."
objvar.WriteFile FileNumber 'Error 52 occurs here.
Close #FileNumber
End Sub
Private Sub Form_Load()
Command1.Caption = "Write to a file opened in the DLL"
Command2.Caption = "Write to a file opened in the main program"
End Sub
' Error 52 occurs here.
Additional query words: kbGrpVB
Keywords : kbsample kbActiveX kbDLL kbVBp kbVBp500 kbVBp600 kbGrpVB
Version : WINDOWS:5.0,6.0
Platform : WINDOWS
Issue type : kbprb
Last Reviewed: February 3, 1999