编组它是什么以及为什么我们需要它?

发布于 2024-08-20 14:56:55 字数 199 浏览 5 评论 0原文

什么是编组以及为什么我们需要它?

我很难相信我无法通过线路从 C# 向 C 发送 int 并且必须对其进行编组。为什么 C# 不能仅发送 32 位以及起始和终止信号,告诉 C 代码它已收到 int

如果有任何关于为什么我们需要编组以及如何使用它的好的教程或网站,那就太好了。

What is marshalling and why do we need it?

I find it hard to believe that I cannot send an int over the wire from C# to C and have to marshall it. Why can't C# just send the 32 bits over with a starting and terminating signal, telling C code that it has received an int?

If there are any good tutorials or sites about why we need marshalling and how to use it, that would be great.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

一个人练习一个人 2024-08-27 14:56:55

因为不同的语言和环境有不同的调用约定、不同的布局约定、不同的基元大小(参见 C# 中的 char 和 C 中的 char)、不同的对象创建/销毁约定,以及不同的设计准则。您需要一种方法将这些内容从受管理的土地中转移到非受管理的土地可以看到和理解的地方,反之亦然。这就是编组的目的。

Because different languages and environments have different calling conventions, different layout conventions, different sizes of primitives (cf. char in C# and char in C), different object creation/destruction conventions, and different design guidelines. You need a way to get the stuff out of managed land an into somewhere where unmanaged land can see and understand it and vice versa. That's what marshalling is for.

倾城月光淡如水﹏ 2024-08-27 14:56:55

.NET 代码(C#、VB)被称为“托管”,因为它是由 CLR“管理”的 (公共语言运行时

如果您用 C 或 C++ 或汇编语言编写代码,那么它都被称为“非托管”,因为不涉及 CLR。您负责所有内存分配/取消分配。

编组是托管代码和非托管代码之间的过程;它是 CLR 提供的最重要的服务之一。

.NET code(C#, VB) is called "managed" because it's "managed" by CLR (Common Language Runtime)

If you write code in C or C++ or assembler it is all called "unmanaged", since no CLR is involved. You are responsible for all memory allocation/de-allocation.

Marshaling is the process between managed code and unmanaged code; It is one of the most important services offered by the CLR.

鹤仙姿 2024-08-27 14:56:55

理想情况下,编组 int 正是您所说的:将内存从 CLR 的托管堆栈复制到 C 代码可以看到它的某个位置。编组字符串、对象、数组和其他类型是困难的事情。

但是 P/Invoke 互操作层会为您处理几乎所有这些事情。

Marshalling an int is ideally just what you said: copying the memory from the CLR's managed stack into someplace where the C code can see it. Marshalling strings, objects, arrays, and other types are the difficult things.

But the P/Invoke interop layer takes care of almost all of these things for you.

冷情 2024-08-27 14:56:55

正如 Vinko 在评论中所说,您可以传递原始类型而无需任何特殊的编组。这些称为“blittable”类型,包括 byte、short、int、long 等类型及其无符号对应类型。

此页面包含blittable 和non-blittable 类型。

As Vinko says in the comments, you can pass primitive types without any special marshalling. These are called "blittable" types and include types like byte, short, int, long, etc and their unsigned counterparts.

This page contains the list of blittable and non-blittable types.

痴骨ら 2024-08-27 14:56:55

编组是一种“媒介”,用于需要更好的词或网关,通过使用 pinvoke 与非托管世界的数据类型进行通信,反之亦然,并确保数据以安全的方式返回。

Marshalling is a "medium" for want of a better word or a gateway, to communicate with the unmanaged world's data types and vice versa, by using the pinvoke, and ensures the data is returned back in a safe manner.

噩梦成真你也成魔 2024-08-27 14:56:55

编组是将函数的签名传递给不同机器上的不同进程,通常通过将结构化数据转换为专用格式来实现,该格式可以传输到其他处理器系统(序列化/反序列化)。

Marshalling is passing signature of a function to a different process which is on a different machine, and it is usually implemented by conversion of structured data to a dedicated format, which can be transferred to other processor systems (serialization / deserialization).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文