Clear Filters
Clear Filters

Pass struct to C++ DLL library with strings

5 views (last 30 days)
Doctor G
Doctor G on 18 Jul 2018
Answered: Doctor G on 18 Jul 2018
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";
}

Answers (1)

Doctor G
Doctor G on 18 Jul 2018
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.

Categories

Find more on Structures in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!