c#-如何解决此“ P / Invoke声明应可移植”的问题?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c#-如何解决此“ P / Invoke声明应可移植”的问题?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1632字,纯文字阅读大概需要3分钟。
内容图文
![c#-如何解决此“ P / Invoke声明应可移植”的问题?](/upload/InfoBanner/zyjiaocheng/675/dd4678267b0845cb93725c3abc41ee4e.jpg)
这段代码:
[DllImport("shell32", CharSet=CharSet.Unicode)]
private static extern int SHGetFileInfo(string pszPath, uint dwFileAttributes, out SHFILEINFO psfi, uint cbFileInfo, uint flags);
导致以下代码分析问题:
CA1901 P/Invoke declarations should be portable
As it is declared in your code, the return type of P/Invoke
‘IconUtil.SHGetFileInfo(string, uint, out IconUtil.SHFILEINFO, uint,
uint)’ will be 4 bytes wide on 64-bit platforms. This is not correct,
as the actual native declaration of this API indicates it should be 8
bytes wide on 64-bit platforms. Consult the MSDN Platform SDK
documentation for help determining what data type should be used
instead of ‘int’.
我应该做些什么?我尝试过“咨询MSDN”,但是我不确定这个问题到底意味着什么.
我也在同一行得到这个:
CA1060 Move P/Invokes to NativeMethods class
Because it is a P/Invoke method, ‘IconUtil.SHGetFileInfo(string, uint,
out IconUtil.SHFILEINFO, uint, uint)’ should be defined in a class
named NativeMethods, SafeNativeMethods, or UnsafeNativeMethods.
解决方法:
警告所指的MSDN页面是本机函数SHGetFileInfo的文档.它的签名为:
DWORD_PTR SHGetFileInfo(
__in LPCTSTR pszPath,
DWORD dwFileAttributes,
__inout SHFILEINFO *psfi,
UINT cbFileInfo,
UINT uFlags
);
因此,第一个警告是指DWORD_PTR是指针大小的无符号整数的事实.这意味着您应该使用UIntPtr.
下一跳是SHFILEINFO结构指示为__inout.这意味着您需要通过引用传递它.
因此,p / invoke声明应为:
[DllImport("shell32", CharSet=CharSet.Unicode)]
private static extern UIntPtr SHGetFileInfo(
string pszPath,
uint dwFileAttributes,
ref SHFILEINFO psfi,
uint cbFileInfo,
uint flags
);
最终警告很容易解决.只需将p / invoke声明放在名为NativeMethods的类中.
内容总结
以上是互联网集市为您收集整理的c#-如何解决此“ P / Invoke声明应可移植”的问题?全部内容,希望文章能够帮你解决c#-如何解决此“ P / Invoke声明应可移植”的问题?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。