.NET Framework Bookmark and Share   
 index > Common Language Runtime > marshalling array of structs with array of structs as one of the parameters
 

marshalling array of structs with array of structs as one of the parameters

I have following structs:

public struct AP_LS

{

public int SoT;
public int GT;
} ;

public struct AP_S
{
public int N;
MarshalAs(UnmanagedType.ByValArray,ArraySubType=UnmanagedType.Struct, SizeConst = MAX_NUM_P)]
public AP_LS[] problemArr;
}

pubic struct TRIAL_RESULTS_AP
{

public int NumR;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = FeasXConst.MAX_NUM_DS)]
public UInt32[] RH;
public int NumD;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = FeasXConst.MAX_NUM_DS)]
public Q_PROPERTIES[] D;
public int NumSchedules;
[MarshalAs(UnmanagedType.ByValArray, ArraySubType =UnmanagedType.Struct,SizeConst=MAX_NUM_AP_S)] public AP_S[] ApS; // } ;

and unmanaged dll's function
int Func (int Foo, TRIAL_RESULTS_AP[] Bar);

I can not marshal TRIAL_RESULTS_AP. Actually, the only part I have problem with is AP_S->ProblemArr.
I've tried to substitute AP_S struct with AP_S_subst struct with following signature (and corresponding TRIAL_RESULTS_AP_subst):

public struct AP_S_subst
{
public int N;
public IntPtr problemPtr;
} ;

and make wrapper for the original Func - Func_wrapper which will take origianl structure and Marshal data directly to the problemPtr using following code:

for
(int j = 0; j < PrevResults[i].ApS.Length; j++)
{
/*... rest ofassignment*/
PrevResults_F[i].ApS[j].problemPtr=
Marshal.AllocHGlobal(Convert.ToInt32(Marshal.SizeOf(new AP_LS()) * MAX_NUM_FLIGHTS_FROM_AP)); long count = PrevResults_F[i].ApS[j].problemPtr.ToInt64(); foreach (AP_LS a in PrevResults[i].ApS[j].Leg)
{
IntPtr some = new IntPtr(count);
Marshal.StructureToPtr(a, some, true);
count +=
Marshal.SizeOf(typeof(AP_LS));
}
}

but this does not work either!!

Thank you for any help.

olelukoyya
Remove the UnmanagedType.Struct declarations they don't do what you think they do. They are not needed here, the marshaller can figure out what to do from the structure type.

The substitute you tried is not equivalent, a ByValArray is the opposite of an IntPtr. If it still doesn't work, you'll have to tell us a *lot* more about what is going wrong.

Hans Passant.
nobugz
Hans,
thank you for prompt answer.
I actually added UnmanagedType.Structafter I'd failed first time.

I understand that ByValArray is the "oppositte" - I wnated to replace array of struct with memory buffer (lets say, struct ss[15] I am trying to substitute with IntPtr buff, and then Marshal.StrToPtr(ss[i], buff+sizeof(typeof(ss), false).
Please, let me know what is A LOT - I would like to solve this problem ASAP:)


Thank you
olelukoyya
Hello olelukoyya

Please use the tool PInvoke Interop Assistant (http://clrinterop.codeplex.com/) tomake sure that the .NET representitives of the native structs are declared rightly. PInvoke Interop Assistant takes the inputs of native structs and outputs the 100% right .NET corresponds.Please post the output of the tool here.

Thanks
Jialiang Ge
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Jialiang Ge [MSFT]
Hi Jialiang,

Not sure this tool handle my situation properly

One example, I have unmanaged function whih expects array of structs to be passed void Func1(SOME_STRUCT[] st, int p);
The tool converts it to

[System.Runtime.InteropServices.DllImportAttribute("<Unknown>", EntryPoint="Func1")]
public static extern int Func1(ref SOME_STRUCT st, int p);

But I cannot pass Array of structures to this function from the C# code now, since once i declare
SOME_STRUCT[] in_st = new SOME_STRUCT(5);
and then fill it with data, I will receive comipler's error (which I'd tested:
Error2The best overloaded method match for Func1(ref SOME_STRUCT, int); has some invalid arguments)


Still looking for help! Please,let mw know what data is needed to clarify the situation.
I dont think that it is possible to pass array of struct in this case.

olelukoyya
It is not a great tool, forget about it. Your original declarations are good. You haven't told us anything about how you know it doesn't work, can't help you without that.

Hans Passant.
  • Marked As Answer byolelukoyya 14 hours 45 minutes ago
  • Unmarked As Answer byolelukoyya 14 hours 44 minutes ago
  •  
nobugz

You can use google to search for other answers

Custom Search

More Threads

• Network share and .Net app
• Interop: .NET COM (CCW) fails to clean up; application won't exit
• How can I load using assemblu by passing the form name as string
• Calling Methods On A COM Interface
• PerformanceCounter acces denied: acces rights question
• Out-of-process invocation of .NET COM visible object does not work
• Calling unmanaged c++ dll's and lib's into c# using visual studio 2005 beta
• How to get notified if finalization is aborted?
• web app works with visual studio test not on production webserver
• Managed COM component memory not released after calling "ReleaseDispatch"