473,386 Members | 2,078 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,386 software developers and data experts.

Passing array of int to c DLL

Hello

I have a problem that I cannot seem to solve

I have a c funtion in a DLL that basically looks like
func_c (some_struct* s) {...}

some_struct is defined like so:

struct some_struct {
int count_bytes; // number of
// elements in array_int
int* array_int;
}

In the c# code, I go like

[DllImport("my.dll")]
unsafe private static extern int func_c(some_struct** ao);

/*...*/

unsafe private SOME_FUNC_NAME () {
some_struct s = new some_struc;
s.count_bytes = 10;

// here, I want to set
// s.array[0] = 44;
// s.array[1] = 58;
//...
// s.array[9] = 10;
}
But I have no idea how to create the array (new int[] or what)
and how to assign the numbers to the array.

Any help appreciated
Albert
Nov 16 '05 #1
3 2140
Dear Albert,

Does the following :
http://msdn.microsoft.com/library/de...llingtypes.asp

solve your problem (look under Marshalling Arrays) ?

Regards,
Bart

"Albert Albani" wrote:
Hello

I have a problem that I cannot seem to solve

I have a c funtion in a DLL that basically looks like
func_c (some_struct* s) {...}

some_struct is defined like so:

struct some_struct {
int count_bytes; // number of
// elements in array_int
int* array_int;
}

In the c# code, I go like

[DllImport("my.dll")]
unsafe private static extern int func_c(some_struct** ao);

/*...*/

unsafe private SOME_FUNC_NAME () {
some_struct s = new some_struc;
s.count_bytes = 10;

// here, I want to set
// s.array[0] = 44;
// s.array[1] = 58;
//...
// s.array[9] = 10;
}
But I have no idea how to create the array (new int[] or what)
and how to assign the numbers to the array.

Any help appreciated
Albert

Nov 16 '05 #2

"Albert Albani" <al**********@yahoo.de> wrote in message
news:7c**************************@posting.google.c om...
Hello

I have a problem that I cannot seem to solve

I have a c funtion in a DLL that basically looks like
func_c (some_struct* s) {...}

some_struct is defined like so:

struct some_struct {
int count_bytes; // number of
// elements in array_int
int* array_int;
}

In the c# code, I go like

[DllImport("my.dll")]
unsafe private static extern int func_c(some_struct** ao);

/*...*/

unsafe private SOME_FUNC_NAME () {
some_struct s = new some_struc;
s.count_bytes = 10;

// here, I want to set
// s.array[0] = 44;
// s.array[1] = 58;
//...
// s.array[9] = 10;
}
But I have no idea how to create the array (new int[] or what)
and how to assign the numbers to the array.

Any help appreciated
Albert


Ok, here I suppose your function signature and struct declarations are
exactly as you posted.
Basically you are passing a "pointer to a structure, containing an int and a
pointer to an array of int's", right?
What you have to do is create/fill the array, create a GCHandle to pin the
array during the call , set array_int to the address of the first element
of the (pinned) array and pass this struct by ref.

Herewith a working sample illustrating the process:

// CPP file
#include <iostream>
struct some_struct {
int count_bytes; // number of
// elements in array_int
int* array_int;
};

extern "C" {
__declspec(dllexport) int func_c (some_struct* s);
}
int func_c (some_struct* s)
{
for(int j = 0; j < s->count_bytes; j++)
std::cout << *s->array_int++ << std::endl;
return s->count_bytes;
}
//CS file
using System;
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential)]
struct S
{
internal int size;
internal IntPtr pIntArray;
}
class Tester
{
[DllImport("native.dll")]
private static extern int func_c(ref S ao);
static void Main()
{
int[] IntArray = new int[10] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
S s = new S();
s.size = 10;
GCHandle gcH = GCHandle.Alloc(IntArray);
s.pIntArray = Marshal.UnsafeAddrOfPinnedArrayElement(IntArray, 0);
Console.WriteLine(func_c(ref s));
gcH.Free();
}
}
Willy.
Nov 16 '05 #3
Willy, you're THE HERO

This is exactly what I needed. Thanks a lot and more

Albert
"Albert Albani" <al**********@yahoo.de> wrote in message
news:7c**************************@posting.google.c om...
Hello

I have a problem that I cannot seem to solve

I have a c funtion in a DLL that basically looks like
func_c (some_struct* s) {...}

some_struct is defined like so:

struct some_struct {
int count_bytes; // number of
// elements in array_int
int* array_int;
}

In the c# code, I go like

[DllImport("my.dll")]
unsafe private static extern int func_c(some_struct** ao);

/*...*/

unsafe private SOME_FUNC_NAME () {
some_struct s = new some_struc;
s.count_bytes = 10;

// here, I want to set
// s.array[0] = 44;
// s.array[1] = 58;
//...
// s.array[9] = 10;
}
But I have no idea how to create the array (new int[] or what)
and how to assign the numbers to the array.

Any help appreciated
Albert


Ok, here I suppose your function signature and struct declarations are
exactly as you posted.
Basically you are passing a "pointer to a structure, containing an int and a
pointer to an array of int's", right?
What you have to do is create/fill the array, create a GCHandle to pin the
array during the call , set array_int to the address of the first element
of the (pinned) array and pass this struct by ref.

Herewith a working sample illustrating the process:

// CPP file
#include <iostream>
struct some_struct {
int count_bytes; // number of
// elements in array_int
int* array_int;
};

extern "C" {
__declspec(dllexport) int func_c (some_struct* s);
}
int func_c (some_struct* s)
{
for(int j = 0; j < s->count_bytes; j++)
std::cout << *s->array_int++ << std::endl;
return s->count_bytes;
}
//CS file
using System;
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential)]
struct S
{
internal int size;
internal IntPtr pIntArray;
}
class Tester
{
[DllImport("native.dll")]
private static extern int func_c(ref S ao);
static void Main()
{
int[] IntArray = new int[10] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
S s = new S();
s.size = 10;
GCHandle gcH = GCHandle.Alloc(IntArray);
s.pIntArray = Marshal.UnsafeAddrOfPinnedArrayElement(IntArray, 0);
Console.WriteLine(func_c(ref s));
gcH.Free();
}
}
Willy.

Nov 16 '05 #4

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

Similar topics

8
by: Alex Vinokur | last post by:
Various forms of argument passing ================================= C/C++ Performance Tests ======================= Using C/C++ Program Perfometer...
15
by: Dave | last post by:
I'm currently working on a small project (admitedly for my CS class) that compares the time difference between passing by value and passing by reference. I'm passing an array of 50000 int's. ...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
9
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
8
by: kalinga1234 | last post by:
there is a problem regarding passing array of characters to another function(without using structures,pointer etc,).can anybody help me to solve the problem.
10
by: Pete | last post by:
Can someone please help, I'm trying to pass an array to a function, do some operation on that array, then return it for further use. The errors I am getting for the following code are, differences...
6
by: DeepaK K C | last post by:
Could anybody tell me how to pass array to a function by value? -Deepak
11
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line...
8
by: S. | last post by:
Hi all, Can someone please help me with this? I have the following struct: typedef struct { char *name; int age; } Student;
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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.