Passing class through using P invoke

  • Thread starter Thread starter Feldaspar
  • Start date Start date
F

Feldaspar

Hi,

I'm trying to pass an object through to unmanaged code using P invoke.
My code for .net and unmanaged (visual objects) code is below.

The object properties are not coming through right. The address is
coming across as a garbled string and the postcode is not the right
number, any ideas what i am doing wrong?


..NET CODE
*************************************************************


[DllImport(@"C:\test\test.DLL")]
static extern string
PassObject([MarshalAs(UnmanagedType.LPStruct)]TestObject oobject);

[StructLayout(LayoutKind.Sequential)]
public class TestObject
{
[MarshalAs(UnmanagedType.LPTStr)]
public string address;

[MarshalAs(UnmanagedType.U4)]
public int postcode;
}


private void Call()
{
TestObject o = new TestObject();
o.address = "test";
o.postcode = 99;
PassObject(o);
}


VISUAL OBJECT CODE:
******************************************************************************

FUNCTION PassObject(oobject AS testobject)
WarningBox{,,oobject:oAddress}:Show()
warningbox{,,AsString(oobject:opostcode)}:Show()
RETURN "what's up"

CLASS testobject
EXPORT oAddress AS STRING
EXPORT opostcode AS INT
 
I'm trying to pass an object through to unmanaged code using P invoke.
My code for .net and unmanaged (visual objects) code is below.

The object properties are not coming through right. The address is
coming across as a garbled string and the postcode is not the right
number, any ideas what i am doing wrong?

I'm not familiar with Visual Object so I don't know the details of its
data types. What's the size of its INT type? Which character encoding
is used in its STRING type? Does it guarantee to preserve member order
in memory of its classes.


Mattias
 
Back
Top