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:
#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
void SendHTMLHeader(EXTENSION_CONTROL_BLOCK *pECB);
void PrintHtml(EXTENSION_CONTROL_BLOCK* pECB, string s);
double *getMatlabFunction(EXTENSION_CONTROL_BLOCK* pECB);
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;
}
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)
{
PrintHtml(pECB, "<HTML><HEAD></HEAD><BODY><H2>TABLE OF CYCLES</H2><br><hr><H3>Control line</H3>");
Engine *m_pEngine;
m_pEngine = engOpen(NULL);
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>");
}
void SendHTMLHeader(EXTENSION_CONTROL_BLOCK *pEcb)
{
char header[4096];
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();
cout << "Closing MatLab Engine ..." << *newres << endl;
cout << "Program terminating." << endl;
return 0;
}
double *getMatlabFunction()
{
char user_fb;
cout << "MatLab API in C++ Demo" << endl;
cout << "Christopher Dabney" << endl;
cout << "MatLab Course - Short Presentation " << endl;
cout << endl
<< "Setup: Opening MatLab Engine and creating a pointer to it..."
<< endl;
Engine *m_pEngine;
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.