473,394 Members | 1,889 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,394 software developers and data experts.

Passing array adresses and pointers to C DLL from C#

Hi, I have some problems with marshaling data to and from C DLL file;

C definitoin for function is:

int32 s7_get_multiple_read_cnf(
void *od_ptr, /* In call, must be null*/
uint16 *result_array, /* Returned adress of an array of uint16*/
uint16 *var_length_array, /*In call and Returned adress of an array
uint16 */
void *value_array /* Returned, pointer of buffer provided by user
programm*/
);
I can`t tell how this function executes, because this is "s732.dll" DLL
file from Siemens for S7 communications. But basicly, all the arrays
should be the same size, because this function writes readed data form
Profibus to these arrays.

In C# function definitios is this:
[DllImport("s732std.dll", SetLastError=true, CharSet=CharSet.Ansi,
CallingConvention = CallingConvention.Winapi)]
public static extern Int32 s7_get_multiple_read_cnf(IntPtr od_ptr,
[In,Out] UInt16[] rec_msg_array, [In,Out] UInt16[] var_lenght_array,
ref IntPtr var_value_array);

I call this function like that:
int ret;
UInt16[] msg_result_array = new UInt16[3];
UInt16[] var_length_array = new UInt16[3];
var_length_array[0] = 2;
var_length_array[1] = 2;
var_length_array[2] = 2;

Int16[] var_value_array = new Int16[3];

IntPtr my_var_value_pointer = Marshal.AllocHGlobal(Marshal.SizeOf(new
System.Int16())*(var_value_array.Length));
Marshal.Copy(var_value_array, 0, my_var_value_pointer,
var_value_array.Length);

ret = s7_get_multiple_read_cnf(new IntPtr(0), msg_result_array,
var_length_array, ref my_var_value_pointer);

The problem is that, if DLL function have to read one element,
everything is fine, but if DLL function have to read more than one
element, I recieve NullreferenceException!!!!!
any ideas????

May 30 '06 #1
1 4469
Since you want to get the values back, you are going to have to marshal
the arrays manually. The runtime does not know how many elements you are
being set when sent back, so it only marshals the first one (or tries to).

What you want to do is declare your function like this:

[DllImport("s732std.dll", SetLastError=true, CharSet=CharSet.Ansi)]
public static extern Int32 s7_get_multiple_read_cnf(IntPtr od_ptr, IntPtr
rec_msg_array, IntPtr var_lenght_array, IntPtr var_value_array);

Then, you will have to allocate and write to the unmanaged memory (using
the methods on the Marshal class), call the function, then read it back when
the function completes.

You can also do this in unsafe code as well (and would be much easier to
do as well).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<du*****@gmail.com> wrote in message
news:11**********************@j55g2000cwa.googlegr oups.com...
Hi, I have some problems with marshaling data to and from C DLL file;

C definitoin for function is:

int32 s7_get_multiple_read_cnf(
void *od_ptr, /* In call, must be null*/
uint16 *result_array, /* Returned adress of an array of uint16*/
uint16 *var_length_array, /*In call and Returned adress of an array
uint16 */
void *value_array /* Returned, pointer of buffer provided by user
programm*/
);
I can`t tell how this function executes, because this is "s732.dll" DLL
file from Siemens for S7 communications. But basicly, all the arrays
should be the same size, because this function writes readed data form
Profibus to these arrays.

In C# function definitios is this:
[DllImport("s732std.dll", SetLastError=true, CharSet=CharSet.Ansi,
CallingConvention = CallingConvention.Winapi)]
public static extern Int32 s7_get_multiple_read_cnf(IntPtr od_ptr,
[In,Out] UInt16[] rec_msg_array, [In,Out] UInt16[] var_lenght_array,
ref IntPtr var_value_array);

I call this function like that:
int ret;
UInt16[] msg_result_array = new UInt16[3];
UInt16[] var_length_array = new UInt16[3];
var_length_array[0] = 2;
var_length_array[1] = 2;
var_length_array[2] = 2;

Int16[] var_value_array = new Int16[3];

IntPtr my_var_value_pointer = Marshal.AllocHGlobal(Marshal.SizeOf(new
System.Int16())*(var_value_array.Length));
Marshal.Copy(var_value_array, 0, my_var_value_pointer,
var_value_array.Length);

ret = s7_get_multiple_read_cnf(new IntPtr(0), msg_result_array,
var_length_array, ref my_var_value_pointer);

The problem is that, if DLL function have to read one element,
everything is fine, but if DLL function have to read more than one
element, I recieve NullreferenceException!!!!!
any ideas????

May 30 '06 #2

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

Similar topics

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.
2
by: Morgan | last post by:
Thanks to all of you because I solved the problem related with my previous post. I simply made confusion with pointers to pointers and then succeeded passing the reference to the first element...
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...
4
by: Christian Maier | last post by:
Hi After surfing a while I have still trouble with this array thing. I have the following function and recive a Segmentation fault, how must I code this right?? Thanks Christian Maier
2
by: luis | last post by:
I'm using ctypes to call a fortran dll from python. I have no problems passing integer and double arryas, but I have an error with str arrys. For example: ..... StringVector = c_char_p *...
17
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...
23
by: osama178 | last post by:
Let's say I have this code -------------------- class GenericQueue { public: bool Pop(void*& refToPtr); // //--------------------(1) };
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
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...

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.