1

I currently have a managed C++ class with a method that looks like this...

int Calculate(double price, double quantity)

I can call this method from my C# library like this...

MyLib.Calculate(1,1)

However I now want to pass in an array of structs that are defined in my C++ library instead of price and quantity primitives.

typedef struct my_prices {          
    double quantity;                
    double price;       
} 

So my C++ method signature then changes to this...

int Calculate(my_prices prices[])

What I'm struggling with now is how to call this managed C++ method and pass it the array of prices from C#. I can't seem to create this struct in C#, I've tried defining a C# version of it but am at a loss as to how I proxy it to the C++ version.

I hope this makes sense, I'm a C# developer with very little C++ experience so may be talking rubbish.

Gavin
  • 17,053
  • 19
  • 64
  • 110
  • Thanks. I'm not in control of the managed C++ library. I need to find a way from C# to start passing this struct in. – Gavin Aug 22 '17 at 13:50
  • @Fildor This is managed C++, isn't that for unmanaged? – Gavin Aug 22 '17 at 13:53
  • Dah, just realized, too. Thought it was that link that I had found, when I searched for the exact same ... but it wasn't. – Fildor Aug 22 '17 at 13:55
  • 1
    @Gavin a [mcve] would probably help for getting meaningful answers. –  Aug 22 '17 at 13:55
  • Just found this question, maybe it can give you a hint: https://stackoverflow.com/q/26715366/982149 – Fildor Aug 22 '17 at 13:59

1 Answers1

0

Doing this in C++/CLI is possible but you need a ref class or value type.

value struct my_prices {          
    double quantity;                
    double price;       
} 

int Calculate(array<my_prices> ^prices)
{
   for each (my_prices p in prices)
   {
...
xMRi
  • 14,982
  • 3
  • 26
  • 59