Passing a Linked List from C++ DLL to C# PInvoke

S

Subodh

Hi All,

I want to use a C++ API in a static lib that returns a linked List in
C#
I am planning to P/Invoke to perform the Interop, I would like to know
which way will be better to interop a Linked list to C# in terms of
performance, I have came across following methods

I am creating a flat DLL to export the static lib functionality.

1. From the C++ DLL write the linked list structures in an xml file
and pass the path of the file to C# app which will
read data from this file

2. Use callback function
-pass a pointer to C# function(delegate) from C# to the exported C++
DLL function
-in the C++ DLL perform callback to this C# function with a pointer to
structure, for each structure in the linked list
-The C# callback function will then marshal the structure using
"PtrToStructure" and put this in required collection
----> With this approach i am worried about performancee if the linked
list contains thousands of nodes

3. Creating a Vector of structure from Linked list in C++ DLL and
passing this to C# function
--> Here i dont know how to marshal a vector in C#

I would like to know which is the better way to transfer a Linked list
to C#, if there is any other approach please let me know...


Thanks and Regards,
Subodh
 
N

Nicholas Paldino [.NET/C# MVP]

Subodh,

#1 is going to be the least performant. #2 could work, but if you have
thousands of nodes, as you say, there will be tremendous overhead. #3 isn't
going to work, because you can't marshal C++ classes to .NET.

Why not just create a managed wrapper for your unmanaged code using the
CLI extensions for C++, and then just call the managed classes you expose
instead?
 
S

Subodh

Subodh,

#1 is going to be the least performant. #2 could work, but if you have
thousands of nodes, as you say, there will be tremendous overhead. #3 isn't
going to work, because you can't marshal C++ classes to .NET.

Why not just create a managed wrapper for your unmanaged code using the
CLI extensions for C++, and then just call the managed classes you expose
instead?

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


I want to use a C++ API in a static lib that returns a linked List in
C#
I am planning to P/Invoke to perform the Interop, I would like to know
which way will be better to interop a Linked list to C# in terms of
performance, I have came across following methods
I am creating a flat DLL to export the static lib functionality.
1. From the C++ DLL write the linked list structures in an xml file
and pass the path of the file to C# app which will
read data from this file
2. Use callback function
-pass a pointer to C# function(delegate) from C# to the exported C++
DLL function
-in the C++ DLL perform callback to this C# function with a pointer to
structure, for each structure in the linked list
-The C# callback function will then marshal the structure using
"PtrToStructure" and put this in required collection
----> With this approach i am worried about performancee if the linked
list contains thousands of nodes
3. Creating a Vector of structure from Linked list in C++ DLL and
passing this to C# function
--> Here i dont know how to marshal a vector in C#
I would like to know which is the better way to transfer a Linked list
to C#, if there is any other approach please let me know...
Thanks and Regards,
Subodh

Hi Nicholas,

Thanks a lot for the reply, I actually would prefer to use the CLI way
to Wrap my C+++ code, I am getting some error in that approach

I linked all my static libs compiled in VS2005 to .Net C++ Application
(Wrapper CLI project)
Then I created a class with static functions to expose the C++ static
lib functionality to managed world
The problem i am facing here is I get following exception when i call
any C++ static lib function from this managed class;


I get following error:
---------------------------
Microsoft Visual C++ Debug Library
---------------------------
Debug Assertion Failed!

Program: ...
File: dbgheap.c
Line: 1473

Expression: _CrtIsValidHeapPointer(pUserData)

For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.

(Press Retry to debug the application)
 
B

Ben Voigt [C++ MVP]

Subodh said:
Hi All,

I want to use a C++ API in a static lib that returns a linked List in
C#
I am planning to P/Invoke to perform the Interop, I would like to know
which way will be better to interop a Linked list to C# in terms of
performance, I have came across following methods

I am creating a flat DLL to export the static lib functionality.

1. From the C++ DLL write the linked list structures in an xml file
and pass the path of the file to C# app which will
read data from this file

2. Use callback function
-pass a pointer to C# function(delegate) from C# to the exported C++
DLL function
-in the C++ DLL perform callback to this C# function with a pointer to
structure, for each structure in the linked list
-The C# callback function will then marshal the structure using
"PtrToStructure" and put this in required collection
----> With this approach i am worried about performancee if the linked
list contains thousands of nodes

3. Creating a Vector of structure from Linked list in C++ DLL and
passing this to C# function
--> Here i dont know how to marshal a vector in C#

I would like to know which is the better way to transfer a Linked list
to C#, if there is any other approach please let me know...

In C++, unwind the linked list to a flat array, and marshal that. This
looks almost the same as #3 (copying into a vector), but the C# code will
need to preallocate the array to a sufficient size. You could return
results in batches if the caller's array runs out before the end of the
list.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top