简体   繁体   English

C#指针:错误CS0254:固定语句赋值的右侧可能不是强制转换表达式

[英]C# Pointer: error CS0254: The right hand side of a fixed statement assignment may not be a cast expression

I have a c++ server and now a trying to make ac# client. 我有一个c ++服务器,现在正在尝试制作ac#客户端。

In c++, to send a packet I make this: 在c ++中,要发送数据包,请执行以下操作:

PMessage *msg = (PMessage*)&buf[0];
msg->id = PID_LOGIN;
sendto(sServer, buf, sizeof(buf), 0, (SOCKADDR*)&from, fromlen);

in c# I trying this: 在C#中我尝试这样做:

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 this error: 编译器显示此错误:

error CS0254: The right hand side of a fixed statement assignment may not be a cast expression. 错误CS0254:固定语句分配的右侧可能不是强制转换表达式。

c#: C#:

[StructLayout(LayoutKind.Sequential, Pack = 1)]
 unsafe struct PMessage
{
    public  fixed byte msg[256];
    public int id;
    public int size;
}

c++: C ++:

   struct PMessage
    {
        char msg[256];
        unsigned int id;
        unsigned int size;
    };

Try this: 尝试这个:

PMessage p = new PMessage();
// p.id = 1001;
byte[] rawdata = new byte[Marshal.SizeOf(typeof(PMessage))];
IntPtr mem = Marshal.AllocHGlobal(rawdata.Length);
try{
    Marshal.StructureToPtr(p, mem, true);
    Marshal.Copy(mem, rawdata, 0, rawdata.Length);
}finally{
    Marshal.FreeHGlobal(mem);
}
udpClient.Send(rawdata, rawdata.Length);

Also don't mix unsafe ( fixed ) and unmanaged ( SizeOf ) environments. 也不要将不安全( fixed )和非托管( SizeOf )环境混合使用。

You can't do this, because the compiler loses track of which object needs to be pinned. 您无法执行此操作,因为编译器无法跟踪需要固定哪个对象。

fixed (PMessage* p = (PMessage*)&rawdata[0])

However this should work 但是,这应该工作

fixed (byte* pby = &rawdata[0]) {
    PMessage* p = (PMessage*)pby;

Please note that your C++ code was probably violating strict aliasing, and both C++ and C# may not have correct alignment. 请注意,您的C ++代码可能违反了严格的别名,并且C ++和C#可能没有正确的对齐方式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#错误中的左侧分配 - Left hand side assignment in C# error 右侧的三元/空合并运算符和赋值表达式? - Ternary/null coalescing operator and assignment expression on the right-hand side? “解构赋值需要右侧类型的表达式” - "Deconstruct assignment requires an expression with a type on the right hand side" c# 的初学者遇到 CS0131 问题:赋值的左侧必须是变量、属性或索引器 - Beginner to c# having issues with CS0131: The left-hand side of an assignment must be a variable, property or indexer C# 编译器错误 CS1963:“表达式树不能包含动态操作” - C# compiler error CS1963: "An expression tree may not contain a dynamic operation" C#类变量问题,不安全/固定的指针分配 - C# Problem with class variable, unsafe/fixed pointer assignment C#错误CS0201:只能将赋值,调用,递增,递减和新对象表达式用作语句 - C# error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement 错误 CS0201:只有赋值、调用、递增、递减、等待和新的 object 表达式可以用作语句 c# - error CS0201: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement c# 分配的C#左侧必须是变量,属性或索引器 - C# Left-Hand Side Of An Assignment Must Be a Variable, Property or Indexer 有条件地选择赋值语句左侧的值? - Conditionally choose a value on the left hand side of an assignment statement?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM