我有一个用托管Visual C ++(Visual Studio 2010)编写的项目。为了支持以 .mat 格式创建文件,我使用了两个库“libmat.dll”和“libmx.dll”以及相应的标题“mat.h”和“matrix.h”。安装Matlab后,这些库位于Matlab目录中(我使用Matlab R2010a)。
示例:
/* content of mat.h */
...
typedef struct MatFile_tag MATFile;
typedef MATFile* (*matOpen)(const char *, const char *);
...
private: System::Void matToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e)
{
MATFile *pmat;
mxArray *pa2;
double data[2] = {0.0, 0.4};
matOpen openFunc;
HINSTANCE hLib = LoadLibrary(L"libmat.dll"); // not equal to NULL
if (hLib==NULL)
{
//some action;
}
openFunc = (matOpen) GetProcAddress((HMODULE) hLib, "matOpen"); // not equal to NULL
if (openFunc == NULL)
{
FreeLibrary((HMODULE) hLib);
}
HINSTANCE mxLib = LoadLibrary(L"libmx.dll"); // not equal to NULL
if (mxLib==NULL)
{
//some action
}
pmat = openFunc("aaa.mat", "w");
...
}
项目编译成功,但在运行时调用 openFunc 命令会导致下一个错误:
An unhandled exception of type 'System.AccessViolationException' occurred in System.Windows.Forms.dll
Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
此外,我创建了类似的项目来处理上面提到的DLL,但对于非托管C ++,它可以正常工作。看起来问题仅存在于托管C ++。
任何输入都将不胜感激。