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 / April 2006

Tip: Looking for answers? Try searching our database.

Best way to return a struct of strings?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Scott Markwell - 03 Apr 2006 21:15 GMT
I want to pass back a group of data to a caller.  What I am currently
doing involves returning a pointer to a struct defined in IDL and
available in the TLB.  The thing is this gets messy w/ managed .NET code
involving messing w/ a System.IntPtr.  Will managed code properly
release a structure with BSTR's located within the struct during garbage
collection?  Is there a better way then providing a function for
unmanaged users to call to release a object?

typedef struct DataResult
{
    BSTR infoA;
    BSTR infoB;
    BSTR infoC;
    long infoD;
    boolean infoE;
}DataResult;

[
    object,
]
    interface IFoo:IUnknown
{
    HRESULT GetData([out,retval]DataResult **dataOut);
    HRESULT FreeData([in,out]DataResult **release);
};
Scott M. - 03 Apr 2006 22:29 GMT
Why not just make a class with a method that returns what you need?  Strings
are classes (reference types) and are stored on the heap anyway, so what do
you gain by using a structure?

>I want to pass back a group of data to a caller.  What I am currently doing
>involves returning a pointer to a struct defined in IDL and available in
[quoted text clipped - 21 lines]
>     HRESULT FreeData([in,out]DataResult **release);
> };
Scott Markwell - 03 Apr 2006 23:43 GMT
Ease of use for a caller. Instead of building their own local format.
Basically the goal is to pass back some structure that is convent for a
caller from C++ or managed code and provides all the data from a single
record in a single type.

> Why not just make a class with a method that returns what you need?  Strings
> are classes (reference types) and are stored on the heap anyway, so what do
[quoted text clipped - 25 lines]
>>     HRESULT FreeData([in,out]DataResult **release);
>> };
Scott Markwell - 04 Apr 2006 01:04 GMT
Think I've found what I need.
System.Runtime.InteropServices.Marshal.DestroyStructure should handle
the BSTR's and call SysFreeString on them.

> Ease of use for a caller. Instead of building their own local format.
> Basically the goal is to pass back some structure that is convent for a
[quoted text clipped - 30 lines]
>>>     HRESULT FreeData([in,out]DataResult **release);
>>> };

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.