[英]Passing pointer argument in MATLAB to a C-DLL function foo(char**)
I am writing a C-DLL to be called from MATLAB. 我正在编写一个从MATLAB调用的C-DLL。 Is it possible to call a function with
const char **
parameter? 是否可以使用
const char **
参数调用函数? eg 例如
void myGetVersion( const char ** );
The C code would be: C代码是:
const char *version=0;
myGetVersion( &version );
What would be corresponding MATLAB-Code (if it is possible at all)? 什么是相应的MATLAB-Code(如果可能的话)?
Thank you very much! 非常感谢你!
PS: I think this is the help page , but I couldn't find my case :-( PS:我认为这是帮助页面 ,但我找不到我的情况:-(
The answer is to build a pointer to a c-string using the LIBPOINTER function as @JonasHeidelberg suggested. 答案是使用LIBPOINTER函数构建一个指向c-string的指针,如@JonasHeidelberg所建议的那样。 Let me expand his solution with a working example..
让我用一个工作实例扩展他的解决方案..
First lets build a simple DLL: 首先让我们构建一个简单的DLL:
#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
#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, ..). 您可以使用首选的编译器来构建DLL库(Visual Studio,MinGW GCC,..)。 I am using MinGW to compile the above, here is the makefile I am using:
我正在使用MinGW编译上面的代码,这是我正在使用的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: 在我们转向MATLAB之前,让我们用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: 现在一切正常,这里是如何使用MATLAB中的这个库:
%# 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
Looking at Working with pointers , section Passing an Array of Strings in the MATLAB documentation (linked from the help page you linked in your question), it seems you need to construct a libpointer object in MATLAB, something like 看看使用指针 ,在MATLAB文档中传递一个字符串数组 (从您在问题中链接的帮助页面链接),似乎您需要在MATLAB中构建一个libpointer对象 ,类似于
version = libpointer('stringPtrPtr',{''}); %# corrected according to Amro's comment
calllib('yourCdll', 'myGetVersion', version)
(I don't have a DLL to test this with right now, and I'm not very firm on C pointers, so no guarantee... hope this is a step in the right direction) (我现在没有DLL来测试这个,而且我对C指针也不是很坚定,所以不能保证......希望这是朝着正确方向迈出的一步)
I could not get @Amro's solution to work (old version of MATLAB?). 我无法得到@ Amro的解决方案(旧版本的MATLAB?)。 So, I had to do something a little more creative:
所以,我必须做一些更有创意的事情:
pstr = libpointer('voidPtr');
ret = calllib('libversion', 'myGetVersion', pstr);
% establish the length
n = 0;
while n==0 || pstr.Value(n) ~= 0
n=n+1;
pstr.setdatatype('uint8Ptr', 1, n);
end
% truncate to exclude the NULL character
pstr.setdatatype('uint8Ptr', 1, n-1);
% convert to string
display(char(pstr.Value))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.