Console output from MFC dialog based app

Dear All,

I have developed a MFC dialog based application. Now when I execute this app 
from command prompt, I need to write some text back to console.
In console application we achieve this using functions like printf(), 
puts(), etc.

Please let me know how do I achieve same in dialog based application since 
printf() and other functions are not working here.

Thanks,
Ajay
0
4/18/2008 12:59:00 PM
vc.mfc 33608 articles. 0 followers. Follow

5 Replies
868 Views

Similar Articles

[PageSpeed] 43

You can't write to the same console window that you were ran from, but you 
can create a console window and write to that.

Take a look at AllocConsole, ReadConsole, and WriteConsole.

Here is an example:
http://www.codeproject.com/KB/cpp/ConsoleAdapter.aspx


AliR.

"Ajay Shinde" <AjayShinde@discussions.microsoft.com> wrote in message 
news:45748FA7-EEB4-4CFB-948A-7F439E24D53D@microsoft.com...
> Dear All,
>
> I have developed a MFC dialog based application. Now when I execute this 
> app
> from command prompt, I need to write some text back to console.
> In console application we achieve this using functions like printf(),
> puts(), etc.
>
> Please let me know how do I achieve same in dialog based application since
> printf() and other functions are not working here.
>
> Thanks,
> Ajay 


0
AliR3470 (3235)
4/18/2008 2:29:48 PM
>I have developed a MFC dialog based application. Now when I execute this app 
>from command prompt, I need to write some text back to console.
>In console application we achieve this using functions like printf(), 
>puts(), etc.
>
>Please let me know how do I achieve same in dialog based application since 
>printf() and other functions are not working here.

