Home All Groups Group Topic Archive Search About

Passing class through using P invoke

Author
8 Jan 2007 1:01 AM
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

Author
8 Jan 2007 6:06 AM
Mattias Sjögren
>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

--
Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

AddThis Social Bookmark Button