.NET Framework 4.5中P/Invoke中的short类型的参数问题 [英] Issue with Parameter of type short in P/Invoke with .NET Framework 4.5

查看:68
本文介绍了.NET Framework 4.5中P/Invoke中的short类型的参数问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在仅具有.NET Framework 4.0的计算机上,我使用mscs-10.0编译了一个64位C#程序集,该程序具有以下P/Invoke

On a machine with just .NET Framework 4.0, I have an 64-bit C# Assembly compiled with mscs-10.0 that has following P/Invoke

[DllImport(VisaDriverFileName64, EntryPoint = "viSetAttribute", CallingConvention = CallingConvention.StdCall)|DllImport(VisaDriverFileName64, EntryPoint = "viSetAttribute", CallingConvention = CallingConvention.StdCall)]
private static extern ViStatus _SetAttributeInt16_64(ViObject vi, ViAttr attrName, short attrState); 

此P/Invoke调用以下C ++入口点

This P/Invoke calls into the following C++ entrypoint

ViStatus _VI_FUNC  viSetAttribute  (ViObject vi, ViAttr attrName, unsigned __int64 attrState);

理想情况下,DllImport签名应具有 ulong ,而不是.但这在仅具有.NET Framework 4.0的系统上非常有效,如果为参数attrState传递了正确的值(1或4),则C ++入口点下的任何其他值都会引发异常,表示不支持的值.

Ideally the DllImport Signature should have ulong instead of short. But this work perfectly on system with only .NET Framework 4.0, if correct value (1 or 4) is passed for the parameter attrState, for any other values underlying C++ entrypoint throws exception saying not supported value.

现在,在同一系统上安装.NET Framework 4.5之后,即使我们从.NET层传递了正确的值(1或4),上述C ++入口点也会引发异常,指出参数attrState不受支持的值.我们知道解决方法,但是 我想知道的是为什么它仅在安装.NET Framework 4.5后才会失败,并且由于调用失败而在.NET Framework 4.5中进行了哪些更改?

Now after installing .NET Framework 4.5 on the same system, above C++ entrypoints throws exception saying not supported value for parameter attrState, even if we pass the correct values (1 or 4) from .NET Layer. We know the fix for this, however what I would like to know is why is it failing only after installing .NET Framework 4.5, and what got changed in .NET Framework 4.5 due to which this call fails?

我对正在发生的事情有一点头绪,但是我不知道为什么会这样.

I have a little clue on what is happening, but what I dont is why is this happening.

对于attrState = 1

For attrState = 1

  • 仅使用.NET Framework 4.0,C ++入口点将参数attrState读取为0x0000000000000001
  • 使用.NET Framework 4.0,并且如果我们安装.NET Framework 4.5,则C ++入口点将参数attrState读取为0x0000 2de11a3f 0001.每次突出显示的中间字节都会得到不同的垃圾值.
  • With just .NET Framework 4.0, C++ entrypoint reads the parameter attrState as 0x0000000000000001
  • With .NET Framework 4.0 and if we install .NET Framework 4.5, C++ entrypoint reads the parameter attrState as 0x00002de11a3f0001. I am getting different garbage value for highlighted middle bytes everytime.

我想知道,为什么只有在.NET Framework 4.0上安装了.NET Framework 4.5时才会发生这种情况.

I want to know, why is this happening only when I have .NET Framework 4.5 installed on top of .NET Framework 4.0. 

谢谢,
修罗

Thanks,
Shura

舒拉

推荐答案

一个可能会帮助您的提示是,在安装4.5时,您会获得4.0的每个修补程序,支持补丁等. 4.5对您的4.0 dll进行了就地升级,因此它确实可以改变很多事情.我被某些修复"工具咬伤了.在4.5滴 在我们必须编写变通办法的4.0应用程序中.虽然,我通过反射调用内部函数,所以它们更改了内部方法签名的事实并不是他们的错.

One clue that might help you is that when you install 4.5, you get every hotfix, support patch, etc. for 4.0. 4.5 does an in-place upgrade of your 4.0 dlls, so it really can change a fair amount of things. I got bitten by some of the "fixes" in the 4.5 drop in our 4.0 app that I had to write workarounds for. Albeit, I was calling internal functions with reflection, so the fact that they changed their internal method signatures wasn't their fault.

无论如何,请尝试使用 System.Int64

Anyway, instead of short, try System.Int64

因为您使用的是64位值.

Since you are using a 64 bit value.


这篇关于.NET Framework 4.5中P/Invoke中的short类型的参数问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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