Calling MFC from mex functions

This is a demonstation on how to call MFC files within a mex function.
267 Downloads
Updated 25 Nov 2011

View License

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:

#ifdef __cplusplus
extern "C"{
#endif

__declspec (dllexport) float mexDialog (float data)
{
CmexDialog dlg;
dlg.m_data = data;
data = dlg.m_data;
return data;
}
#ifdef __cplusplus
}
#endif

This function must also be declared within the mex function, but this time it is declared as an import function:

#ifdef __cplusplus
extern "C"{
#endif

__declspec (dllimport) float mexDialog(float data);

#ifdef __cplusplus
}
#endif

This dll function is called from within the mex function just like any other function:

data2 = mexDialog (data1);

Cite As

Jim Glidewell (2024). Calling MFC from mex functions (https://www.mathworks.com/matlabcentral/fileexchange/33934-calling-mfc-from-mex-functions), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2009a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Categories
Find more on Biotech and Pharmaceutical in Help Center and MATLAB Answers

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes
1.0.0.0