开发者

Why does (incorrectly) using ref myarray[0] to pass in an array work, but only in 32bit Applications?

开发者 https://www.devze.com 2023-01-24 16:19 出处:网络
I did something foolish in some interop (using DllImport) at one point, but it still worked on 32 b开发者_如何学编程it machines.

I did something foolish in some interop (using DllImport) at one point, but it still worked on 32 b开发者_如何学编程it machines.

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

Method 1 (wrong way):

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

Method 2 (right way):

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.

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.


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.


My guess would be that you're loading different versions of the dll itself... a 64-bit application can't load the same 32-bit version of the dll that was loaded when running as x86, so you have to be pointing to a different dll. The 64-bit version of the dll must be handling the parameters in a different manner.

What DLL are you loading? Would it be something handled by WinSxs?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号