-1

I need to create a list (array) from .net which consists of about 50 000 elements, pass it to c++ dll, operate on it and return a list (array) from c++ to .net.

  1. First option which comes to my mind, is to create a struct on both sides. Return an array of structs from .net to c++.

Here are my concerns:

a) if in a struct, it consists non reference types like: ints, doubles, etc will an array of structs with its values will be stored on stack?

Is there a limit when creating array of struct? Is it efficient?

Is it an efficient way to initialize such a large array on .net side?

  1. Do you have any sample which shows how to pass references to objects, etc? Without COM, interoperability, etc?

Generally I seek an advise how to perform efficiently the following things:

1) Fetch data from db 2) Allocate it in a structure which I could efficiently pass to a dll c++ win32 library 3) Perform operations on c++ side, then return an array back to .net

I also need an advise, on which side the objects should be allocated/deallocated in terms of performing the above operations..

Thanks for help in advance

P.S. I also don't understand the info that making an array public in a class, makes a whole copy every time I access it ... Could someone explain that to me?

John
  • 1,834
  • 5
  • 32
  • 60
  • 1
    I think you're overthinking this. The simplest, most efficient way is to pass C++ the buffer and the number of items, and let C++ work on that buffer. If you want samples, the Windows API has many functions that require the user to pass a buffer and length. – PaulMcKenzie May 09 '15 at 13:06
  • Do you have any example of How I could prepare the buffer with array of structs on the c# side? – John May 09 '15 at 16:02
  • 1
    I'm sure there are plenty of examples showing how to create a struct that is compatible with both C# and C++ in terms of alignment, padding, etc. Just create an array of those, and pass it to the C++ DLL, along with the number of items in the array -- basically no different than if you were sending an array of int's or double's. See here: http://stackoverflow.com/questions/10838825/convert-c-struct-to-c-sharp – PaulMcKenzie May 09 '15 at 16:11
  • You were helpfull. You can answer to that question so that I could mark as answered. Thanks – John May 10 '15 at 11:39

1 Answers1

1

I think the way to do this is the simplest, and that is to pass the buffer to the C++ DLL, along with a length argument denoting the number of items. This way, the client is responsible for creating the buffer and not the DLL. The DLL is no longer responsible for creation or deletion of the buffer, just the manipulation of the buffer.

This is the way that most, if not all Windows API functions accomplish this. Most of those API functions deal with character buffers, but the same principle applies.

For structs, here is a link describing how to define your C# struct to be compatible with a C++ (or C) struct:

Convert C++ struct to C#

Community
  • 1
  • 1
PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45