c#如何使用新版本的助手API

c#如何使用新版本的助手API,第1张

概述c#如何使用新版本的助手API

由于Osversion现在不可靠,因为windows 10已经发布(这个函数报告windows 10的windows 10),我试图在我的C#应用​​程序中使用新的版本帮助器API函数。 他们在这里 。

我很抱歉,如果这只是我的DLL导入问题,但这里是我试图拉入这些新的方法来正确检测 *** 作系统。

[Dllimport("kernel32.dll",CharSet = CharSet.auto)] public static extern bool Iswindows7OrGreater(); [Dllimport("kernel32.dll",CharSet = CharSet.auto)] public static extern bool Iswindows8OrGreater(); [Dllimport("kernel32.dll",CharSet = CharSet.auto)] public static extern bool Iswindows8Point1OrGreater(); [Dllimport("kernel32.dll",CharSet = CharSet.auto)] public static extern bool Iswindows10OrGreater();

每当我调用这些方法,我得到:

exception是:EntryPointNotFoundException – 无法在DLL'kernel32.dll'中find名为“Iswindows7OrGreater”的入口点。

符号链接.net 4.5应用程序将无法正常工作

C# – ScheduledTasks类 – 如何指定本地主机?

如何检查.NET框架是否安装

有没有一种方法来静默或自动安装.NET?

沟通策略:linux到.Net over SSL

难道我做错了什么? 有人有主意吗? 谢谢你的帮助!

编辑:请参阅接受的答案,并看看这个代码项目移植到C#这些方法的良好开端。

我可以在windows上加载一个32位DLL到64位进程吗?

桌面通知程序没有可见的forms给用户

在特定的桌面上启动进程

什么是.NET平台文件扩展名?

显示多个可移动驱动器窗口的C#代码8

不幸的是,这比这更复杂一点。 “函数”实际上是在VersionHelpers.h定义的宏。

如果你想到了,这是唯一的方法 – 他们不能追溯功能添加到较旧的windows版本。

您必须将宏移植到C#。

您的代码必须包含一个Manifest

<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1"> <application> <!-- A List of all windows versions that this application is designed to work with. windows will automatically select the most compatible environment.--> <!-- windows 10 --> <supportedOS ID="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}"/> <!-- windows 8.1 --> <supportedOS ID="{1f676c76-80e1-4239-95bb-83d0f6d0da78}"/> <!-- windows Vista --> <supportedOS ID="{e2011457-1546-43c5-a5fe-008deee3d3f0}"/> <!-- windows 7 --> <supportedOS ID="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/> <!-- windows 8 --> <supportedOS ID="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}"/> <!-- If your application is designed to work with windows 7,uncomment the following supportedOS node--> <!--<supportedOS ID="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>--> </application> </compatibility>

这告诉 *** 作系统你的代码支持更新的 *** 作系统

使用下面的类

