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 / Languages / VB.NET / October 2007

Tip: Looking for answers? Try searching our database.

Declare Sub XXX Lib "X.dll" (Structure vs Class) ...

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Joe HM - 30 Oct 2007 12:42 GMT
Hello -

I have an Ada *.dll that is called from withing VB.NET.  The following
works ...

Public Declare Sub XXX Lib "X.dll" (ByRef XArgument As XStructure)

<StructLayout(LayoutKind.Explicit)> _
Structure XStructure
   <FieldOffset(0)> Dim A As Single
   <FieldOffset(4)> Dim B As Single
   ...
End Structure

... but I would like to use a Class to utilize inheritance.  Here is
what does not work ...

Public Declare Sub XXX Lib "X.dll" (ByRef XArgument As XClassBase)

Class XClassBase
End Class

<StructLayout(LayoutKind.Explicit)> _
Class XClassDerived
   Inherits XClassBase

   <FieldOffset(0)> Dim A As Single
   <FieldOffset(4)> Dim B As Single
   ...
End Class

... when I call it using ...

Dim lDummy as New XClassDerived
XXX(CType(lDummy, XClassDerived))

I assume that the Class stores some additional data that confuses the
interface to teh X.dll?  Is there any way to make this work???

Thanks!
Joe
Armin Zingler - 30 Oct 2007 13:54 GMT
> Dim lDummy as New XClassDerived
> XXX(CType(lDummy, XClassDerived))
>
> I assume that the Class stores some additional data that confuses
> the interface to teh X.dll?

No. A structure is a value type. A class is a reference type. Using ByRef,
the reference to the passed value is passed. Using ByVal.... wait...

http://groups.google.com/group/microsoft.public.dotnet.languages.vb/msg/41e83beb
9988aabf


> Is there any way to make this work???

Change it to ByVal: (ByVal XArgument As XClassBase)

Armin
Joe HM - 30 Oct 2007 14:18 GMT
Hello -

Thanks for the information!  Using ByVal will not make it abort
anymore but the function in the X.dll that is called modifies the
fields of the data passed in.  So I need to use ByRef to get to the
modified values.

Is there any way to make this work with ByRef?

Thanks,
Joe

> > Dim lDummy as New XClassDerived
> > XXX(CType(lDummy, XClassDerived))
[quoted text clipped - 12 lines]
>
> Armin
Terry - 30 Oct 2007 14:48 GMT
Passing a reference type byval does allow the function to change the passed
type.  ByVal of a reference type, passes the reference, allowing the routine
to change the object that the reference points to.  ByRef of a reference
type, passes a reference to the reference, allowing the routine to change the
reference!
Signature

Terry

> Hello -
>
[quoted text clipped - 24 lines]
> >
> > Armin
Joe HM - 30 Oct 2007 15:10 GMT
Yep ... that was my point.  I need this to work with ByRef ... any
ideas?

Thanks!
Joe

> Passing a reference type byval does allow the function to change the passed
> type.  ByVal of a reference type, passes the reference, allowing the routine
[quoted text clipped - 34 lines]
>
> - Show quoted text -
Rory Becker - 30 Oct 2007 15:33 GMT
> Yep ... that was my point.  I need this to work with ByRef ... any
> ideas?

I *think* Terry was trying to say that you can change your declaration to
ByVal and you will still be able to change the fields of the param in question.
ByVal only prevents you from assigning a new object to the param.

--
Rory
Terry - 30 Oct 2007 15:51 GMT
you had said ..."but the function in the X.dll that is called modifies the
fields of the data passed in"
And I am saying, passing a reference type byval DOES allow that.  When
passing a reference type byval, a copy of the REFERENCE (not the object the
reference points to) is passed to the function so that there are now 2
references to the SAME object.
Now having said all that, I am not familiar with Ada, so I don't know if
that presents any additional complications.
Signature

Terry

> Yep ... that was my point.  I need this to work with ByRef ... any
> ideas?
[quoted text clipped - 40 lines]
> >
> > - Show quoted text -
Joe HM - 30 Oct 2007 17:33 GMT
Hello -

Sorry guys ... I guess I did not pay attention when I read this.  I
remember the concept of passing in a copy of the reference from C++.
I did not create the *.dll but I can talk to the person who did to see
whether this can be done in Ada.

Thanks!
Joe

> you had said ..."but the function in the X.dll that is called modifies the
> fields of the data passed in"
[quoted text clipped - 53 lines]
>
> - Show quoted text -
Jack Jackson - 30 Oct 2007 18:07 GMT
No you don't need ByRef.  In either case the called routine can modify
the contents of the class or structure.

With ByRef the called routine can allocate a new instance of the class
and change the reference in the caller.  With ByVal the caller's
reference can't change.

>Hello -
>
[quoted text clipped - 24 lines]
>>
>> Armin

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.