473,394 Members | 1,640 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 pointer reference to structure with array

Hi!
I have a problem which is quite tricky.
I need to pass a struct from .NET to a native Win32 DLL.
But i just need to pass the pointer to a reference of that struct.
With my first struct this worked pretty well, accepting to write
unsafe code ;-)
But my next struct has an array inside and I don't get it passed over
to the DLL correctly.

Here my struct in c#:

[StructLayout(LayoutKind.Sequential)]
unsafe public struct SDKNavInfo
{
public System.UInt32 size;
public TSDKPosition Pos;

[..]
//this is the relevant array!!
public System.UInt32[] PreviousManover;

[..]
}

My DLL Import looks like this (this can not be changed by myself):

[DllImport("SDK")]
unsafe private extern static int GetGuidanceInformation(TGPSData *
GPSData, System.Int32 forecast, ref SDKNavInfo *
NavigationInformation);

And this is the corresponding code where I try to call the function:

//method declared unsafe because of use of pointers and reference
private unsafe void btnCalcRoute_Click(object sender, System.EventArgs
e)
{
[..]
SDKNavInfo NavInfo = new SDKNavInfo();

[..]
SDKNavInfo * pNavInfo = &NavInfo;

GetGuidanceInformation(null, 0, ref pNavInfo);
}

Calling the GetGuidanceInformation function results in an error where
the debugger says (sorry for the translation, I get it in german!)
"parameter#3 can not be marshalled. Pointer can not point to
marshalled structures. Use byref instead."

I don't get it...what does it mean? Or even better, how can I fix this
problem??
Thanks in advance for your help :-)
cheerz
Tobias
Nov 16 '05 #1
1 9475
Tobias,

This is dependent on the original definition of your structure. If the
structure has the array embedded in it, then you can declare the structure
in .NET like this:

[StructLayout(LayoutKind.Sequential)]
unsafe public struct SDKNavInfo
{
public System.UInt32 size;
public TSDKPosition Pos;
//this is the relevant array!!
[MarshalAs(UnmanagedType.ByValArray, SizeOf=10)]
public System.UInt32[] PreviousManover;
}

Notice the SizeOf property on the MarshalAs attribute. This would be
the size of the array as it is defined in the original structure.

Now, if the array is defined as a pointer to an address in memory, then
you will have to marshal that array into unmanaged memory (getting an
IntPtr), and then define the field as an IntPtr and setting it to the
marshaled value.

If you are using unsafe code, then you can just define it as void* and
then get the address of the array and use that. You have to be careful that
the unmanaged code you call doesn't change the value of this pointer, or you
will orphan memory.

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

"Tobias" <ho***********@gmx.de> wrote in message
news:e5**************************@posting.google.c om...
Hi!
I have a problem which is quite tricky.
I need to pass a struct from .NET to a native Win32 DLL.
But i just need to pass the pointer to a reference of that struct.
With my first struct this worked pretty well, accepting to write
unsafe code ;-)
But my next struct has an array inside and I don't get it passed over
to the DLL correctly.

Here my struct in c#:

[StructLayout(LayoutKind.Sequential)]
unsafe public struct SDKNavInfo
{
public System.UInt32 size;
public TSDKPosition Pos;

[..]
//this is the relevant array!!
public System.UInt32[] PreviousManover;

[..]
}

My DLL Import looks like this (this can not be changed by myself):

[DllImport("SDK")]
unsafe private extern static int GetGuidanceInformation(TGPSData *
GPSData, System.Int32 forecast, ref SDKNavInfo *
NavigationInformation);

And this is the corresponding code where I try to call the function:

//method declared unsafe because of use of pointers and reference
private unsafe void btnCalcRoute_Click(object sender, System.EventArgs
e)
{
[..]
SDKNavInfo NavInfo = new SDKNavInfo();

[..]
SDKNavInfo * pNavInfo = &NavInfo;

GetGuidanceInformation(null, 0, ref pNavInfo);
}

Calling the GetGuidanceInformation function results in an error where
the debugger says (sorry for the translation, I get it in german!)
"parameter#3 can not be marshalled. Pointer can not point to
marshalled structures. Use byref instead."

I don't get it...what does it mean? Or even better, how can I fix this
problem??
Thanks in advance for your help :-)
cheerz
Tobias

Nov 16 '05 #2

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

Similar topics

5
by: kazack | last post by:
I am a little confused with code I am looking at. My c++ book does not go into passing a structure to a function so I pulled out a c book which does. and I do not understand the prototype verses...
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...
3
by: Steven Taylor | last post by:
Hope someone can assist. I'm trying to answer a book question. I'm going around in circles in relation to 'pointer-to-char'. Object : A short program is to be created, which involves a...
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: 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...
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...
12
by: Mike | last post by:
Consider the following code: """ struct person { char *name; int age; }; typedef struct person* StructType;
7
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the...
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: 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:
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
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
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.