Click here to Skip to main content
15,895,462 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,
my question is how can I start MATLAB engine using the ISAPI extension.

getMatlabFunction work fine if I run program as WIN32 console application, but when I try to run ISAPI extension program was terminated with error; Error: Matlab Engine not opened!
So the following is done:

// matlabISAPI3.cpp : Defines the initialization routines for the DLL.
//
#include "stdafx.h"
#include "matlabISAPI3.h"

#include <sstream>
#include "httpext.h"
#include <iostream>
#include "engine.h"
#pragma comment( lib, "Libmx.lib"    )
#pragma comment( lib, "libmex.lib"   )
#pragma comment( lib, "libeng.lib"   )
using namespace std;
#define SIZE 1

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
//  Manditory Exported Functions Prototypes
void SendHTMLHeader(EXTENSION_CONTROL_BLOCK *pECB);
void PrintHtml(EXTENSION_CONTROL_BLOCK* pECB, string s);
double *getMatlabFunction(EXTENSION_CONTROL_BLOCK* pECB);
/////////////////////////////////////////////////////////////////////////////
//  GetExtensionVersion()
//
//  This is the standard implementation of this function.  Don't change any
//  thing except the description of the DLL ("matlabISAPI DLL") unless you know
//  what you're doing
BOOL WINAPI GetExtensionVersion(HSE_VERSION_INFO *pVer)
{
	pVer->dwExtensionVersion = MAKELONG(HSE_VERSION_MINOR, HSE_VERSION_MAJOR);
	lstrcpyn(pVer->lpszExtensionDesc, "MATLABisapi dll", HSE_MAX_EXT_DLL_NAME_LEN);
	return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
//  HttpExtensionProc()
//
//  This is the 'main()' function for the DLL.
DWORD WINAPI HttpExtensionProc(EXTENSION_CONTROL_BLOCK *pECB)
{
  SendHTMLHeader(pECB);
  
  double *newres;
  newres = getMatlabFunction(pECB);
	string str;
	stringstream ss;
	PrintHtml(pECB, "<HTML><HEAD></HEAD><BODY><H2>TABLE OF CYCLES</H2><br><hr><H3>Control line</H3>");
		ss << "<hr><hr><hr><hr><hr><hr>Error: Matlab Engine not opened!<hr><hr><hr><hr><hr><hr><hr><hr>"<< *newres  << endl;;
	
	ss << "</BODY></HTML>";
		
	PrintHtml(pECB, ss.str());
	pECB->dwHttpStatusCode = 200;
	
return HSE_STATUS_SUCCESS;
}
double *getMatlabFunction(EXTENSION_CONTROL_BLOCK* pECB)
{
	//char user_fb;
	// head at top, title, etc..
	PrintHtml(pECB, "<HTML><HEAD></HEAD><BODY><H2>TABLE OF CYCLES</H2><br><hr><H3>Control line</H3>");
	/******************************************************************
	 * Open the MatLab Engine, and check if the opening was successful.
	 *
	 * m_pEngine is now a matlab pointer to our Engine.  It is how we
	 * will send out commands to MatLab through C++.  As we discussed in
	 * class so often, it is a handle to a copy of MatLab.
	 ******************************************************************/
		
	Engine *m_pEngine;
	m_pEngine = engOpen(NULL);
	//m_pEngine = engOpen("setenv DISPLAY puff:0; matlab");
	if (m_pEngine == NULL)
	{
		PrintHtml(pECB, "Error: Matlab Engine not opened!");
		exit(1);
	}
	else 
	{
		double x[SIZE], y[SIZE];
		x[0]=10;
		y[0]=22;
		mxArray *m_X, *m_Y;
		m_X=mxCreateDoubleMatrix(1, SIZE, mxREAL);
		memcpy((void *)mxGetPr(m_X), (void *)x, sizeof(double)*SIZE);
		engPutVariable(m_pEngine, "x", m_X);
		m_Y=mxCreateDoubleMatrix(1, SIZE, mxREAL);
		memcpy((void *)mxGetPr(m_Y), (void *)y, sizeof(double)*SIZE);
		engPutVariable(m_pEngine, "y", m_Y);
		double *newr;
		mxArray *result;
		engEvalString(m_pEngine, "z = x + y;");
		result = engGetVariable(m_pEngine,"z");
		newr = mxGetPr(result);
		
		return newr;
	}	

	PrintHtml(pECB, "</BODY></HTML>");
}
/////////////////////////////////////////////////////////////////////////////
//  SendHTMLHeader()
//
void SendHTMLHeader(EXTENSION_CONTROL_BLOCK *pEcb)
{
	char header[4096];
	// output header
	strcpy(header, "Content-type: text/html\n\n");
	pEcb->ServerSupportFunction(pEcb->ConnID, HSE_REQ_SEND_RESPONSE_HEADER, NULL, 0, (DWORD *) header);
}
void PrintHtml(EXTENSION_CONTROL_BLOCK* pECB, string s)
{
  char* str = new char[s.length() + 1];
  size_t len = s.length();
  pECB->WriteClient(pECB->ConnID, (LPVOID)s.c_str(), (LPDWORD)&len, 0);
}


There is a code of the program that as a win32 console application works perfectly.

#include <iostream>
#include "engine.h"
#pragma comment( lib, "Libmx.lib"    )
#pragma comment( lib, "libmex.lib"   )
#pragma comment( lib, "libeng.lib"   )
using namespace std;
#define SIZE 1		// for demo 2
#define SIZE1 10	// for demo 4
#define SIZE2 3	
double *getMatlabFunction();
int main ()
{
	double *newres;
	 newres = getMatlabFunction();
	/******************************************************************
	 * Not many tutorials actually talked about closing the engine, 
	 * but I believe that closing the engine actually shuts the 
	 * Matlab prompt down when the program closes.
	 * The parameter, obviously, is the pointer to the Engine.
	 *****************************************************************/
	cout << "Closing MatLab Engine ..." << *newres << endl;
	cout << "Program terminating." << endl;

	return 0;
}
double *getMatlabFunction()
{
	
	char user_fb;
	// head at top, title, etc..
	cout << "MatLab API in C++ Demo" << endl;
	cout << "Christopher Dabney" << endl;
	cout << "MatLab Course - Short Presentation " << endl;
	/******************************************************************
	 * Open the MatLab Engine, and check if the opening was successful.
	 *
	 * m_pEngine is now a matlab pointer to our Engine.  It is how we
	 * will send out commands to MatLab through C++.  As we discussed in
	 * class so often, it is a handle to a copy of MatLab.
	 ******************************************************************/
		cout << endl 
		<< "Setup: Opening MatLab Engine and creating a pointer to it..." 
		<< endl;
	Engine *m_pEngine;
	//m_pEngine = engOpen("setenv DISPLAY puff:0; matlab");
	if (!(m_pEngine = engOpen(NULL)))
	{
		cout << "Error: Matlab Engine not opened!" << endl;
		exit(1);
	}
	else 
	{
		double x[SIZE], y[SIZE];
		x[0]=10;
		y[0]=22;
		mxArray *m_X, *m_Y;
		m_X=mxCreateDoubleMatrix(1, SIZE, mxREAL);
		memcpy((void *)mxGetPr(m_X), (void *)x, sizeof(double)*SIZE);
		engPutVariable(m_pEngine, "x", m_X);
		m_Y=mxCreateDoubleMatrix(1, SIZE, mxREAL);
		memcpy((void *)mxGetPr(m_Y), (void *)y, sizeof(double)*SIZE);
		engPutVariable(m_pEngine, "y", m_Y);
		double *newr;
		mxArray *result;
		engEvalString(m_pEngine, "z = x + y;");
		result = engGetVariable(m_pEngine,"z");
		newr = mxGetPr(result);
		cout << "Matlab Engine Successfully opened." << m_pEngine << endl;
	cout << endl << "Press any key to continue: ";
	cin >> user_fb;
	cout << endl << endl;
		return newr;
	}	
	
}


if somebody knows how to run as ISAPI extension please explain to me and write an update of first post code.
Posted
Updated 18-May-11 8:49am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900