468,785 Members | 1,599 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,785 developers. It's quick & easy.

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

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

Jul 9 '07 #1
2 5188
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]
- mv*@spam.guard.caspershouse.com

"Subodh" <su***********@gmail.comwrote in message
news:11**********************@n60g2000hse.googlegr oups.com...
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

Jul 9 '07 #2

"Subodh" <su***********@gmail.comwrote in message
news:11**********************@n60g2000hse.googlegr oups.com...
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.
>

Thanks and Regards,
Subodh

Jul 11 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Seung-Uk Oh | last post: by
3 posts views Thread by Pushkar Pradhan | last post: by
5 posts views Thread by Dream Catcher | last post: by
6 posts views Thread by Steve Lambert | last post: by
17 posts views Thread by =?Utf-8?B?U2hhcm9u?= | last post: by
reply views Thread by Atos | last post: by
reply views Thread by zhoujie | last post: by
2 posts views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.