473,513 Members | 2,661 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5606
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3412
by: Seung-Uk Oh | last post by:
Hi, everyone! We are developing an application using both C and C++. We have defined a structure in a C program as follows: typedef struct node { struct node *next; int value; }List;
3
2855
by: Pushkar Pradhan | last post by:
I need to pass the STL linked list to a function, this function should modify the linked list. So do I need to pass it by address, this is how I do it: void qhull(PARTICLE S, int len,...
5
859
by: Dream Catcher | last post by:
1. I don't know once the node is located, how to return that node. Should I return pointer to that node or should I return the struct of that node. 2. Also how to do the fn call in main for that...
10
15100
by: Kent | last post by:
Hi! I want to store data (of enemys in a game) as a linked list, each node will look something like the following: struct node { double x,y; // x and y position coordinates struct enemy...
6
4582
by: Steve Lambert | last post by:
Hi, I've knocked up a number of small routines to create and manipulate a linked list of any structure. If anyone could take a look at this code and give me their opinion and details of any...
0
980
by: mephisto83 | last post by:
Hello, I need to use an unmanaged dll written in c in a program written in c#. This dll uses linked list and I use PInvoke in my c# code. Dll code: typedef struct payload{ char name; int...
17
7211
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
0
8611
by: Atos | last post by:
SINGLE-LINKED LIST Let's start with the simplest kind of linked list : the single-linked list which only has one link per node. That node except from the data it contains, which might be...
7
5756
by: QiongZ | last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it...
0
7157
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7379
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7098
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7521
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5682
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5084
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1591
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.