Microsoft KB Archive/110701

= How to Get a Handle to MS-DOS Application and Change Title =

Article ID: 110701

Article Last Modified on 10/29/2003

-

APPLIES TO


 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 3.0 Professional Edition

-



This article was previously published under Q110701



SUMMARY
This article shows by example how to get the handle to a MS-DOS application, and then use that handle to change the MS-DOS Window title or automatically unload the MS-DOS Window from Visual Basic.



Step-by-Step Example
 Start a new project in Visual Basic. Form1 is created by default. Add two command buttons (Command1 and Command2) to Form1.  Enter the following code in the General Declarations of a form or module: ' Enter each of the following Declare statements on one, single line: Declare Function PostMessage Lib "User" (ByVal hWnd As Integer, ByVal wMsg As Integer,     ByVal wParam As Integer, lParam As Any) As Integer Declare Sub SetWindowText Lib "User" (ByVal hWnd As Integer, ByVal lpString As String) Declare Function GetActiveWindow Lib "User" As Integer Dim MhWnd as Integer Const WM_CLOSE = &H10

  Enter the following code in the Click Event of Command1: Sub Command1_Click Dim X as Integer X = Shell("c:\windows\dosprmpt.pif", 1) ' Open an MS-DOS Window For X = 0 To 100 DoEvents ' a bunch of DOEVENTS to wait for the MS-DOS Window to open. Next X

' Get the handle when the MS-DOS Window has the focus: Mhwnd = GetActiveWindow

' Now pass the handle to the window with a new title: Call SetWindowText(Mhwnd, "My Application!") End Sub   Place the following code in the Click Event of Command2:

Sub Command2_Click Dim X as Integer ' Note: In order for this to work on a MS-DOS Window, you have ' to have a PIF setup that will allow an MS-DOS Window to be closed. ' In the PIF editor, select "Advanced", then click "Close When     ' Active". This allows MS-DOS applications to be closed ' programatically X = PostMessage(MhWnd, WM_CLOSE, 0, 0) ' Return greater than zero ' if successful. If X = 0 Then MsgBox "Application did not close!" End If  End Sub  Start the program, or press the F5 key. Click the Command1 button to see the title change.</li></ol>

Additional query words: 2.00 3.00

Keywords: KB110701

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.