Under XP (and presumably later OS's) you can use AttachConsole like
this:

	AttachConsole( ATTACH_PARENT_PROCESS );

	int hCrt;
	FILE *hf;

	hCrt = _open_osfhandle( (intptr_t)
GetStdHandle(STD_OUTPUT_HANDLE), _O_TEXT );
	hf = _fdopen( hCrt, "w" );
	*stdout = *hf;
	int i = setvbuf( stdout, NULL, _IONBF, 0 ); 

	SetConsoleCtrlHandler( HandlerRoutine, true );

	printf( "Hello World\n" );

Under other OS's I'm not aware of how you can do it other than to
cheat by having 2 separate programs - a console one with a .com
extension and a GUI one with a .exe extension. The .com starts first
and can then conditionally run the .exe version.

Dave
0
davidl7375 (2060)
4/18/2008 2:42:47 PM
A more important question is "why do you think you need to write to a console?"  For
example, for debug tracing, the TRACE macro works exceedingly well, and if you need
something that will be in the final product, something like my Logging ListBox is a better
choice.  Generally, although it is possible to do so, it should generally be considered
bad practice to do so.

So instead of asking how to do something that isn't very good practice, why not ask a
question based on what you are trying to accomplish?
					joe

On Fri, 18 Apr 2008 05:59:00 -0700, Ajay Shinde <AjayShinde@discussions.microsoft.com>
wrote:

>Dear All,
>
>I have developed a MFC dialog based application. Now when I execute this app 
>from command prompt, I need to write some text back to console.
>In console application we achieve this using functions like printf(), 
>puts(), etc.
>
>Please let me know how do I achieve same in dialog based application since 
>printf() and other functions are not working here.
>
>Thanks,
>Ajay
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
0
newcomer (15974)
4/20/2008 5:47:10 PM
Thanks a ton David.
It worked.

"David Lowndes" wrote:

> >I have developed a MFC dialog based application. Now when I execute this app 
> >from command prompt, I need to write some text back to console.
> >In console application we achieve this using functions like printf(), 
> >puts(), etc.
> >
> >Please let me know how do I achieve same in dialog based application since 
> >printf() and other functions are not working here.
> 
> Under XP (and presumably later OS's) you can use AttachConsole like
> this:
> 
> 	AttachConsole( ATTACH_PARENT_PROCESS );
> 
> 	int hCrt;
> 	FILE *hf;
> 
> 	hCrt = _open_osfhandle( (intptr_t)
> GetStdHandle(STD_OUTPUT_HANDLE), _O_TEXT );
> 	hf = _fdopen( hCrt, "w" );
> 	*stdout = *hf;
> 	int i = setvbuf( stdout, NULL, _IONBF, 0 ); 
> 
> 	SetConsoleCtrlHandler( HandlerRoutine, true );
> 
> 	printf( "Hello World\n" );
> 
> Under other OS's I'm not aware of how you can do it other than to
> cheat by having 2 separate programs - a console one with a .com
> extension and a GUI one with a .exe extension. The .com starts first
> and can then conditionally run the .exe version.
> 
> Dave
> 
0
4/22/2008 7:22:01 AM
Thanks AliR for your response.
Solution provided by David solved the issue.

"AliR (VC++ MVP)" wrote:

> You can't write to the same console window that you were ran from, but you 
> can create a console window and write to that.
> 
> Take a look at AllocConsole, ReadConsole, and WriteConsole.
> 
> Here is an example:
> http://www.codeproject.com/KB/cpp/ConsoleAdapter.aspx
> 
> 
> AliR.
> 
> "Ajay Shinde" <AjayShinde@discussions.microsoft.com> wrote in message 
> news:45748FA7-EEB4-4CFB-948A-7F439E24D53D@microsoft.com...
> > Dear All,
> >
> > I have developed a MFC dialog based application. Now when I execute this 
> > app
> > from command prompt, I need to write some text back to console.
> > In console application we achieve this using functions like printf(),
> > puts(), etc.
> >
> > Please let me know how do I achieve same in dialog based application since
> > printf() and other functions are not working here.
> >
> > Thanks,
> > Ajay 
> 
> 
> 
0
4/22/2008 7:23:00 AM
Reply:

Similar Artilces:

sbs 2008 console error
Good day, after the migration we have some problems with sbs 2008 console: we cannon't create the user with console. We can create with manual command. An exception of type 'Type: Microsoft.WindowsServerSolutions.Admin.Common.ADException, AdminCommon, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' has occurred. Timestamp: 03/09/2010 19:21:28 Message: La funzionalità Windows SBS Console non è stata configurata correttamente. Stack: in Microsoft.WindowsServerSolutions.Admin.Common.ProductADConstants.GetRolesDistinguishedName() ...

locale-neutral msinfo32.exe output
if i'm using msinfo32 /report c:\t.txt /categories +ComponentsDisplay command to genrate report and the OS is Windows XP chinees then the report out put is comming english + chines language mixed and because of that reason i'm not able to find few string "OS Name" and "Vesrion" for example. So can you tell me that how can i genrate this report in english only even if the OS is in any Laguage. Here by pasting the report which was genrated on chinese WindosXP OS. ---------------------------------------------- report---------------------------------...

OWA - SSL
Hi I have a problem with OWA, SSL.... can you help me.... let me tell you exactly what I did: I have FE/BE server... both windows 2003 Enterprise sever SP1 and Exchange 2003 SP1 FE: Srv2 192.168.0.2 BE: Srv1 192.168.0.1 The BE server (Srv1) is CA root and DNS : mail A 192.168.0.2 mail MX mail.domain.com srv2 A 192.168.0.2 srv1 A 192.168.0.1 NS: srv1.domain.com CA: certificate Autority: srv1.domain.com mail.domain.com Issued To mail.domain.com Issued By mail.domain.com Exchange mail server url for internal and ext...

Where to delete a modeless dialog member?
Hi, I have a CView . I want to keep a modeless dialog as a member. I create it in the OnCreate() function of my window, where do I delete it though? Here's the pseudocode I'm using right now, since i'm deleting the dialog in the OnDestroy() handler, I get warnings about it in the debug window: CMyWnd : public CView { OnCreate(); OnDestroy(); CMyModelessDlg *m_pMyDlg; }; CMyWnd::OnCreate() { m_pMyDlg = new CMyModelessDlg(...); } CMyWnd::OnDestroy() { delete m_pMyDlg; CView::OnDestroy(); } markww wrote: > Hi, > > I have a CView . I want to k...

mfc: 4 Millions Domains data with Category
Successfull Internet and Direct Marketing products on www.promotionsite.net * NEW * DOMUS Domains Toolkit Fall 2004 - Unique on the Net 4 Millions "Whois" Domains data with Expiration Date and Category*. Ultimate Version (October 2004) - Our best rate starts from US *$149*. A wonderful tool for Internet and Direct Marketing. Available in Basic, Advanced and Full Editions. It contains a domain database with 4 millions *FRESH* October 2004 New records .com, .net, .org. 4 Gigabytes MS Excel data zipped on CD-Roms/Dvd. Compression 3:1. MS Excel or Text tab del...

add criteria to field that is based on named field
Sorry if this posting shows up twice, I think my first post attempt crashed... I think I am missing something fundamental but I do not know what. I don't know what this issue could potentially be called so searching forums has not been helpful. Setup 1.Start new select query, add any table so that query can be run 2.setup the following ----------------|---------------|--------------- |field1 |field2 ----------------|---------------|--------------- field |Expr1: 1+1 |Expr2: [Expr1] Criteria | | ----------------|---------------|---------------...

System Management Console
Can someone tell me if there is a independent "exe" file for system management console for exchange that can be installed on windows xp machine? Thanks, R. Brown Hi R. Brown, check out the following link. It tells you how to install the Sys Mgr onto an XP box, and some of the pre-requisites etc. http://support.microsoft.com/default.aspx?scid=kb;en-us;815529 HTH, Katherine "R. Brown" <rbrown_nospam@rbnetworking.net> wrote in message news:YoU0c.564$rB4.178@bignews6.bellsouth.net... > Can someone tell me if there is a independent "exe" file for syste...

Modeless Dialog in MFC
Hi I have a simple question. In MSDN(for modeles dialog): --------------------------- void CMyDialog::OnMenuShowSimpleDialog() { //m_pSimpleDialog initialized to NULL in the constructor of CMyDialog class m_pSimpleDlg = new CSimpleDlg(); //Check if new succeeded and we got a valid pointer to a dialog object if(m_pSimpleDlg != NULL) { BOOL ret = m_pSimpleDlg->Create(IDD_SIMPLEDIALOG, this); if(!ret) //Create failed. AfxMessageBox(_T("Error creating Dialog")); m_pSimpleDlg->ShowWindow(SW_SHOW); } else { AfxMessageBox(_...

Combo values from query based on form fields
I am setting the values for a combo box in a form(s) via a query that 'filters' the results with criteria based upon the values of other fields on the form. The combo is a field that is bound. However, this is giving all kinds of problems ranging from Access completely crashing to being asked for the parameter values of those criteria fields when closing the form. I have tried making the combo an unbound field and then setting the value of the bound field to that unbound field after update, but that still leads to the same issues. How can I do this? As example - I have a form w...

cannot add modal dialog box to VBA
Hello: I'm trying the add to VBA the dialog box that pops up when an end user clicks the Delete button in the Payables Transaction Entry window in GP 9.0. I am trying to do so by clicking the Ctrl+F11 keys. This is the standard way of adding a modal box to VBA. But, when I next click Shift+F11 to add the Delete button, nothing happens. In other words, GP is adding neither the dialog box nor the Delete button to VBA. I have done this successfully before in prior versions of GP. But, I cannot get this to be successful here. What am I doing wrong? Thanks! childofthe1980s &quo...

Console to Dialog
I'm trying to port a console based app to a dialog based app so that I can have access to gui controls. The console app connects to a remote server and process the data from the server through a dll. It does this by starting a thread which begins a session and handles the incoming data with callback functions. I tried porting by creating a regular dialog app with a button control that would start the main thread of the console app which I created a class for. I took out all references of creating or messaging the console window, so just the session/callback code remained. The dialog crash...

Exchange message console
Hi. I'm sorry to re-post this message, but i've loose the preceding answer. I've got a Windows SBS 2003 Server. Is there a console where I can see all mail (sender, recipients, message contents, ecc) which pass through my Exchange server? Inside and outside? I need to see every message that my employees send and receive. Thanks Hi Roberto Sounds like you need to implement message archival: http://www.msexchange.org/tutorials/MF011.html -- Neil Hobson Exchange MVP For Exchange news, links and tips, check: http://www.msexchangeblog.com "Roberto Sartori" <sa...

Can we Get Table Names from an access database while using MFC provided CRecordset class
Hey Friends Can we Get Table Names from an access database while using MFC provided CRecordset class. vikrant wrote: > Hey Friends > > Can we Get Table Names from an access database while using MFC provided > CRecordset class. > > Yes. The CATALOG sample in MSDN shows how. -- Scott McPhillips [VC++ MVP] vikrant, Yes... You can use access the system tables directly, especially using a CRecordset object connected to a Database object that is associated with the database in question and this SQL statement: SELECT Name FROM MSysObjects WHERE ( Flags=0) AND (Typ...

What files need to be shipped with Visual Studio 2008 Application that uses MFC Feature Pack
What all files (MFC dlls etc) need to be shipped with such an application (as in the subject line)? I understand most of the users won't be having the latest system dlls. So i wanted them to reside besides the application exe. I am using many Feature Pack utilities like Ribbon Interface etc. I found that wiki (http://en.wikipedia.org/wiki/ Microsoft_Foundation_Class_Library) mentions MFC 9.0.30411 (mfc90.dll). How do I identify this? warm regards, Divya Rathore When I build my applications, I prefer to statically link with MFC. Although this makes my EXE image larger, it simplifies di...

average hourly output for date, result shown by day
hello there, i have a template in excel2007 that is blank until the data will begin to be entered, in column A i have the date in the format 14/03/01, in column D i have hours worked (in regular number eg 2.89) and in column M i have output in units. i was wondering if there was a formula that could return the average hourly output and total output in the style below? Average Hourly Output Total Output Monday Tuesday Wednesday Thursday Friday Saturday Sunday i have tried setting up another column that corresponds to the date column A, and trying to get the...

saving sheet as html can I affect output size?
Excel 2002, I have a workbook, I need to save a sheet to a web page. I need an auto filter applied on a row. I go to File, save as web page, select the options as a sheet and add interactivity. Then Save. The resulting output is a small spreadsheet in the html page. About 2 inches of surrounding background space on my 1024x768 display. Due to the amount of data I want to utilize this wasted space by enlarging the spreadsheet. Now if I remove the auto filter and don't select then the add interactivity option the resulting output is a full size spreadsheet that fills the entire html page. ...

mail merge output
I have a problem with a source file in excel for my mail merge. the field in excel has all lines of the address split by a / sign... there are three in each cell e.g. anyhouse/anytown/anycounty/anyzipcode how do i split the data out so i can produce my mailmerge any ideas please See reply to "How do I split addresses(city, state,zip)?" 13 questions ago dated 9/2/2008 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! "pj finsol" wrote: > I have a problem with a source file in excel for my mail merge. > > the field in excel has all lines of the address split by a / sign.....

Using TaskDialogIndirect is Causing an Access Violation in MFC #4
When trying to use TaskDialogIndirect with a callback in MFC, the application performs an access violation in wincore.cpp on line 425. This is using VisualStudio 2005 SP1 with Vista updates. ...

Invalid "Office In Use" Dialog
Hello, I'm a college student and whenever I'm on my college network and I try to run any of the programs in Microsoft Office 2004 for Mac (Student Edition), I get a message that reads as follows: <br><br>"Microsoft Office 2004 cannot start because Microsoft Office is already in use. <br><br>An Office program is being used by &lt;insert name here, it's always different&gt;. Your installation exceeds the number of installations permitted by the license agreement..." <br><br>I know that this cannot be true, because I have a valid c...

Output to console
I am programming a replacement on-screen keyboard. This is a win32 program that needs to send messages to other windows. We use SendInput to send the keyboard messages to the window with the focus. This works for normal windows. It does not, however, work for sending output to a console window. Does anyone know how I can send characters to a console window? Dave ...

Console app with no console window
I'm trying to replace our fairly crappy scheduler with the NT Task Scheduler to run a console app. When our scheduler runs this app, it does so with no console window and the app then closes its standard in/out handles. Our scheduler calls CreateProcess() and sets the flag parameter to DETACHED_PROCESS. The command line for the console app includes a /d switch to tell it to close its IO handles. How would I get the same behaviour when running the console app from the NT Task Scheduler? I've looked at the interface for the task object and can't see anything there that would stop t...

Print spooler sub system App stopped working
Hi, I have a problem with my printer. I am getting a message saying that the spooler subsystem has stopped working and closed. I have tried manually restarting the spooler but I then get a interactive services message, saying that there is a message that cannot be viewed in windows, I click the view message button and I get a blue screen with a black window then I am told the it does not need to show me the message. I am running Vista SP2 64bit AMD athlon 64X2 Dual core processor 6400+ 3.20GHz with 4.00Gb RAM Thanks in advance. Nig Wait for responses but Just FYI: ...

xml/xslt, .net 2.0, whitespace, output to text file
I'm having a problem with line feeds in an xslcompiledtransform in .net 2.0. I'm trying to use xsl to transform xml to a text file that can be read on a Unix box. The problem is that when I insert a line feed, it always results in a carriage-return/linefeed - the Unix box does not like this. I've tried different encoding settings and using hex/dec/html values for line feed but the result is always CRLF as viewed in a hex editor. here's the code that pertains to my issue if anyone can help XSL Transformation code Dim fs As New IO.FileStream(f.FilePathWorkin...

What message when deactivating dialog.
I would like to find out what message id sent when user deactivates a dialog. This is usually the case when the user clicks the mouse button outside the dialog window and the title bar becomes gray (instead blue, as usual). I couldn't find this information in MSDN. Thanks for any hints, Pshemek <przemyslaw.sliwa@gazeta.pl> wrote in message news:1185119389.627510.226450@22g2000hsm.googlegroups.com... >I would like to find out what message id sent when user deactivates a > dialog. This is usually the case when the user clicks the mouse button > outside the dialog window a...

Adding Backup and Security Status to Windwos SBS Console
Backup Status; It is possible to add the same Backup Status notification to a second server? Or maybe a client machine that you are using Windows based backup? Security Status; I would assume that I need to enable the firewall rule for the severs to have the the Security Status change from "Not Available" to OK correct? Or is there a fix for this? I tried to search for threads and couldn't find anything. Kev wrote: > Backup Status; > It is possible to add the same Backup Status notification to a second > server? Or maybe a client ma...