How to marshall a struct with a string in it from C# to unrealscript?

167 views Asked by At

I am investigating what is possible with Robert Giesecke's approach to calling C# DLL Functions from Unrealscript. I have been following the fine examples Located at gamedev.net as well. (Okay, now everything is sourced :)

I would like to pass a structure from C# back to Unrealscript that contains a string. I have tried several things, none of which quite have worked, but my current c# struct looks like this:

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    struct PSTest
    {
        public float a;
        public int b;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
        public string c;
    }

..And my additional C# code looks like this:

    private static IntPtr MarshalToPointer(object data)
    {
        IntPtr buf = Marshal.AllocHGlobal(
            Marshal.SizeOf(data));
        Marshal.StructureToPtr(data,
            buf, false);
        return buf;
    }

    [DllExport("ReturnTesting", CallingConvention = CallingConvention.StdCall)]
    static IntPtr ReturnTesting()
    {
        PSTest ps = new PSTest();
        ps.a = 1.0f;
        ps.b = 2;
        ps.c = "a";

        IntPtr lpstruct = MarshalToPointer(ps);
        return lpstruct;
    }

I am calling those functions from UnrealScript, and my UnrealScript looks like the following:

struct MYTest
{
var float a;
var int b;
var string c;
};

dllimport final function MYTest ReturnTesting();

function DoWork()
{
    local MYTest my;

    my = ReturnTesting();
}

The Unrealscript here is a little simplified, but it works.

When I execute the Unrealscript, the struct comes back from ReturnTesting() with valid data for the float and int values in variables a and b, but the variable c is a blank string. This is the best situation I have gotten this code into, as mucking with the string variables in the structs usually ends up with crashes.

Has anyone had experience with a similar situation? How can I properly pass string information in my c# struct back to Unrealscript? Any hints and information is much appreciated, and I thank everyone ahead of time.

0

There are 0 answers