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

J

Joe HM

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
 
A

Armin Zingler

Joe HM said:
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/41e83beb9988aabf

Is there any way to make this work???

Change it to ByVal: (ByVal XArgument As XClassBase)


Armin
 
J

Joe HM

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
 
G

Guest

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!
 
J

Joe HM

Yep ... that was my point. I need this to work with ByRef ... any
ideas?

Thanks!
Joe
 
R

Rory Becker

Yep ... that was my point. I need this to work with ByRef ... any


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.
 
G

Guest

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.
 
J

Joe HM

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
 
J

Jack Jackson

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top