473,385 Members | 1,944 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Passing structures between C# and C?

Hi,

I've never got an answer to this question in any group till now....
How do we pass a array of structures between a C# and a C DLL, I am using
RCW interop?

The C# Library gets an array of structures from a webservice and it has to
pass these values to the C DLL so that the C DLL could parse it and consume
1 structure at a time?
Help!
Nov 16 '05 #1
6 1858
The runtime does not support this, so you have to do it yourself using
Custom marshaling.

Willy.

"Germic" <sy**********@yahoo.com> wrote in message
news:uc**************@tk2msftngp13.phx.gbl...
Hi,

I've never got an answer to this question in any group till now....
How do we pass a array of structures between a C# and a C DLL, I am using
RCW interop?

The C# Library gets an array of structures from a webservice and it has to
pass these values to the C DLL so that the C DLL could parse it and
consume
1 structure at a time?
Help!

Nov 16 '05 #2
Hi Willy,

Thanks for your reply.. could you pls shed some more light on how to do this?

Regards.

"Willy Denoyette [MVP]" wrote:
The runtime does not support this, so you have to do it yourself using
Custom marshaling.

Willy.

Nov 16 '05 #3
Sure, just post a structure you want to marshal.

Willy.

"GeRmIc" <Ge****@discussions.microsoft.com> wrote in message
news:B8**********************************@microsof t.com...
Hi Willy,

Thanks for your reply.. could you pls shed some more light on how to do
this?

Regards.

"Willy Denoyette [MVP]" wrote:
The runtime does not support this, so you have to do it yourself using
Custom marshaling.

Willy.

Nov 16 '05 #4
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct L_Germplasm
{
public long gdate;
public long gid;
public long glocn; ....etc., around 9 long fields
}

I have to pass an array of such structures to my C program in one hit
(instance).

OR

How do can I hold that array of structures in C# Library itself and then
pass one such structure each time the C DLL contacts the C# Library? How do I
hold and pass the address of the ArrayList or HashTable between the C DLL and
C# Library?

Any suggestions/help to any of the above two questions would be greatly
appreciated.
Nov 16 '05 #5
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct L_Germplasm
{
public long gdate;
public long gid;
public long glocn; ....etc., around 9 long fields
}

I have to pass an array of such structures to my C program in one hit
(instance).

OR

How do can I hold that array of structures in C# Library itself and then
pass one such structure each time the C DLL contacts the C# Library? How do I
hold and pass the address of the ArrayList or HashTable between the C DLL and
C# Library?

Any suggestions/help to any of the above two questions would be greatly
appreciated.
Nov 16 '05 #6

"GeRmIc" <Ge****@discussions.microsoft.com> wrote in message
news:5C**********************************@microsof t.com...
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct L_Germplasm
{
public long gdate;
public long gid;
public long glocn; ....etc., around 9 long fields
}

I have to pass an array of such structures to my C program in one hit
(instance).

Ok, there are several way's to marshal such simple array of struct .
Here's one of them:

//C# prog.
[StructLayout(LayoutKind.Sequential)]
struct S
{
internal int a;
internal int b;
internal int c;
internal int d;
}

class Tester
{
[DllImport("nativeStrucArr.dll")]
private static extern void func_c( S[] ao, int len);

static void Main()
{
int elem = 10;
S[] IntArray = new S[elem];
for (int s = 0;s < elem ; s++)
{
IntArray[s].a = elem - s;
IntArray[s].b = elem - s;
IntArray[s].c = elem - s;
IntArray[s].d = elem - s;
}
// Pass array and number of elements, IntArray is pinned for the duration of
the call only!
func_c( IntArray, IntArray.Length);
}
}

//C program....
struct S
{
int a;
int b;
int c;
int d;
};
// take pointer to void for the array pointer
extern "C" {
__declspec(dllexport) void __stdcall func_c (void * pas, int len);
}

void __stdcall func_c (void * pas, int len)
{
int ret = 0;
S *sa = new S[len]; // allocate array of struct in native heap
S *s = static_cast<S*>(pas); // cast array * to struct *
// copy array of struct elements to new array (native memory)
for (int n = 0; n < len; n++)
{
sa[n].a = (s + n)->a;
sa[n].b = (s + n)->b;
sa[n].c = (s + n)->c;
sa[n].d = (s + n)->d;
}
// Use array when done
// Dump to console for demo
for (int n = 0; n < len; n++)
{
printf_s("Cont %d\n", sa[n].a);
printf_s("Cont %d\n", sa[n].b);
printf_s("Cont %d\n", sa[n].c);
printf_s("Cont %d\n", sa[n].d);
}
}
OR

How do can I hold that array of structures in C# Library itself and then
pass one such structure each time the C DLL contacts the C# Library? Not sure what you mean with OR.

What exactly do you mean with contacts the C# library? How do I
hold and pass the address of the ArrayList or HashTable between the C DLL
and
C# Library?

Any suggestions/help to any of the above two questions would be greatly
appreciated.


But it seems like you are looking for something which isn't possible, you
cannot take the address of a managed object and use it as such in unmanaged
code.
That's the whole point of marshaling, a C program has no idea what an
Hashtable is or looks like, so you have to convert the data members to
something that C can understand and use, and that is what marshaling is
about.

Willy.


Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: jason | last post by:
Hello everyone, I have a C# object library which encapsulates data. Data is keyed by way of Guid values, which are stored in the objects as read-only Guid properties. However, we have a...
4
by: dogalacar | last post by:
Hi All, I am trying to pass array of structures from a C dll to C# as msdn sample does(outarrayofstructs sample) but PtrToStructure function gives error : --> "structure must not be a value...
8
by: Bryan G | last post by:
Hi, I'm working on a VB project which involves using C library functions which take struct pointers as args, and I keep running into this error when trying to pass either an IntPtr or a Structure...
2
by: Steve Turner | last post by:
I have read several interesting posts on passing structures to C dlls, but none seem to cover the following case. The structure (as seen in C) is as follows: typedef struct tag_scanparm { short...
3
by: Steve | last post by:
Hello, I created a public Structure in a Standard Module and also an array of Structures. Then I load data into the array of structures in a public sub that I call on the Form load event. ...
3
by: Christopher H | last post by:
I've been reading about how C# passes ArrayLists as reference and Structs as value, but I still can't get my program to work like I want it to. Simple example: ...
13
by: ElderGeek | last post by:
Hello. I'm pretty new to C, and having a problem which I suspect this has to do with how I'm handling (or not handling) pointers. I have an array of dirent structures which I am passing to a...
7
by: pereges | last post by:
which one do you think is better ? I need to make my program efficient and in some places I have passed the copy of a variable which makes life some what easy while writing huge expressions but...
6
by: Andy Baker | last post by:
I am attempting to write a .NET wrapper for a C++ DLL file, but am having problems with passing strings as parameters. How should I be writing my C# function call when the C header file is...
13
by: Andy Baker | last post by:
I am attempting to write a .NET wrapper in C# for an SDK that has been supplied as a .LIB file and a .h header file. I have got most of the functions to work but am really struggling with the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.