Path: news.mathworks.com!newsfeed-00.mathworks.com!nlpi057.nbdc.sbc.com!prodigy.net!border1.nntp.dca.giganews.com!nntp.giganews.com!postnews.google.com!m45g2000hsb.googlegroups.com!not-for-mail
From: rych <rychphd@gmail.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: C++ mex question
Date: Thu, 25 Sep 2008 07:21:38 -0700 (PDT)
Organization: http://groups.google.com
Lines: 157
Message-ID: <5b850fc6-660d-420f-ba1c-f417c29a3046@m45g2000hsb.googlegroups.com>
References: <g9fgba$bs0$1@fred.mathworks.com> <8389190.1220347586004.JavaMail.jakarta@nitrogen.mathforum.org>
NNTP-Posting-Host: 144.124.16.33
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
X-Trace: posting.google.com 1222352498 25362 127.0.0.1 (25 Sep 2008 14:21:38 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Thu, 25 Sep 2008 14:21:38 +0000 (UTC)
Complaints-To: groups-abuse@google.com
Injection-Info: m45g2000hsb.googlegroups.com; posting-host=144.124.16.33; 
	posting-account=q0YHcwoAAADbDc1UOEbilYdUiU-iCJDX
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.1) 
	Gecko/2008070208 Firefox/3.0.1,gzip(gfe),gzip(gfe)
X-HTTP-Via: 1.1 towy.aber.ac.uk:8082 (squid/2.7.STABLE3)
Bytes: 5709
Xref: news.mathworks.com comp.soft-sys.matlab:491987


On Sep 2, 10:25=A0am, Bill August <hui.s...@beds.ac.uk> wrote:
> > Hi,
>
> > I am using Matlab R2008a 64 bit and MSVC++ 9.0
> > (Visual
> > Studio 2008) on Windows Vista 64 bit. I compile the
> > following mex function
>
> > #include "mex.h"
> > #include <iostream>
> > using namespace std;
>
> > extern "C"
> > void mexFunction(int nlhs,mxArray *plhs[],int
> > nrhs,const
> > mxArray *prhs[])
> > {
> > =A0 =A0 cout << "Test" << endl << flush;
> > =A0 =A0 //printf("Test\n");
> > }
>
> > The cout doesn't result in anything being displayed.
>
> > Section 26 of the tech note
> >http://www.mathworks.com/support/tech-notes/1600/1605.
> > html
>
> > does say "Using cout will not work as expected in C++
> > MEX-
> > files. This is because cout is expecting to use a
> > display
> > that is not MATLAB. To workaround this problem, use
> > mexPrintf instead.". However,
> > 1. This mex function works with Matlab R14 and MSVC++
> > 7.1
> > (Visual Studio 2003) on Windows XP.
> > 2. I am using some C++ template libraries that use
> > cout. So
> > I cannot change them to mexPrintf.
> > 3. The commented printf call works. Aren't cout and
> > printf
> > using the same output device?
>
> > Thank you for your help.
>
> > Siva
>
> Hi Siva,
> My solution is to open a new console window for cout. It may help you to =
solve the problem.
> Regards.
>
> ------file coutdemo.cpp-----
> // Usage:
> // =A0 =A0 =A0 =A0show how to open a console window for matlab to handle =
cout message
> // Input:
> // =A0 =A0 =A0 =A0none
> // Output:
> // =A0 =A0 =A0 =A0none
>
> #include "mex.h"
> #include <windows.h>
> #include <iostream>
> #include "guicon.h"
> using namespace std ;
>
> void mexFunction(int nlhs, mxArray *phs[], int nrhs, const mxArray *prhs[=
])
> {
> =A0 =A0 =A0 =A0 // redirect
> =A0 =A0 =A0 =A0 RedirectIOToConsole();
> =A0 =A0 =A0 =A0 // print
> =A0 =A0 =A0 =A0 cout<<"cout demo"<<endl;
> =A0 =A0 =A0 =A0 // wait for kill
> =A0 =A0 =A0 =A0 cin.get() ;
> =A0 =A0 =A0 =A0 // kill the console
> =A0 =A0 =A0 =A0 FreeConsole() ;
>
> }
>
> -----file guicon.h------
> #ifndef __GUICON_H__
> #define __GUICON_H__
> #ifdef _DEBUG
>
> void RedirectIOToConsole();
>
> #endif
> #endif
> /* End of File */
>
> -----file guicon.cpp------
> #include <windows.h>
> #include <stdio.h>
> #include <fcntl.h>
> #include <io.h>
> #include <iostream>
> #include <fstream>
> #ifndef _USE_OLD_IOSTREAMS
> using namespace std;
> #endif
>
> // maximum mumber of lines the output console should have
> static const WORD MAX_CONSOLE_LINES =3D 500;
> #ifdef _DEBUG
>
> void RedirectIOToConsole()
> {
> int hConHandle;
> long lStdHandle;
> CONSOLE_SCREEN_BUFFER_INFO coninfo;
> FILE *fp;
>
> // allocate a console for this appAllocConsole();
>
> // set the screen buffer to be big enough to let us scroll text
> GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &coninfo);
> coninfo.dwSize.Y =3D MAX_CONSOLE_LINES;
> SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE),
> coninfo.dwSize);
>
> // redirect unbuffered STDOUT to the console
> lStdHandle =3D (long)GetStdHandle(STD_OUTPUT_HANDLE);
> hConHandle =3D _open_osfhandle(lStdHandle, _O_TEXT);
> fp =3D _fdopen( hConHandle, "w" );
> *stdout =3D *fp;
> setvbuf( stdout, NULL, _IONBF, 0 );
>
> // redirect unbuffered STDIN to the console
> lStdHandle =3D (long)GetStdHandle(STD_INPUT_HANDLE);
> hConHandle =3D _open_osfhandle(lStdHandle, _O_TEXT);
> fp =3D _fdopen( hConHandle, "r" );
> *stdin =3D *fp;
> setvbuf( stdin, NULL, _IONBF, 0 );
>
> // redirect unbuffered STDERR to the console
> lStdHandle =3D (long)GetStdHandle(STD_ERROR_HANDLE);
> hConHandle =3D _open_osfhandle(lStdHandle, _O_TEXT);
> fp =3D _fdopen( hConHandle, "w" );
> *stderr =3D *fp;
> setvbuf( stderr, NULL, _IONBF, 0 );
>
> // make cout, wcout, cin, wcin, wcerr, cerr, wclog and clog
> // point to console as well
> ios::sync_with_stdio();}
>
> #endif
> /* End of File */

Bill, what do you with the console after the mexFunction returns? If I
call the mex file again, the call to AllocConsole fails ("access
denied" and no GetStdHandle) and no output to the existing console is
possible, unfortunately, I don't understand why. So, I free the
console first and allocate a new one. The questions is how to reuse
the console between different mexFunction invocations?
Igor