Pass struct to C++ DLL library with strings

조회 수: 3 (최근 30일)
Doctor G
Doctor G 2018년 7월 18일
답변: Doctor G 2018년 7월 18일
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
Albert Fan
Albert Fan 2018년 7월 18일
How did you defined you sm variable?

댓글을 달려면 로그인하십시오.

답변 (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에서 Structures에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by