Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / September 2004

Tip: Looking for answers? Try searching our database.

(out pointer to) array of structs marshaled from c++ to c# as return value

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Pavel Savara - 21 Sep 2004 13:36 GMT
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

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.