我有一个用托管 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++ 中。
任何输入将不胜感激。