Thread Subject: C++ mex question

Subject: C++ mex question

From: Siva Mettupalayam

Date: 1 Sep, 2008 01:26:02

Message: 1 of 5

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[])
{
    cout << "Test" << endl << flush;
    //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

Subject: C++ mex question

From: Bill August

Date: 2 Sep, 2008 09:25:54

Message: 2 of 5

> 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[])
> {
> cout << "Test" << endl << flush;
> //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:
// show how to open a console window for matlab to handle cout message
// Input:
// none
// Output:
// none

#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[])
{
// redirect
RedirectIOToConsole();
// print
cout<<"cout demo"<<endl;
// wait for kill
cin.get() ;
// kill the console
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 = 500;
#ifdef _DEBUG

void RedirectIOToConsole()
{
int hConHandle;
long lStdHandle;
CONSOLE_SCREEN_BUFFER_INFO coninfo;
FILE *fp;

// allocate a console for this app
AllocConsole();

// set the screen buffer to be big enough to let us scroll text
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &coninfo);
coninfo.dwSize.Y = MAX_CONSOLE_LINES;
SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE),
coninfo.dwSize);

// redirect unbuffered STDOUT to the console
lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE);
hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
fp = _fdopen( hConHandle, "w" );
*stdout = *fp;
setvbuf( stdout, NULL, _IONBF, 0 );

// redirect unbuffered STDIN to the console
lStdHandle = (long)GetStdHandle(STD_INPUT_HANDLE);
hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
fp = _fdopen( hConHandle, "r" );
*stdin = *fp;
setvbuf( stdin, NULL, _IONBF, 0 );

// redirect unbuffered STDERR to the console
lStdHandle = (long)GetStdHandle(STD_ERROR_HANDLE);
hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
fp = _fdopen( hConHandle, "w" );
*stderr = *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 */

Subject: C++ mex question

From: rych

Date: 25 Sep, 2008 14:21:38

Message: 3 of 5

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

Subject: C++ mex question

From: Bill August

Date: 9 Oct, 2008 12:00:47

Message: 4 of 5

> On Sep 2, 10:25 am, 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[])
> > > {
> > >     cout << "Test" << endl << flush;
> > >     //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:
> > //        show how to open a console window for
> matlab to handle cout message
> > // Input:
> > //        none
> > // Output:
> > //        none
> >
> > #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[])
> > {
> >         // redirect
> >         RedirectIOToConsole();
> >         // print
> >         cout<<"cout demo"<<endl;
> >         // wait for kill
> >         cin.get() ;
> >         // kill the console
> >         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 = 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_HAN
> DLE), &coninfo);
> > coninfo.dwSize.Y = MAX_CONSOLE_LINES;
> >
> SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HAN
> DLE),
> > coninfo.dwSize);
> >
> > // redirect unbuffered STDOUT to the console
> > lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE);
> > hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
> > fp = _fdopen( hConHandle, "w" );
> > *stdout = *fp;
> > setvbuf( stdout, NULL, _IONBF, 0 );
> >
> > // redirect unbuffered STDIN to the console
> > lStdHandle = (long)GetStdHandle(STD_INPUT_HANDLE);
> > hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
> > fp = _fdopen( hConHandle, "r" );
> > *stdin = *fp;
> > setvbuf( stdin, NULL, _IONBF, 0 );
> >
> > // redirect unbuffered STDERR to the console
> > lStdHandle = (long)GetStdHandle(STD_ERROR_HANDLE);
> > hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
> > fp = _fdopen( hConHandle, "w" );
> > *stderr = *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,
It is quite easy to solve this probelm.
The idea is to return the handles of the console.
You can visit my blog for details.
beljsl.blogspot.com
Good luck.

Subject: C++ mex question

From: Hui Song

Date: 9 Oct, 2008 13:53:20

Message: 5 of 5

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

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
c mex Siva Mettupalayam 1 Sep, 2008 17:12:36
rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com