|
rych <rychphd@gmail.com> wrote in message <5b850fc6-660d-420f-ba1c-f417c29a3046@m45g2000hsb.googlegroups.com>...
> 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
Hi Igor,
The idea is quite easy. Just return the handles of the console so that when you can find it by the handls next time.
For details you can visit my blog:
beljsl.blogspot.com
|