c++ - C# Pointer: error CS0254: The right hand side of a fixed statement assignment may not be a cast expression -
i have c++ server , trying make c# client.
in c++, send packet make this:
pmessage *msg = (pmessage*)&buf[0]; msg->id = pid_login; sendto(sserver, buf, sizeof(buf), 0, (sockaddr*)&from, fromlen);
in c# trying this:
unsafe { try { byte[] rawdata = new byte[marshal.sizeof(typeof(pmessage))]; fixed (pmessage* p = (pmessage*)&rawdata[0]) { // p->id = 1001; } udpclient.send(rawdata, rawdata.length); ; } catch (exception ex) { messagebox.show(ex.tostring()); } }
the compiler show error:
error cs0254: right hand side of fixed statement assignment may not cast expression.
c#:
[structlayout(layoutkind.sequential, pack = 1)] unsafe struct pmessage { public fixed byte msg[256]; public int id; public int size; }
c++:
struct pmessage { char msg[256]; unsigned int id; unsigned int size; };
you can't this, because compiler loses track of object needs pinned.
fixed (pmessage* p = (pmessage*)&rawdata[0])
however should work
fixed (byte* pby = &rawdata[0]) { pmessage* p = (pmessage*)pby;
please note c++ code violating strict aliasing, , both c++ , c# may not have correct alignment.
Comments
Post a Comment