Question

I have a very simple header file for a dll library but it is in C++. Anyone can help me to edit it in a way to be compatible with "LoadLibrary" command in Matlab (native C)? I realize it is not a general problem but more likely lack of my knowledge. But if the solution is simple, I would appreciate any advice.

// The following ifdef block is the standard way of creating macros which make exporting 
// from a DLL simpler. All files within this DLL are compiled with the TRACKERERRORSDLL_EXPORTS
// symbol defined on the command line. this symbol should not be defined on any project
// that uses this DLL. This way any other project whose source files include this file see 
// TRACKERERRORSDLL_API functions as being imported from a DLL, whereas this DLL sees symbols
// defined with this macro as being exported.
#ifdef TRACKERERRORSDLL_EXPORTS
#define TRACKERERRORSDLL_API __declspec(dllexport)
#define TRACKERERRORSDLL_VB __declspec(dllexport) __stdcall
#else
#define TRACKERERRORSDLL_API __declspec(dllimport)
#define TRACKERERRORSDLL_VB __declspec(dllimport) __stdcall
#endif

#include <string>
using namespace std;

bool TRACKERERRORSDLL_API GetTPIErrorDescription_wstring(long errorNumber, 
                                                 basic_string<__wchar_t> & shortDescription,
                                                 basic_string<__wchar_t> & longDescription,
                                                 basic_string<__wchar_t> & solutionDescription,
                                                 bool & isAutoRecoverOnGreenState);

bool TRACKERERRORSDLL_API GetTPIErrorDescription_wstring(long errorNumber, 
                                                 basic_string<unsigned short> & shortDescription,
                                                 basic_string<unsigned short> & longDescription,
                                                 basic_string<unsigned short> & solutionDescription,
                                                 bool & isAutoRecoverOnGreenState);

bool TRACKERERRORSDLL_API GetTPIErrorDescription_string(long errorNumber, 
                                                 string & shortDescription,
                                                 string & longDescription,
                                                 string & solutionDescription,
                                                 bool & isAutoRecoverOnGreenState);

bool TRACKERERRORSDLL_API GetTPIErrorDescription_CString(long errorNumber, 
                                                 CString & shortDescription,
                                                 CString & longDescription,
                                                 CString & solutionDescription,
                                                 bool & isAutoRecoverOnGreenState);

bool TRACKERERRORSDLL_VB GetTPIErrorDescription_VB(int errorNumber, 
                                                 LPSTR* shortDescription,
                                                 LPSTR* longDescription,
                                                 LPSTR* solutionDescription,
                                                 bool* isAutoRecoverOnGreenState);

Link to download the library (64bit): https://docs.google.com/file/d/0BzzppV2CG8ZldzFRVzJUa252MHc/edit?usp=sharing

Matlab R2013a 64bit

Était-ce utile?

La solution

The only function that you can call is GetTPIErrorDescription_VB. All the others use C++ classes that you cannot access. So I suggest that you do the following:

  1. Remove all the other functions from the header file.
  2. Remove the #include and the using lines.
  3. Remove the #ifdef and replace TRACKERERRORSDLL_VB with __stdcall.
  4. Either include windows.h or add some #define statements for the Win32 types.
  5. Possibly deal with the bool type depending on whether or not MATLAB knows how to deal with it. If MATLAB won't recognise it, replace bool with int.

At that point the call to loadlibrary should work and then you just need to write the code that calls calllib.

The resulting header file might look like something this:

#define LPSTR char*

__declspec(dllimport) bool __stdcall GetTPIErrorDescription_VB(
    int errorNumber, 
    LPSTR* shortDescription,
    LPSTR* longDescription,
    LPSTR* solutionDescription,
    bool* isAutoRecoverOnGreenState
);

Finally, do note that LPSTR* is a rather surprising type to encounter. It suggests that the DLL is going to allocate char* C strings, and then return them to you through the three description parameters. This presents a memory allocation issue. Who is going to deallocate the memory? Does it even need to be deallocated, or is it static? Those issues will need to be resolved by consulting the documentation for the DLL.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top