我怎么在我的程序中调用一个基于windows控制台的程序,使得我的控制台程序运行完毕后自动关闭那个控制台窗口?谢谢了

解决方案 »

  1.   

    HWND hwnd = FindWindow(...)//find the console application
    SendMessage(hwnd,WM_CLOSE,0,0)
      

  2.   

    lret:=FindWindow(DOS窗口类名, 'MS-DOS 方式');
    SendMessage(lRet, WM_CLOSE, 0, 0)
      

  3.   

    看来我要重新来阐述一下我的问题.
    在windows的开始/运行那里输入一个telnet 127.0.0.1 它执行完之后,自己自动就把那个控制台窗口关了,大家有没有发现?
    可是,我在我的程序中用api调用了一个这样的控制台程序,当程序执行完了后,控制台窗口自己并不会关掉.我要怎么做呢?才能让他自己关呢?
    还有上面这为兄弟说的  lret:=FindWindow(DOS窗口类名, 'MS-DOS 方式');
    其中的'DOS窗口类名'我怎么得到呢?
      

  4.   

    to cg1120(代码最优化-§帮助那些值得帮助的人§) 
    哈哈,又学一招
       谢谢  谢谢 谢谢 谢谢 谢谢
      

  5.   

    晕~~楼上的好像说的都是错的~~~
    AllocConsole创建的控制台用FreeConsole来关闭~~以前写了一个控制台类,不过是VB代码的。
    VERSION 1.0 CLASS
    BEGIN
      MultiUse = -1  'True
      Persistable = 0  'NotPersistable
      DataBindingBehavior = 0  'vbNone
      DataSourceBehavior  = 0  'vbNone
      MTSTransactionMode  = 0  'NotAnMTSObject
    END
    Attribute VB_Name = "Console"
    Attribute VB_GlobalNameSpace = False
    Attribute VB_Creatable = True
    Attribute VB_PredeclaredId = False
    Attribute VB_Exposed = False
    Option Explicit
    Private Declare Function AllocConsole Lib "kernel32" () As Long
    Private Declare Function FreeConsole Lib "kernel32" () As Long
    Private Declare Function GetStdHandle Lib "kernel32" (ByVal nStdHandle As Long) As Long
    Private Declare Function WriteConsole Lib "kernel32" Alias "WriteConsoleA" (ByVal hConsoleOutput As Long, ByVal lpBuffer As Any, ByVal nNumberOfCharsToWrite As Long, lpNumberOfCharsWritten As Long, lpReserved As Any) As Long
    Private Declare Function GetConsoleTitle Lib "kernel32" Alias "GetConsoleTitleA" (ByVal lpConsoleTitle As String, ByVal nSize As Long) As Long
    Private Declare Function SetConsoleTitle Lib "kernel32" Alias "SetConsoleTitleA" (ByVal lpConsoleTitle As String) As Long
    Private Declare Function SetConsoleTextAttribute Lib "kernel32" (ByVal hConsoleOutput As Long, ByVal wAttributes As Long) As Long
    Private Declare Function SetConsoleMode Lib "kernel32" (ByVal hConsoleOutput As Long, dwMode As Long) As Long
    Private Declare Function GetConsoleMode Lib "kernel32" (ByVal hConsoleHandle As Long, lpMode As Long) As Long
    Private Declare Function ReadConsole Lib "kernel32" Alias "ReadConsoleA" (ByVal hConsoleInput As Long, ByVal lpBuffer As String, ByVal nNumberOfCharsToRead As Long, lpNumberOfCharsRead As Long, lpReserved As Any) As Long
    Private Const MAX_PATH = 256
    Private Const STD_INPUT_HANDLE = -10&
    Private Const STD_OUTPUT_HANDLE = -11&
    Private Const STD_ERROR_HANDLE = -12&
    Private mvarhOutput As Long '局部复制
    Private mvarhInput As Long '局部复制
    Private mvarhError As Long '局部复制
    Private mvarTitle As String '局部复制
    Private Type COORD
            x As Integer
            y As Integer
    End Type
    Private Type SMALL_RECT
            Left As Integer
            Top As Integer
            Right As Integer
            Bottom As Integer
    End Type
    Private Type CHAR_INFO
            Char As Integer
            Attributes As Integer
    End TypePrivate Declare Function ReadConsoleOutput Lib "kernel32" Alias "ReadConsoleOutputA" _
    (ByVal hConsoleOutput As Long, lpBuffer As CHAR_INFO, dwBufferSize As COORD, _
    dwBufferCoord As COORD, lpReadRegion As SMALL_RECT) As LongPrivate Declare Function GetConsoleScreenBufferInfo Lib "kernel32" (ByVal hConsoleOutput As Long, lpConsoleScreenBufferInfo As CONSOLE_SCREEN_BUFFER_INFO) As Long
    Private Type CONSOLE_SCREEN_BUFFER_INFO
            dwSize As COORD
            dwCursorPosition As COORD
            wAttributes As Long
            srWindow As SMALL_RECT
            dwMaximumWindowSize As COORD
    End Type
    Private Type CONSOLE_CURSOR_INFO
            dwSize As Long
            bVisible As Long
    End TypePublic Function GetColor() As Long
    Dim i As CONSOLE_SCREEN_BUFFER_INFO
    GetConsoleScreenBufferInfo mvarhOutput, i
    GetColor = i.wAttributes
    End Function
    Public Property Get IN_ENABLE_LINE_INPUT() As Long
    IN_ENABLE_LINE_INPUT = &H2
    End Property
    Public Property Get IN_ENABLE_ECHO_INPUT() As Long
    IN_ENABLE_ECHO_INPUT = &H4
    End Property
    Public Property Get IN_ENABLE_MOUSE_INPUT() As Long
    IN_ENABLE_MOUSE_INPUT = &H10
    End Property
    Public Property Get IN_ENABLE_PROCESSED_INPUT() As Long
    IN_ENABLE_PROCESSED_INPUT = &H1
    End Property
    Public Property Get IN_ENABLE_WINDOW_INPUT() As Long
    IN_ENABLE_WINDOW_INPUT = &H8
    End Property
    Public Property Get OUT_ENABLE_PROCESSED_OUTPUT() As Long
    OUT_ENABLE_PROCESSED_OUTPUT = &H1
    End Property
    Public Property Get OUT_ENABLE_WRAP_AT_EOL_OUTPUT() As Long
    OUT_ENABLE_WRAP_AT_EOL_OUTPUT = &H2
    End Property
    Public Property Get FOREGROUND_BLUE() As Long
    FOREGROUND_BLUE = &H1
    End Property
    Public Property Get FOREGROUND_GREEN() As Long
    FOREGROUND_GREEN = &H2
    End Property
    Public Property Get FOREGROUND_RED() As Long
    FOREGROUND_RED = &H4
    End Property
    Public Property Get FOREGROUND_INTENSITY() As Long
    FOREGROUND_INTENSITY = &H8
    End Property
    Public Property Get BACKGROUND_BLUE() As Long
    BACKGROUND_BLUE = &H10
    End Property
    Public Property Get BACKGROUND_GREEN() As Long
    BACKGROUND_GREEN = &H20
    End Property
    Public Property Get BACKGROUND_RED() As Long
    BACKGROUND_RED = &H40
    End Property
    Public Property Get BACKGROUND_INTENSITY() As Long
    BACKGROUND_INTENSITY = &H80
    End Property
    Public Property Let Mode(ByVal Handle As Long, ByVal vMode As Long)
    SetConsoleMode Handle, vMode
    End Property
    Public Property Get Mode(ByVal Handle As Long) As Long
    Dim s As Long
    GetConsoleMode Handle, s
    Mode = s
    End Property
    Public Property Let Title(ByVal vData As String)
    SetConsoleTitle vData
    End Property
    Public Function SetColor(ByVal nColor As Long) As Long
    SetColor = SetConsoleTextAttribute(mvarhOutput, nColor)
    End Function
    Public Property Get Title() As String
    mvarTitle = String(MAX_PATH, Chr(0))
    GetConsoleTitle mvarTitle, Len(mvarTitle)
    mvarTitle = Left(mvarTitle, InStr(mvarTitle, Chr(0)) - 1)
        Title = mvarTitle
    End Property
    Public Function WriteLine(ByVal szOutput As String) As Long
    'WriteLineBuf = szOutputWriteLine = WriteConsole(mvarhOutput, Replace(szOutput, "\n", vbCrLf), sLen(szOutput), vbNull, vbNull)
    End Function
    Private Function sLen(ByVal mm As String) As Long
    Dim bArr() As Byte
    bArr = StrConv(mm, vbFromUnicode)
    sLen = UBound(bArr) + 1
    Erase bArr
    End Function
    Public Function ReadLine() As String
    Dim s As String
    s = String(MAX_PATH, Chr(0))
    ReadConsole mvarhInput, s, Len(s), vbNull, vbNull
    ReadLine = Left(s, InStr(s, Chr(0)) - 1)
    End Function
    Public Property Get hError() As Long
    '检索属性值时使用,位于赋值语句的右边。
    'Syntax: Debug.Print X.hError
        hError = mvarhError
    End Property
    Public Property Let hError(ByVal vNewHandle As Long)
    '检索属性值时使用,位于赋值语句的右边。
    'Syntax: Debug.Print X.hError
         mvarhError = vNewHandle
    End PropertyPublic Property Let hInput(ByVal vNewHandle As Long)
    '检索属性值时使用,位于赋值语句的右边。
    'Syntax: Debug.Print X.hError
         mvarhInput = vNewHandle
    End PropertyPublic Property Get hInput() As Long
    '检索属性值时使用,位于赋值语句的右边。
    'Syntax: Debug.Print X.hInput
        hInput = mvarhInput
    End PropertyPublic Property Let hOutput(ByVal vNewHandle As Long)
    '检索属性值时使用,位于赋值语句的右边。
    'Syntax: Debug.Print X.hError
         mvarhOutput = vNewHandle
    End PropertyPublic Property Get hOutput() As Long
    '检索属性值时使用,位于赋值语句的右边。
    'Syntax: Debug.Print X.hOutput
        hOutput = mvarhOutput
    End Property
    Private Sub Class_Initialize()
    AllocConsole
    Title = App.Title
    mvarhOutput = GetStdHandle(STD_OUTPUT_HANDLE)
    mvarhInput = GetStdHandle(STD_INPUT_HANDLE)
    mvarhError = GetStdHandle(STD_ERROR_HANDLE)
    SetColor FOREGROUND_RED Or FOREGROUND_GREEN Or FOREGROUND_BLUE
    End Sub
    Private Sub Class_Terminate()
    FreeConsole
    End Sub
      

  6.   

    有没有用delphi或vc的,vb美学过,我看着太累了.
      

  7.   

    lret:=FindWindow(DOS窗口类名, 'MS-DOS 方式');
    SendMessage(lRet, WM_CLOSE, 0, 0)这个DOS窗口类名可以用VS附带的Spy++来得到。
      

  8.   

    没在DOS下开发过程序吗?当程序执行到end.就自然结束了!