使用托管代码调用非托管代码的 MarshalAs 查询
这个问题是此线程的结果。
我的问题是,为什么我们必须为某些参数指定 MarshalAs 属性而不是其他参数?我本以为 C++ 和 C# 数据类型之间的差异足够大,需要编译器指导?
请原谅我的无知,因为我以前从未处理过非托管代码:)
干杯, 亚当
This question is a result of this thread.
My question is, why do we have to specify the MarshalAs attribute for some of the parameters but not others? I would have thought the differences between c++ and c# data types would be large enough to require direction for the compiler?
Excuse my ignorance, as ive never dealt with unmanaged code before :)
Cheers,
Adam
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
未编组的参数只是整数。整数就是整数,整数就是整数,无处不在,无论是否是原生的。因此不需要任何特殊的指令来处理它们,值只是按值传递,就可以了。
指针和字符串更加棘手。 C# 字符串不一定像 C 字符串一样表示,C# 字符串只是指向以 null 结尾的字符数组的指针。可能需要进行内部转换。同样,数组需要通过引用传递 - 并明确告知这样做。
请记住,在非托管代码中,地址只是数据。他们没有什么特别的。因此,C# 编译器需要知道如何获取有关变量的所有信息,并将其转换为数字。
The parameters that aren't marshalled are just ints. Ints are ints are ints are ints, everywhere, native or not. So there don't need to be any special instructions to deal with them, the value is just passed by value, and you're all set.
The pointers and strings are more tricky. C# strings aren't necessarily represented like C strings, which are simply pointers to a null-terminated array of characters. Internal conversion might be necessary. Similarly, the array needs to be passed by reference - and explicitly told to do so.
Remember, in unmanaged code, addresses are just data. There's nothing special about them. So the C# compiler needs to know how to take all it knows about your variables, and turn it into numbers.