ISAPI and MATLAB engine Help !

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
<span style="border-collapse:collapse; color:#191919; font-family:Arial,Helvetica,sans-serif; font-size:11px <span style="line-height:17px <span style="font-family:Verdana,Arial,sans-serif; font-size:13px; line-height:16px; border-collapse:separate; color:#000000 Hi,<br/>
my question is how can I start MATLAB engine using the ISAPI extension.<br/>
<br/>
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!<br/>
So the following is done:
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; if (m_pEngine == NULL)
{
PrintHtml(pECB, <span style="color:#A31515; "Error: Matlab Engine not opened!");
exit(1);
}
[/code]


<span style="font-family:Verdana,Arial,sans-serif; font-size:13px; line-height:16px There is a code of the program that as a win32 console application works perfectly.
<span style="font-family:Verdana,Arial,sans-serif; font-size:13px; line-height:16px
<div style="color:Black;background-color:White; <pre>
#include <iostream>
#include <span style="color:#A31515; "engine.h"
#pragma comment( lib, <span style="color:#A31515; "Libmx.lib" )
#pragma comment( lib, <span style="color:#A31515; "libmex.lib" )
#pragma comment( lib, <span style="color:#A31515; "libeng.lib" )
<span style="color:Blue; using <span style="color:Blue; namespace std;
#define SIZE 1 <span style="color:Green; // for demo 2
#define SIZE1 10 <span style="color:Green; // for demo 4
#define SIZE2 3
<span style="color:Blue; double *getMatlabFunction();
<span style="color:Blue; int main ()
{
<span style="color:Blue; double *newres;
newres = getMatlabFunction();
<span style="color:Green; /******************************************************************
* 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 << <span style="color:#A31515; "Closing MatLab Engine ..." << *newres << endl;
cout << <span style="color:#A31515; "Program terminating." << endl;

<span style="color:Blue; return 0;
}
<span style="color:Blue; double *getMatlabFunction()
{

<span style="color:Blue; char user_fb;
<span style="color:Green; // head at top, title, etc..
cout << <span style="color:#A31515; "MatLab API in C++ Demo" << endl;
cout << <span style="color:#A31515; "Christopher Dabney" << endl;
cout << <span style="color:#A31515; "MatLab Course - Short Presentation " << endl;
<span style="color:Green; /******************************************************************
* 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
<< <span style="color:#A31515; "Setup: Opening MatLab Engine and creating a pointer to it..."
<< endl;
Engine *m_pEngine;
<span style="color:Green; //m_pEngine = engOpen("setenv DISPLAY puff:0; matlab");
<span style="color:Blue; if (!(m_pEngine = engOpen(NULL)))
{
cout << <span style="color:#A31515; "Error: Matlab Engine not opened!" << endl;
exit(1);
}
<span style="color:Blue; else
{
<span style="color:Blue; double x[SIZE], y[SIZE];
x[0]=10;
y[0]=22;
mxArray *m_X, *m_Y;
m_X=mxCreateDoubleMatrix(1, SIZE, mxREAL);
memcpy((<span style="color:Blue; void *)mxGetPr(m_X), (<span style="color:Blue; void *)x, <span style="color:Blue; sizeof(<span style="color:Blue; double)*SIZE);
engPutVariable(m_pEngine, <span style="color:#A31515; "x", m_X);
m_Y=mxCreateDoubleMatrix(1, SIZE, mxREAL);
memcpy((<span style="color:Blue; void *)mxGetPr(m_Y), (<span style="color:Blue; void *)y, <span style="color:Blue; sizeof(<span style="color:Blue; double)*SIZE);
engPutVariable(m_pEngine, <span style="color:#A31515; "y", m_Y);
<span style="color:Blue; double *newr;
mxArray *result;
engEvalString(m_pEngine, <span style="color:#A31515; "z = x + y;");
result = engGetVariable(m_pEngine,<span style="color:#A31515; "z");
newr = mxGetPr(result);
cout << <span style="color:#A31515; "Matlab Engine Successfully opened." << m_pEngine << endl;
cout << endl << <span style="color:#A31515; "Press any key to continue: ";
cin >> user_fb;
cout << endl << endl;
<span style="color:Blue; return newr;
}

}
[/code]
And there is ISAPI code.

<span style="border-collapse:collapse; color:#191919; font-family:Arial,Helvetica,sans-serif; font-size:11px <span style="line-height:17px <span style="font-family:Verdana,Arial,sans-serif; font-size:13px; line-height:16px; border-collapse:separate; color:#000000
<div style="color:Black;background-color:White; <pre>
<span style="color:Green; // matlabISAPI3.cpp : Defines the initialization routines for the DLL.
<span style="color:Green; //
#include <span style="color:#A31515; "stdafx.h"
#include <span style="color:#A31515; "matlabISAPI3.h"

#include <sstream>
#include <span style="color:#A31515; "httpext.h"
#include <iostream>
#include <span style="color:#A31515; "engine.h"
#pragma comment( lib, <span style="color:#A31515; "Libmx.lib" )
#pragma comment( lib, <span style="color:#A31515; "libmex.lib" )
#pragma comment( lib, <span style="color:#A31515; "libeng.lib" )
<span style="color:Blue; using <span style="color:Blue; namespace std;
#define SIZE 1

#ifdef _DEBUG
#define <span style="color:Blue; new DEBUG_NEW
#undef THIS_FILE
<span style="color:Blue; static <span style="color:Blue; char THIS_FILE[] = __FILE__;
#endif

<span style="color:Green; /////////////////////////////////////////////////////////////////////////////
<span style="color:Green; // Manditory Exported Functions Prototypes
<span style="color:Blue; void SendHTMLHeader(EXTENSION_CONTROL_BLOCK *pECB);
<span style="color:Blue; void PrintHtml(EXTENSION_CONTROL_BLOCK* pECB, string s);
<span style="color:Blue; double *getMatlabFunction(EXTENSION_CONTROL_BLOCK* pECB);
<span style="color:Green; /////////////////////////////////////////////////////////////////////////////
<span style="color:Green; // GetExtensionVersion()
<span style="color:Green; //
<span style="color:Green; // This is the standard implementation of this function. Dont change any
<span style="color:Green; // thing except the description of the DLL ("matlabISAPI DLL") unless you know
<span style="color:Green; // what youre doing
BOOL WINAPI GetExtensionVersion(HSE_VERSION_INFO *pVer)
{
pVer->dwExtensionVersion = MAKELONG(HSE_VERSION_MINOR, HSE_VERSION_MAJOR);
lstrcpyn(pVer->lpszExtensionDesc, <span style="color:#A31515; "MATLABisapi dll", HSE_MAX_EXT_DLL_NAME_LEN);
<span style="color:Blue; return TRUE;
}
<span style="color:Green; /////////////////////////////////////////////////////////////////////////////
<span style="color:Green; // HttpExtensionProc()
<span style="color:Green; //
<span style="color:Green; // This is the main() function for the DLL.
DWORD WINAPI HttpExtensionProc(EXTENSION_CONTROL_BLOCK *pECB)
{
SendHTMLHeader(pECB);

<span style="color:Blue; double *newres;
newres = getMatlabFunction(pECB);
string str;
stringstream ss;
PrintHtml(pECB, <span style="color:#A31515; "<HTML><HEAD></HEAD><BODY><H2>TABLE OF CYCLES</H2><br>< <H3>Control line</H3>");
ss << <span style="color:#A31515; "< < < < < < Error: Matlab Engine not opened!< < < < < < < < "<< *newres << endl;;

ss << <span style="color:#A31515; "</BODY></HTML>";

PrintHtml(pECB, ss.str());
pECB->dwHttpStatusCode = 200;

<span style="color:Blue; return HSE_STATUS_SUCCESS;
}
<span style="color:Blue; double *getMatlabFunction(EXTENSION_CONTROL_BLOCK* pECB)
{
<span style="color:Green; //char user_fb;
<span style="color:Green; // head at top, title, etc..
PrintHtml(pECB, <span style="color:#A31515; "<HTML><HEAD></HEAD><BODY><H2>TABLE OF CYCLES</H2><br>< <H3>Control line</H3>");
<span style="color:Green; /******************************************************************
* 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);
<span style="color:Green; //m_pEngine = engOpen("setenv DISPLAY puff:0; matlab");
<span style="color:Blue; if (m_pEngine == NULL)
{
PrintHtml(pECB, <span style="color:#A31515; "Error: Matlab Engine not opened!");
exit(1);
}
<span style="color:Blue; else
{
<span style="color:Blue; double x[SIZE], y[SIZE];
x[0]=10;
y[0]=22;
mxArray *m_X, *m_Y;
m_X=mxCreateDoubleMatrix(1, SIZE, mxREAL);
memcpy((<span style="color:Blue; void *)mxGetPr(m_X), (<span style="color:Blue; void *)x, <span style="color:Blue; sizeof(<span style="color:Blue; double)*SIZE);
engPutVariable(m_pEngine, <span style="color:#A31515; "x", m_X);
m_Y=mxCreateDoubleMatrix(1, SIZE, mxREAL);
memcpy((<span style="color:Blue; void *)mxGetPr(m_Y), (<span style="color:Blue; void *)y, <span style="color:Blue; sizeof(<span style="color:Blue; double)*SIZE);
engPutVariable(m_pEngine, <span style="color:#A31515; "y", m_Y);
<span style="color:Blue; double *newr;
mxArray *result;
engEvalString(m_pEngine, <span style="color:#A31515; "z = x + y;");
result = engGetVariable(m_pEngine,<span style="color:#A31515; "z");
newr = mxGetPr(result);

<span style="color:Blue; return newr;
}

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


View the full article
 
Back
Top