为什么(不正确)使用ref myarray [0]来传递数组,但是只能在32位应用程序中使用? [英] Why does (incorrectly) using ref myarray[0] to pass in an array work, but only in 32bit Applications?

查看:125
本文介绍了为什么(不正确)使用ref myarray [0]来传递数组,但是只能在32位应用程序中使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经在某些互操作中做过一些愚蠢的事情(使用 DllImport ),但是它仍然可以在32位计算机上工作。

I did something foolish in some interop (using DllImport) at one point, but it still worked on 32 bit machines.

在导致方法1行为不同的64位应用程序上做了什么不同(以及为什么?)?

What is done differently (and why?) on a 64 bit application that causes Method 1 to behave differently ?

方法1(错误方式):

ref byte param   //Signature of `DllImport`ed function
ref myarray[0]   //Parameter passed in when calling function

方法2(正确方法):

byte[] param   //Signature of `DllImport`ed function  
myarray        //Parameter passed in when calling function  

无论哪种方式,导入函数最终看到的地址都应该是第一个数组元素的地址。由于将在内存中连续分配数组,因此我期望这两个方法是等效的。

Either way, the address the imported function eventually sees should be whatever the address of the first array element is. Since the array will be allocated contiguously in memory, my expectation was that these two methods would be equivalent.

应用程序针对CPU类型 Any进行了编译。在32位计算机上,方法1和方法2都可以正常工作,并且表现出预期的效果。
在64位计算机上,仅方法2正常工作。使用方法1,应用程序只能看到数组的第一个元素。数组的其余部分被清零。

Application was compiled for CPU type "Any." On a 32bit machine, Method 1 and Method 2 both worked, and behaved as expected. On a 64bit machine, only method 2 worked correctly. With Method 1, the application was only able to see the first element of the array. The rest of the array was zeroed out.

推荐答案

在调用函数之前,64位框架可能已复制到某些临时存储空间。 ,然后在函数完成后将该数据复制回原始的 ref参数。

The 64 bit Framework might be copying to some temporary storage space before the function call, then copying that data back out to the original 'ref' parameter after the function is finished.

这篇关于为什么(不正确)使用ref myarray [0]来传递数组,但是只能在32位应用程序中使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