方法' WriteLine'对象' ITextStream'失败

时间:2017-04-03 10:47:25

标签: vb6

这可能是一个基本问题,但无法弄清楚这个问题,因为它对VB 6来说还是一个新手。 这是我在网上搜索此错误代码时所得到的

  

“方法'〜'对象'〜'失败"这通常发生在dll没有的时候   注册。确切地说,当运行时甚至无法分辨出什么时   对象是,并且对象无法传播更多信息   背部。它可以是任何组件。这是一个不匹配的DLL文件。“

尝试使用regsvr32.exe注册 scrrun.dll ,但没有运气

在尝试运行程序时,在下面的代码块中获取错误

Sub PrintToConsole(ByVal LineToPrint As String)
    Dim fso As New FileSystemObject
    fso.GetStandardStream(StdOut).WriteLine (LineToPrint)
End Sub

3 个答案:

答案 0 :(得分:1)

要链接控制台子系统,您可以将这些行添加到.VBP文件的末尾:

[VBCompiler]
LinkSwitches=/SUBSYSTEM:CONSOLE

然后编译的EXE应该可以正常工作。

还要考虑这个示例程序。这是Module1.bas:

Option Explicit

Private StdIn As Scripting.TextStream
Private StdOut As Scripting.TextStream

'--- Only required for testing in IDE or Windows Subsystem ===
Private Declare Function AllocConsole Lib "kernel32" () As Long
Private Declare Function GetConsoleTitle Lib "kernel32" _
    Alias "GetConsoleTitleA" ( _
    ByVal lpConsoleTitle As String, _
    ByVal nSize As Long) As Long
Private Declare Function FreeConsole Lib "kernel32" () As Long

Private mEnableConsole As Boolean
Private mAllocated As Boolean

Private Property Get EnableConsole() As Boolean
    EnableConsole = mEnableConsole
End Property

Private Property Let EnableConsole(ByVal RHS As Boolean)
    Dim Title As String

    If Not RHS And mEnableConsole Then
        If mAllocated Then
            StdOut.Write "Press enter to continue..."
            StdIn.ReadLine
            FreeConsole
            mAllocated = False
        End If
        mEnableConsole = False
    ElseIf RHS And Not mEnableConsole Then
        Title = Space$(260)
        If GetConsoleTitle(Title, 260) = 0 Then
            AllocConsole
            mAllocated = True
        End If
        mEnableConsole = True
    End If
End Property
'--- End testing ---------------------------------------------

Private Sub Main()
    Dim Msg As String

    EnableConsole = True
    On Error GoTo Cleanup

    With New Scripting.FileSystemObject
        Set StdIn = .GetStandardStream(Scripting.StdIn)
        Set StdOut = .GetStandardStream(Scripting.StdOut)
    End With

    With StdOut
        .Write "Hello from "
        .Write App.Path
        .Write "\"
        .Write App.EXEName
        .WriteLine ".exe"

        .Write "Command line: ["
        .Write Command$()
        .WriteLine "]"

        .WriteLine "Type an input message and press Enter:"

        Msg = StdIn.ReadLine()
        .WriteLine "I saw:"
        .WriteLine Msg
    End With
    Msg = 1 / 0 'Cause an exception for illustration!

Cleanup:
    If Err Then
        With StdOut
            .Write "#Error: "
            .Write CStr(Err.Number)
            .Write ", "
            .WriteLine Err.Description
        End With
    End If
    EnableConsole = False
End Sub

这可以很方便,因为我们大多数人都喜欢能够测试和调试。

为了测试已编译的程序,您可以尝试类似TestProject1.cmd:

@Echo Off
Project1 Run from CMD file!
Pause

假设您当然编译为Project1.exe。

您也可以StdInStdOutEnableConsole所有Public来访问其他模块。我可能只做EnableConsole" getter" Public我自己。

答案 1 :(得分:0)

试试这个:

Dim fso as Object
Set fso = CreateObject("Scripting.FileSystemObject")
fso.GetStandardStream(StdOut).WriteLine (LineToPrint)

答案 2 :(得分:0)

GetStandardStream方法返回一个TextStream对象。

FileSystemObject对象 https://msdn.microsoft.com/en-us/library/z9ty6h50(v=vs.84).aspx

GetStandardStream方法 https://msdn.microsoft.com/en-us/library/y6hbz9es(v=vs.84).aspx