How to return value from my vb application to dos?

I created vb program and i want that my program return value wen exiting.
KitaipAsked:
Who is Participating?
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Here you go:

' API Call to generate an Exit Code *** Do all clean up First! ***
' When this function is called the program IMMEDIATELY exits with the
' specified exit code.
Private Declare Sub ExitProcess Lib "kernel32" (ByVal uExitCode As Long)

' Used to determine if we are running in the IDE or not
Private Declare Function GetModuleFileName Lib "kernel32" Alias _
    "GetModuleFileNameA" (ByVal hModule As Long, ByVal lpFileName As String, _
    ByVal nSize As Long) As Long

Private Sub Form_Unload(Cancel As Integer)
    Dim code As Long
    Dim strFileName  As String
    Dim lngCount As Long

    code = 5
    strFileName = String(255, 0)
    lngCount = GetModuleFileName(App.hInstance, strFileName, 255)
    strFileName = Left(strFileName, lngCount)
    If UCase(Right(strFileName, Len(App.EXEName) + 4)) = UCase(App.EXEName) & ".EXE" Then
        ExitProcess code ' We are not in the IDE...actually exit with code
    Else
        MsgBox "Exit Code: " & code ' We are in the IDE...simulate exit w/ code
    End If
End Sub
0
 
mladenoviczCommented:
Try ExitProcess API

Private Declare Sub ExitProcess Lib "kernel32" (ByVal uExitCode As Long)
Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Sub Form_Load()
    'KPD-Team 1999
    'URL: http://www.allapi.net/
    'E-Mail: KPDTeam@Allapi.net
    'end this process
    ExitProcess GetExitCodeProcess(GetCurrentProcess, 0)
End Sub
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
mladenoviczCommented:
Idle_Mind was faster ;)
0
 
unknown_routineCommented:
return value where?

If you just want to see a value or put it inide the registry , the only thing you need is
     
Form_Unload event




Form_Unload
     Msgbox "Myvariable is :" & myvariable
   'code to write to a text file
End Sub


0
 
KitaipAuthor Commented:
Example:

c:\unzip.exe d:\archive.zip

//unzip.exe returns to dos
bad zip file.

or somthing like that

I want return of this type, like in example.
0
 
mladenoviczCommented:
Option Explicit

Public Sub Main()
  Dim FSO As Object
  Set FSO = CreateObject("Scripting.FileSystemObject")
 
  Call FSO.GetStandardStream(1).WriteLine("Some text...")
End Sub


Compile you app (you will get e.g project1.exe), and after that type this in command prompt
editbin /subsystem:console project1.exe

Now you can start your app from command prompt and you will get result back in console
0
 
mladenoviczCommented:
you will need reference to the Microsoft scripting Runtime
0
 
mladenoviczCommented:
I wrote code with late binding so you will not need reference to MSR.
Editbin goes with Visual Studio
0
 
KitaipAuthor Commented:
I need effect like in C++:  
Main() {

return "Some text..."
}
0
 
mladenoviczCommented:
I am not familliar with C++, but above code will write to the command prompt window
0
 
KitaipAuthor Commented:
there are some errors vith that code
 
0
 
KitaipAuthor Commented:
it dont work

when runing compiled .exe there is one error, and when running from vb there is other error
0
 
mladenoviczCommented:
can you post error number, description and line where error occurs
0
 
KitaipAuthor Commented:
Run-time error '-2147024890 (80070006)':

Method 'WriteLine' of object 'ITextStream' failed
0
 
KitaipAuthor Commented:
Run-time error '-2147024890 (80070006)':

Method Method '~' of object '~' failed
0
 
mladenoviczCommented:
Ok, let's try this

1. create new project.
2. add new module to the project
3. paste this code to the module
Option Explicit

Public Sub Main()
  Dim FSO As Object
  Set FSO = CreateObject("Scripting.FileSystemObject")
 
  Call FSO.GetStandardStream(1).WriteLine("Some text...")
End Sub
4. go to project->project properties
5. set startup object to the Sub Main
6. compile project (create project1.exe)
7. open command prompt.
8. go to the folder where exe is stored
9. type to the command line this: editbin /subsystem:console project1.exe
10 type to the command line this: type project1

this gives me "Some text..." as output.
You must process exe file with editbin in order to get output to the console
0
 
KitaipAuthor Commented:

Microsoft (R) COFF Binary File Editor Version 5.00.7022
Copyright (C) Microsoft Corp 1992-1997. All rights reserved.


EDITBIN : warning LNK4044: unrecognized option "subsystem:console"; ignored
0
 
mladenoviczCommented:
Obviously that is the problem.

My version is 6.00.8447 and everything works fine. I am running Windows XP.
0
 
mladenoviczCommented:
Visual Studio SP6
0
 
KitaipAuthor Commented:

And not using "subsystem:console" is any way to this.

and

Mayby you know how to make application return values like functions.
that value is returned only when program ends.
0
 
mladenoviczCommented:
Try to install latest SP for VB (SP6)

Check ExitProcess on the top of this question
0
 
KitaipAuthor Commented:
i have only VB5!
0
 
mladenoviczCommented:
Looks like you have to have VB6 in order to use EDITBIN
0
 
KitaipAuthor Commented:
there is a problem to get VB6 to me. i didn find any links to download it
 
0
 
unknown_routineCommented:
Kitaip :


Dont bother getting VB6.

What you want is a console application.The problem is that VB6  does not support DOS(console) applications.

So the above code will not work.


Microsoft understood this problem with VB6 and then added this functionality to VB.NET.







0
 
unknown_routineCommented:
Kitaip :

I have enterprise edition of VB6 and windoes 2000 pro. and that code does not work.


As I said your solution is use C++ or a much easier way VB.NET.

VB simply does not have this functionality.
0
 
unknown_routineCommented:
Hmmm,

It seems I proved to be wrong,

I found a solution on how to write to console here:

http://www.freevbcode.com/ShowCode.asp?ID=4618

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
unknown_routine,

You are only partially wrong (or correct) depending how you want to look at.

It is easy to create a new console window and redirect input/output to it as in the example you have provided.

What is not easy however, is making your application output to the DOS window that your application was called from.

The only examples I have ever seen for a VB6 application, are those for outputting to a console window that it created and not to the calling DOS window.

Idle_Mind
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I was able to get mladenoviczs link to work.  I created a new project and added a reference to the Microsoft Scripting Runtime.  Then I added a Module with the following code:

    Sub Main()
        Dim fso As New FileSystemObject
        fso.GetStandardStream(StdOut).Write ("Hello World")
    End Sub

From the Project Properties, I set the startup object to be Sub Main and then removed Form1 from the project.  I compiled the application as VBConsole.exe in the following folder:

    C:\Program Files\Microsoft Visual Studio\VC98\Bin

Then I copied this file into the Bin folder as well:

    C:\Program Files\Microsoft Visual Studio\VB98\MSPDB60.DLL

Next I used the EditBin utility as described in the PDF document and ran VBConsole.  "Hello World" did echo back successfully to the DOS console from which my app was run.  Here is the text from my Console window showing EditBin being used and then VBConsole.Exe being run:

C:\PROGRA~1\MICROS~6\VC98\BIN>editbin /subsystem:console vbconsole.exe
Microsoft (R) COFF Binary File Editor Version 6.00.8447
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.



C:\PROGRA~1\MICROS~6\VC98\BIN>vbconsole
Hello World
C:\PROGRA~1\MICROS~6\VC98\BIN>
0
 
mladenoviczCommented:
Idle_Mind, I wanted to hear that ;)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.