using System; using System.Runtime.InteropServices; namespace VersionHelper { public static class VersionHelper { const byte VER_EQUAL = 1; const byte VER_GREATER = 2; const byte VER_GREATER_EQUAL = 3; const byte VER_LESS = 4; const byte VER_LESS_EQUAL = 5; const byte VER_AND = 6; const byte VER_OR = 7; const byte VER_CONDITION_MASK = 7; const byte VER_NUM_BITS_PER_CONDITION_MASK = 3; // // RtlVerifyVersionInfo() type mask bits // const uint VER_MInorVERSION = 0x0000001; const uint VER_MAJORVERSION = 0x0000002; const uint VER_BUILDNUMBER = 0x0000004; const uint VER_PLATFORMID = 0x0000008; const uint VER_SERVICEPACKMInor = 0x0000010; const uint VER_SERVICEPACKMAJOR = 0x0000020; const uint VER_SUITEname = 0x0000040; const uint VER_PRODUCT_TYPE = 0x0000080; // wProductType // Any additional information about the system.This member can be one of the following values. const byte VER_NT_DOMAIN_CONTRolLER = 0x0000002; const byte VER_NT_SERVER = 0x0000003; const byte VER_NT_WORKSTATION = 0x0000001; // // _WIN32_WINNT version constants // const ushort _WIN32_WINNT_NT4 = 0x0400; const ushort _WIN32_WINNT_WIN2K = 0x0500; const ushort _WIN32_WINNT_WINXP = 0x0501; const ushort _WIN32_WINNT_WS03 = 0x0502; const ushort _WIN32_WINNT_WIN6 = 0x0600; const ushort _WIN32_WINNT_VISTA = 0x0600; const ushort _WIN32_WINNT_WS08 = 0x0600; const ushort _WIN32_WINNT_LONGHORN = 0x0600; const ushort _WIN32_WINNT_WIN7 = 0x0601; const ushort _WIN32_WINNT_WIN8 = 0x0602; const ushort _WIN32_WINNT_WINBLUE = 0x0603; const ushort _WIN32_WINNT_WINTHRESHolD = 0x0A00; /* ABRACADABRA_THRESHolD*/ const ushort _WIN32_WINNT_WIN10 = 0x0A00; /* ABRACADABRA_THRESHolD*/ const bool FALSE = false; static byte LOBYTE(ushort w) { return ((byte)(w & 0xff)); } static byte HIBYTE(ushort w) { return ((byte)(w >> 8 & 0xff)); } [Dllimport("kernel32.dll")] static extern ulong VerSetConditionMask(ulong ConditionMask,uint TypeMask,byte Condition ); [Dllimport("kernel32.dll")] static extern bool VerifyVersionInfoW(ref OsveRSIONINFOEXW lpVersioninformation,uint DWTypeMask,ulong DWlConditionMask); [StructLayout(LayoutKind.Sequential,CharSet = CharSet.Unicode)] struct OsveRSIONINFOEXW { public int DWOsversionInfoSize; public int DWMajorVersion; public int DWMinorVersion; public int DWBuildNumber; public int DWPlatformID; [MarshalAs(UnmanagedType.ByValTStr,SizeConst = 128)] public string szCSDVersion; public UInt16 wServicePackMajor; public UInt16 wServicePackMinor; public UInt16 wSuiteMask; public byte wProductType; public byte wReserved; } public static bool IswindowsVersionorGreater(ushort wMajorVersion,ushort wMinorVersion,ushort wServicePackMajor) { var osvi = new OsveRSIONINFOEXW { DWOsversionInfoSize = Marshal.SizeOf(typeof(OsveRSIONINFOEXW)) }; var DWlConditionMask = VerSetConditionMask( VerSetConditionMask( VerSetConditionMask( 0,VER_MAJORVERSION,VER_GREATER_EQUAL),VER_MInorVERSION,VER_SERVICEPACKMAJOR,VER_GREATER_EQUAL); osvi.DWMajorVersion = wMajorVersion; osvi.DWMinorVersion = wMinorVersion; osvi.wServicePackMajor = wServicePackMajor; return VerifyVersionInfoW(ref osvi,VER_MAJORVERSION | VER_MInorVERSION | VER_SERVICEPACKMAJOR,DWlConditionMask) != FALSE; } public static bool IswindowsXPOrGreater() { return IswindowsVersionorGreater(HIBYTE(_WIN32_WINNT_WINXP),LOBYTE(_WIN32_WINNT_WINXP),0); } public static bool IswindowsXPSP1OrGreater() { return IswindowsVersionorGreater(HIBYTE(_WIN32_WINNT_WINXP),1); } public static bool IswindowsXPSP2OrGreater() { return IswindowsVersionorGreater(HIBYTE(_WIN32_WINNT_WINXP),2); } public static bool IswindowsXPSP3OrGreater() { return IswindowsVersionorGreater(HIBYTE(_WIN32_WINNT_WINXP),3); } public static bool IswindowsVistaOrGreater() { return IswindowsVersionorGreater(HIBYTE(_WIN32_WINNT_VISTA),LOBYTE(_WIN32_WINNT_VISTA),0); } public static bool IswindowsVistaSP1OrGreater() { return IswindowsVersionorGreater(HIBYTE(_WIN32_WINNT_VISTA),1); } public static bool IswindowsVistaSP2OrGreater() { return IswindowsVersionorGreater(HIBYTE(_WIN32_WINNT_VISTA),2); } public static bool Iswindows7OrGreater() { return IswindowsVersionorGreater(HIBYTE(_WIN32_WINNT_WIN7),LOBYTE(_WIN32_WINNT_WIN7),0); } public static bool Iswindows7SP1OrGreater() { return IswindowsVersionorGreater(HIBYTE(_WIN32_WINNT_WIN7),1); } public static bool Iswindows8OrGreater() { return IswindowsVersionorGreater(HIBYTE(_WIN32_WINNT_WIN8),LOBYTE(_WIN32_WINNT_WIN8),0); } public static bool Iswindows8Point1OrGreater() { return IswindowsVersionorGreater(HIBYTE(_WIN32_WINNT_WINBLUE),LOBYTE(_WIN32_WINNT_WINBLUE),0); } public static bool IswindowsThresholdOrGreater() { return IswindowsVersionorGreater(HIBYTE(_WIN32_WINNT_WINTHRESHolD),LOBYTE(_WIN32_WINNT_WINTHRESHolD),0); } public static bool Iswindows10OrGreater() { return IswindowsVersionorGreater(HIBYTE(_WIN32_WINNT_WINTHRESHolD),0); } public static bool Iswindowsserver() { var osvi = new OsveRSIONINFOEXW { DWOsversionInfoSize = Marshal.SizeOf(typeof(OsveRSIONINFOEXW)),wProductType = VER_NT_WORKSTATION }; var DWlConditionMask = VerSetConditionMask(0,VER_PRODUCT_TYPE,VER_EQUAL); return !VerifyVersionInfoW( ref osvi,DWlConditionMask); } } }

VersionHelpers函数不会在任何dll中导出,而是在VersionHelpers.h文件中定义的,为了利用C#代码中的功能,您可以从头文件复制功能,

导入这两个功能:

[Dllimport("kernel32.dll")] static extern ulong VerSetConditionMask(ulong DWlConditionMask,uint DWTypeBitMask,byte DWConditionMask); [Dllimport("kernel32.dll")] static extern bool VerifyVersionInfo([In] ref OsversionInfoEx lpVersionInfo,ulong DWlConditionMask);

定义以下结构:

[StructLayout(LayoutKind.Sequential)] struct OsversionInfoEx { public uint OsversionInfoSize; public uint MajorVersion; public uint MinorVersion; public uint BuildNumber; public uint PlatformID; [MarshalAs(UnmanagedType.ByValTStr,SizeConst = 128)] public string CSDVersion; public ushort ServicePackMajor; public ushort ServicePackMinor; public ushort SuiteMask; public byte ProductType; public byte Reserved; }

然后使用以下功能:

static bool IswindowsVersionorGreater(uint majorVersion,uint minorVersion,ushort servicePackMajor) { OsversionInfoEx osvi = new OsversionInfoEx(); osvi.OsversionInfoSize = (uint)Marshal.SizeOf(osvi); osvi.MajorVersion = majorVersion; osvi.MinorVersion = minorVersion; osvi.ServicePackMajor = servicePackMajor; // These constants initialized with corresponding deFinitions in // winnt.h (part of windows SDK) const uint VER_MInorVERSION = 0x0000001; const uint VER_MAJORVERSION = 0x0000002; const uint VER_SERVICEPACKMAJOR = 0x0000020; const byte VER_GREATER_EQUAL = 3; ulong versionorGreaterMask = VerSetConditionMask( VerSetConditionMask( VerSetConditionMask( 0,VER_GREATER_EQUAL); uint versionorGreaterTypeMask = VER_MAJORVERSION |VER_MInorVERSION | VER_SERVICEPACKMAJOR; return VerifyVersionInfo(ref osvi,versionorGreaterTypeMask,versionorGreaterMask);

}

总结

以上是内存溢出为你收集整理的c#如何使用新版本的助手API全部内容,希望文章能够帮你解决c#如何使用新版本的助手API所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1293827.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-10
下一篇 2022-06-10

发表评论

登录后才能评论

评论列表(0条)

保存