execution from command line

S

Selva Chinnasamy

Hi I have a Windows application written in VB which has many functions. In
addition it also reads a database and generates a output file.

Currently this function is called when ever user clicks the button from UI.
But I like to call the function from command line too.
app.exe database.mdb should call the function to generate the output file.

I have modified my Main() to do this. but I wasn't able to send output to
console. I have tried many functions in the Console class, but none was
helpful.

Any ideas on how to send output (or) error info to the console window.

Thanks In Advance.
 
G

Guest

Selva,

You should be able to write to the console with a method such as
Console.Writeline.

You might need to do a Console.Readline as the last statement to the console
window, so the window remains on screen instead of disappearing. Then you can
hit Enter to close the window.

Kerry Moorman
 
Z

zacks

Kerry said:
Selva,

You should be able to write to the console with a method such as
Console.Writeline.

You might need to do a Console.Readline as the last statement to the console
window, so the window remains on screen instead of disappearing. Then you can
hit Enter to close the window.

You can access a Console window from a Windows Forms app?
 
S

Selva Chinnasamy

Kerry, I have tried doing that. But nothing gets printed to the console
window.

Selva
 
J

Jeffrey Tan[MSFT]

Hi Selva,

Based on my understanding, your winform application wanted to read input
from command line while startup and write some output to the console
window.

Yes, the winform application will disable the console window by default, so
the Console.WriteLine method will not have any effect in winform
application. To workaround this problem, you have 2 choices:

1. The simplest way is: after creating the winform project, you may change
the project type from "Windows Application" to "Console Application"(in
Project setting dialog->General->"Output Type" dropdownlist). This will
force the runtime to display a console window follow with the GUI form, and
any Console.WriteLine method will be displayed in the window.

2. If you do not want to show the console window while startup, but want to
display it on-demand. You'd better p/invoke Win32 Console API AllocConsole
and AllocConsole to get this done.

AllocConsole API will allocate a new console for the calling process, which
Console.WriteLine will be displayed, while FreeConsole() will destroy this
console when you do not want it. Below is the working sample code snippet:

Imports System.Runtime.InteropServices
Public Class Form1
Inherits System.Windows.Forms.Form

Public Class Win32
<DllImport("kernel32.dll")> _
Public Shared Function AllocConsole() As [Boolean]
End Function

<DllImport("kernel32.dll")> _
Public Shared Function FreeConsole() As [Boolean]
End Function
End Class 'Win32

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Win32.AllocConsole()
Console.WriteLine("abc")

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Win32.FreeConsole()
End Sub
End Class

Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
S

Selva Chinnasamy

Thank You so much Jeff. Thats exactly I am looking for,

Thanks Again.

"Jeffrey Tan[MSFT]" said:
Hi Selva,

Based on my understanding, your winform application wanted to read input
from command line while startup and write some output to the console
window.

Yes, the winform application will disable the console window by default, so
the Console.WriteLine method will not have any effect in winform
application. To workaround this problem, you have 2 choices:

1. The simplest way is: after creating the winform project, you may change
the project type from "Windows Application" to "Console Application"(in
Project setting dialog->General->"Output Type" dropdownlist). This will
force the runtime to display a console window follow with the GUI form, and
any Console.WriteLine method will be displayed in the window.

2. If you do not want to show the console window while startup, but want to
display it on-demand. You'd better p/invoke Win32 Console API AllocConsole
and AllocConsole to get this done.

AllocConsole API will allocate a new console for the calling process, which
Console.WriteLine will be displayed, while FreeConsole() will destroy this
console when you do not want it. Below is the working sample code snippet:

Imports System.Runtime.InteropServices
Public Class Form1
Inherits System.Windows.Forms.Form

Public Class Win32
<DllImport("kernel32.dll")> _
Public Shared Function AllocConsole() As [Boolean]
End Function

<DllImport("kernel32.dll")> _
Public Shared Function FreeConsole() As [Boolean]
End Function
End Class 'Win32

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Win32.AllocConsole()
Console.WriteLine("abc")

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Win32.FreeConsole()
End Sub
End Class

Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top