Calling MFC from mex functions (Scripts) Publisher's description
from Jim Glidewell
I recently had a requirement to make MFC calls within a mex function
I recently had a requirement to make MFC calls within a mex function. After looking around I was unable to find a single example, so I had to develop my own. Hopefully this will make the job easier for you. The procedure is actually pretty easy once you know how. This discussion assumes that you already know how to write mex functions and are famliar with MFC.
This simple demo creates a mex function called "mexDemo", which passes a single number as an argument. This data is then displayed in a dialog box. The user can modify the number and then hit "OK". This new value is then returned to MATLAB.
First, in order to process MFC messages you need a CWinApp class. Because both the mex Function and the CWinApp class require their own dll entry point, I couldn't find a way to put them both in the same file, so I had to create two separate dll's, the mex function and the actual MFC dll.
The demo contains three files. "mexDemo" defines the mex funtion. This simply takes the value passed in, calls the dialog, and then returns the new value.
The file "mexDialogDemo" defines the CWinApp class. To create this class simply create a new project by selecting "MFC AppWizard (dll). You can choose either static or dynamic linking. I choose static so that the MFC code will be self-contained.
The file "mexDialog" defines the dialog and is the most important part since this provides the interface to the mex function. This dialog was added to the mexDialogDemo program like any other dialog would be.
Access to the dialog is provided through an export function call "mexDialog", which in turn brings up the dialog. This intermediate function is required because the mex function cannot directly call the dialog.
This export function must be declared as a C callable dllexport function:
__declspec (dllexport) float mexDialog (float data)
dlg.m_data = data;
data = dlg.m_data;
This function must also be declared within the mex function, but this time it is declared as an import function:
__declspec (dllimport) float mexDialog(float data);
This dll function is called from within the mex function just like any other function:
data2 = mexDialog (data1);
System Requirements:MATLAB 7.8 (R2009a)
Program Release Status: New Release
Program Install Support: Install and Uninstall