Hello,
c++ signature:
struct TickInfo
{
time_t ctm;
char symbol[8];
double bid;
double ask;
};
__declspec(dllimport)
TickInfo* MtGetUpdatedTickInfo(const int handle,int *items);
my c# try:
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi, Pack=8)]
public struct TickInfo
{
public uint ctm;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=8)]
public string symbol;
public double bid;
public double ask;
};
[return: MarshalAs(UnmanagedType.LPArray, SizeParamIndex=1)]
[DllImport "mtapi.dll",CallingConvention=CallingConvention.Cdecl)]
public static extern TickInfo[]
MtGetUpdatedTickInfo(int handle,[Out]out int items);
- - - - - - - - - - - - - - - - - -
problem:
System.Runtime.InteropServices.MarshalDirectiveException
_message "Can not marshal return value."
1) Does anybody know how to use marshaler properly in this case ?
2) I tried use IntPtr as return type and that worked, but I don't know
how to read data from IntPtr(as pointer to array of structs).
3) Is it possible to marshal it using atributes whenever c++ signature
looks like:
void MtGetUpdatedTickInfo(const int handle,int *items,TickInfo**
array);
Thanks
Pavel Savara
Mattias Sj?gren - 21 Sep 2004 17:13 GMT
Pavel,
>2) I tried use IntPtr as return type and that worked, but I don't know
>how to read data from IntPtr(as pointer to array of structs).
Read item by item with Marshal.PtrToStructure.
Mattias

Signature
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Pavel Savara - 21 Sep 2004 18:24 GMT
>>2) I tried use IntPtr as return type and that worked, but
>>I don't know how to read data from IntPtr(as pointer to
>>array of structs).
>Read item by item with Marshal.PtrToStructure.
How ?
IntPtr pt=MtGetUpdatedTickInfo(handle,out count);
TickInfo[] ti=new TickInfo[count];
Marshal.PtrToStructure(pt,ti);
throws:
System.ArgumentException
"The specified structure must be blittable or have layout information."
how to solve it ?
Pavel Savara - 22 Sep 2004 10:13 GMT
> >>2) I tried use IntPtr as return type and that worked, but
> >>I don't know how to read data from IntPtr(as pointer to
[quoted text clipped - 13 lines]
>
> how to solve it ?
OK, I've found it on your pages
http://www.dotnetinterop.com/faq/?q=CalleeAllocatedStructArray
Thank you
Pavel