The answer is to build a pointer to a c-string using the LIBPOINTER function as @JonasHeidelberg suggested. Let me expand his solution with a working example..
First lets build a simple DLL:
version.h
#ifndef VERSION_H
#define VERSION_H
#ifdef __cplusplus
extern "C" {
#endif
#ifdef _WIN32
# ifdef BUILDING_DLL
# define DLL_IMPORT_EXPORT __declspec(dllexport)
# else
# define DLL_IMPORT_EXPORT __declspec(dllimport)
# endif
#else
# define DLL_IMPORT_EXPORT
#endif
DLL_IMPORT_EXPORT void myGetVersion(char**str);
#ifdef __cplusplus
}
#endif
#endif
version.c
#include "version.h"
#include <string.h>
DLL_IMPORT_EXPORT void myGetVersion(char **str)
{
*str = strdup("1.0.0");
}
You can use your preferred compiler to build the DLL library (Visual Studio, MinGW GCC, ..). I am using MinGW to compile the above, here is the makefile I am using:
Makefile
CC = gcc
all: main
libversion.dll: version.c
$(CC) -DBUILDING_DLL version.c -I. -shared -o libversion.dll
main: libversion.dll main.c
$(CC) main.c -o main -L. -lversion
clean:
rm -rf *.o *.dll *.exe
Before we move to MATLAB, lets test the library with a C program:
main.c
#include <stdio.h>
#include <stdlib.h>
#include "version.h"
int main()
{
char *str = NULL;
myGetVersion(&str);
printf("%s\n", str);
free(str);
return 0;
}
Now that all is working, here is how to use this library from MATLAB:
testDLL.m
%# load DLL and check exported functions
loadlibrary libversion.dll version.h
assert( libisloaded('libversion') )
libfunctions libversion -full
%# pass c-string by reference
pstr = libpointer('stringPtrPtr',{''}); %# we use a cellarray of strings
get(pstr)
calllib('libversion','myGetVersion',pstr)
dllVersion = pstr.Value{1}
%# unload DLL
unloadlibrary libversion
The output with the string returned:
Functions in library libversion:
stringPtrPtr myGetVersion(stringPtrPtr)
Value: {''}
DataType: 'stringPtrPtr'
dllVersion =
1.0.0