Here is my C++ code
#include "CppMatlabWrapper.h"
#include <exception>
#include <string>
using namespace std;
//Method to validate the data
extern "C" __declspec(dllexport) validationResult __stdcall
_validateData(double dataToMat[], int time)
{
validationResult res;
res.signalStatusArray[0]=1;
res.signalStatusArray[1]=2;
res.signalStatusArray[2]=3;
res.signalStatusArray[3]=4;
res.micDataValid = (int)(dataToMat[0]*100);
res.rWaveValid = (int)(dataToMat[1]*100);
return res;
}
------------
Here is the header file CppMatlabWrapper.h
//Structure to store the result of the validation operation
struct validationResult
{
int micDataValid; //validity of the microphone data
int rWaveValid; //validity of the r Wave
int signalStatusArray[4]; //validity of each of the channel data
};
extern "C" __declspec(dllexport) validationResult __stdcall
_validateData(double dataToMat[], int time);
This C++ code compiled gives me the library "CppMatlabWrapper.dll"
--------------------------------------------------------------------
Here is the C# code to access this library
using System;
using System.Runtime.InteropServices;
namespace MatlabWrapper
{
class Program
{
//Structure to receive the result of the validation operation
public struct validationResult
{
public int micDataValid; //validity of the microphone data
public int rWaveValid; //validity of the r Wave
public fixed int signalStatusArray[4];
};
[DllImport("CppMatlabWrapper.dll")]
static extern validationResult _validateData(double[] data,
int time);
static void Main(string[] args)
{
//code for creating a double array called dataToMat of
some size
//
validationResult res;
res = _validateData(dataToMat, 2);
}
}
}
THANKS
________
On Apr 18, 5:08 pm, "Willy Denoyette [MVP]"
<willy.denoye...@telenet.bewrote:
"AM" <abhi.me...@gmail.comwrote in message
news:11**********************@n59g2000hsh.googlegr oups.com...
I guess i did not copy my code correctly.
Here is what my C# struct looks like
public struct validationResult
{
public int rWaveValid; //validity of the r Wave
public int[] signalStatusArray; //validity of each of the
//channel data
};
Are you suggesting that the the error is caused by my array definition
in C# is dynamic (because of the []), while my C++ array is of size 4?
Hard to tell, as you did not post the function declaration (C#), also I'm not clear on why
you allocate a managed array in C# while your C function returns a pointer (to an array in
unmanaged memory).
res.signalStatusArray = new int[4]; // WHY????
res = _validateData(dataToMat, 2); //res is returned from C, it's a pointer right???
I would suggest you to post a complete sample that illustrates the issue.
Willy.