Pass struct to C++ DLL library with strings

6 次查看(过去 30 天)
I have a simple C++ program, where I pass a c_struct by reference and am able to update the values for the doubles. Now I would like to also pass back a string. But it fails on the construction:
---
>> sc = libstruct('c_struct', sm)
Error using feval
Cannot convert data value for field p3 due to error:
Parameter can not be converted to a character vector
Error in libstruct (line 16)
ptr=feval(['lib.' structtype],initialvalue);
----
How do I pass back a string in the struct. Here is the Header and the Code:
#pragma once
#include "helper.hpp"
struct c_struct {
double p1;
double p2;
char* p3;
};
#ifdef __cplusplus
extern "C" {
#endif
EXPORTED_FUNCTION int sq(int x);
EXPORTED_FUNCTION void lmb(struct c_struct *st);
#ifdef __cplusplus
}
#endif
----
#include "stdafx.h"
#include "helper.hpp"
#include "simple.hpp"
int sq(int x)
{
return (x*x);
}
void lmb(struct c_struct *st) {
st->p1 = 3.0;
st->p2 = 4.0;
st->p3 = "goodbye";
}

回答(1 个)

Doctor G
Doctor G 2018-7-18
as a struct.
e.g. sm.p1 = 3 sm.p2 = 5 sm.p3 = "foobar"
But anyway, I might have to give this approach up, since I need a C++ class that has two methods. One to open up a TCP connection (nanomsg) and the other to return the next packet.
I am thinking of re-doing this in C#, since it has much better API support with Matlab.

类别

Help CenterFile Exchange 中查找有关 Data Exchange and Mapping with C++ Applications 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by