Solved

How to return value from my vb application to dos?

Posted on 2004-09-03
35
2,809 Views
Last Modified: 2012-08-14
I created vb program and i want that my program return value wen exiting.
0
Comment
Question by:Kitaip
  • 15
  • 10
  • 4
  • +1
35 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 11972403
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
 
LVL 8

Expert Comment

by:mladenovicz
ID: 11972411
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
 
LVL 8

Expert Comment

by:mladenovicz
ID: 11972418
Idle_Mind was faster ;)
0
 
LVL 15

Expert Comment

by:unknown_routine
ID: 11972666
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
 

Author Comment

by:Kitaip
ID: 11972826
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
 
LVL 8

Expert Comment

by:mladenovicz
ID: 11972855
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
 
LVL 8

Expert Comment

by:mladenovicz
ID: 11972863
you will need reference to the Microsoft scripting Runtime
0
 
LVL 8

Expert Comment

by:mladenovicz
ID: 11972904
I wrote code with late binding so you will not need reference to MSR.
Editbin goes with Visual Studio
0
 
LVL 8

Expert Comment

by:mladenovicz
ID: 11972914
0
 

Author Comment

by:Kitaip
ID: 11973070
I need effect like in C++:  
Main() {

return "Some text..."
}
0
 
LVL 8

Expert Comment

by:mladenovicz
ID: 11973117
I am not familliar with C++, but above code will write to the command prompt window
0
 

Author Comment

by:Kitaip
ID: 11973201
there are some errors vith that code
 
0
 

Author Comment

by:Kitaip
ID: 11973219
it dont work

when runing compiled .exe there is one error, and when running from vb there is other error
0
 
LVL 8

Expert Comment

by:mladenovicz
ID: 11973248
can you post error number, description and line where error occurs
0
 

Author Comment

by:Kitaip
ID: 11973287
Run-time error '-2147024890 (80070006)':

Method 'WriteLine' of object 'ITextStream' failed
0
 

Author Comment

by:Kitaip
ID: 11973301
Run-time error '-2147024890 (80070006)':

Method Method '~' of object '~' failed
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 8

Expert Comment

by:mladenovicz
ID: 11973382
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
 

Author Comment

by:Kitaip
ID: 11973482

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
 
LVL 8

Expert Comment

by:mladenovicz
ID: 11973553
Obviously that is the problem.

My version is 6.00.8447 and everything works fine. I am running Windows XP.
0
 
LVL 8

Expert Comment

by:mladenovicz
ID: 11973568
Visual Studio SP6
0
 

Author Comment

by:Kitaip
ID: 11973622

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
 
LVL 8

Expert Comment

by:mladenovicz
ID: 11973804
Try to install latest SP for VB (SP6)

Check ExitProcess on the top of this question
0
 

Author Comment

by:Kitaip
ID: 11974035
i have only VB5!
0
 
LVL 8

Expert Comment

by:mladenovicz
ID: 11974057
Looks like you have to have VB6 in order to use EDITBIN
0
 

Author Comment

by:Kitaip
ID: 11974142
there is a problem to get VB6 to me. i didn find any links to download it
 
0
 
LVL 15

Expert Comment

by:unknown_routine
ID: 11975947
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
 
LVL 15

Expert Comment

by:unknown_routine
ID: 11975984
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
 
LVL 15

Expert Comment

by:unknown_routine
ID: 11976044
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 11976905
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
 
LVL 8

Accepted Solution

by:
mladenovicz earned 500 total points
ID: 12563855
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 12565746
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
 
LVL 8

Expert Comment

by:mladenovicz
ID: 12566070
Idle_Mind, I wanted to hear that ;)
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now