Proficient Visual Basic .NET (Favorites)

zhaozj2021-02-16  91

Proficient Visual Basic .NET

Release Date: 7/12/2004

| Update Date: 7/12/2004

Do you have problems or puzzles in the Internet or Windows-based development? At this point, you can help Dr. Gui (Drgui@microsoft.com); he answers your questions online twice every month. Although the busy work arrangement made him unable to reply to all the questions, he will answer more questions as much as possible. If you just select your question, then doctors will send you a GUI Dr. T-shirt!

Summary: Dr. Gui tells Visual Basic .NET developers describe how to determine the exact version of the operating system of the running program.

How to determine the operating system version?

Dr. Gui!

I have just started development under Visual Basic .NET, but I have used many years Visual Basic 6.0. I am not a professional developer, but I am very fortunate to get this opportunity to touch the fish here.

Now, my friend (he is a profession) and I have been hard in a question. We work on a MUD server but still develop in Visual Basic 6.0. However, some components (including a development program I are being responsible) basically rely on how to return information about the current machine to control how the application runs in a way that the operating system is dependent.

For example, if the user runs a program called the devTools?, The program will operate in the client mode in Windows 2000 PROFESSIONAL or Windows XP (HOME or PROFESSIONAL). But if it is running on any 2000 or .NET Server version, it will be operated in server mode.

Basically, the system is working automatically, but it will not be possible because we cannot determine the exact version of the operating system. Of course, we can directly determine the Windows version number, but we lack a simple way to return different kits masks with the OsversionInfoEx API. If you can provide any help, we will be grateful!

Thank you,

Brandon Dobbie

Dr. Gui Reply:

There is no doubt that Dr. Gui can provide a solution for your questions. You have provided a lot of clues for Ol 'DOC, which means that he doesn't have to do how much work can charge your full charge.

By the way, although there are many good reasons to know which operating system is running in the end of your program, our Ph.D. is not blindly changing the operating mode based on the operating system. For example, some people may run a server operating system on a portable computer (according to Dr. Gui, many people in Microsoft are like this), but they also want to run your software as the client. Our doctors can handle this issue by command line parameters. (Of course, this can be performed by default as you recommend, provided that you can rewrite the command line parameters.)

However, you should still check it to make sure you are running server functions on the server operating system. So, now let's talk about how to do it.

The .NET framework provides a class named OperatingSystem, which will provide some (but not all, such as what you see) about the current operating system. We can find this object to find the currently operational platform and the version of the platform, you can also use it to complete the work of the lookup system version. We can also use existing knowledge base (KB) articles to learn about the basic knowledge of this object: how to: determine windows version by using visual basic .net. In fact, we can start with the cutting and paste work we do. (Remnounced, Ph.D. is very willing to have less work.) However, although this is useful, it does not provide all the information we need. In order to get all information about the OsversionInfoEx structure you are used, we must use the OsversionInfoEx structure. This also means we need some Win32 API calls. So, all API skills you have mastered in Microsoft Visual Basic 6.0 is still useful.

In the Visual Basic .NET and .Net framework, you can call Microsoft Windows APIs from the managed code with a fairly transparent package and the InteroP mechanism. What you need to do is to create an API declaration using the structural definition associated with the API declaration and make sure that the notification .NET runtime is expected to handle these structures. (Please note that it is not irrelevant correctly; in order to help debug and test, you may have to write C or C programs to display data in the desired structure; then call the program from the .NET framework. This Help you verify that all data is passed correctly.)

First, we must declare the API function you need to use: getVersionex.

There are two ways to declare your API call:

1. Use the very similar statements that are extremely similar to Visual Basic 6.0. 2. Use the DLLIMPORT attribute (it applies to any language).

From simplering, I am willing to use the Declare statement, because everyone knows and likes this method. Let's take a look at the declaration of getversionex:

Public Declare Auto Function GetversionEx lib "kernel32.dll" _ (unmanagedtype.struct)> byref osinfo as osversioninfoEx) _ as int32

Most of the content should be familiar, but we should notice that there are some new things. The first is the auto keyword in the statement. We need to tell the running library that should call the Unicode version or the ANSI version of the API. We can explicitly declare which format to call which format, you can also use the Auto keyword to automatically select the running library. If you use the Auto keyword, use the Unicode version API on Windows NT, Windows 2000, and Windows XP platforms, using an ANSI version on the Windows 98 and Windows ME platforms.

Now we have declare the API, then you need to define the OsversionInfoEx structure:

_ Public Structure OSVERSIONINFOEX Public dwOSVersionInfoSize As Int32 Public dwMajorVersion As Int32 Public dwMinorVersion As Int32 Public dwBuildNumber As Int32 Public dwPlatformId As Int32 Public szCSDVersion As String Public wServicePackMajor As Int16 Public wServicePackMinor As Int16 Public wSuiteMask As Int16 Public wProductType As Byte Public wReserved As Byte End Structure we have to let the runtime know which character set we want to use, and How to use the structlayout property to discharge the structure into memory. In this example, we will tell the compiler to discharge the structure into memory in order. The most interesting part in this statement is the fixed string included in the middle:

public szcsdversion as string

The VBFixedString property will notify the run library: We will use a fixed length string of 128 characters long. The Marshalas property notifies the way the run library processes the string at runtime. We also want to tell the compiler that the length of the string is 128 characters (SIZECONST: = 128), and the string should be treated as a character set (Byvaltstr) based on the platform.

After handling these statements, we can call GetVersionex to retrieve information and then check the return value we need. This structure contains multiple members, which have many different possible values, so we need to see the online help of OsversionEx to view these values.

Now let's take a look at the full example of this code. To create a project for testing the code, perform the following steps:

1. Start a new Visual Basic .NET console application.

2. The following code into the code window of the default module: Option Strict On Imports System.Environment Imports System.Runtime.InteropServices Module Module1 'API declarations Public Declare Auto Function GetVersionEx Lib "kernel32.dll" _ ( ByRef osinfo As _ OSVERSIONINFOEX) As Int32 'Structure definition _ Public Structure OSVERSIONINFOEX Public dwOSVersionInfoSize As Int32 Public dwMajorVersion As Int32 Public dwMinorVersion As Int32 Public dwBuildNumber As Int32 Public dwPlatformId As Int32 _ Public szCSDVersion As String Public wServicePackMajor As Int16 Public wServicePackMinor As Int16 Public wSuiteMask As Int16 Public wProductType As Byte Public wReserved As Byte End Structure ' Useful constants public const32 = & h1 public const ver_nt_domain_controller as int32 = & h2 public const32 = & h3 public cons t VER_WORKSTATION_NT As Int32 = & H40000000 & 'Microsoft Small Business Server Public Const VER_SUITE_SMALLBUSINESS As Int32 = & H1 &' Win2k Adv Server or .Net Enterprise Server Public Const VER_SUITE_ENTERPRISE As Int32 = & H2 & 'Terminal Services is installed. Public Const VER_SUITE_TERMINAL As Int32 = & H10 &' Win2k Datacenter Public const ver_suite_datacenter as int32 = & h80 & 'Terminal Server in Remote Admin Mode public const ver_suite_singleuserts as int32 = & h100 & public const32 = & h200 &'

Microsoft .Net webserver installed Public Const VER_SUITE_BLADE As Int32 = & H400 Sub Main () Console.WriteLine (getVersion ()) End Sub Public Function getVersion () As String Dim osInfo As OperatingSystem osInfo = OSVersion With osInfo Select Case .Platform Case .Platform. Win32windows select case (.version.minor) case 0 getversion = "windows 95" case 10 if .version.revision.tostring () = _ "2222a" Then getversion = _ "Windows 98 second edition" else getversion = "Windows 98" End if case 90 getversion = "windows me" end select custom .platform.win32nd select code (.version.major) case 4 getversion = "windows nt 4.0" case 5 if .version.minor = 0 Then getversion = "Windows 2000" Else getVersion = "Windows XP" End If End Select 'Since this is an NT / 2k / XP product,' Check the product type getVersion = "" getVersion = getProductType (osInfo) Case Else getVersion = "Failed to get version information "End Select End with End Function Public Function GetProductType (Byval Osinfo As OperatingSystem) _ As String Dim osverinfo As OSVERSIONINFOEX Dim result As Int32 osverinfo.dwOSVersionInfoSize = Marshal.SizeOf (osverinfo) result = GetVersionEx (osverinfo) If result = 0 Then 'There was a failure in the api call.' There should be error handling here! Console.WriteLine ( "Error calling GetVersionEx!") Return "Failed" End If If osinfo.Version.Major = 4 Then 'Windows NT If osverinfo.wProductType = VER_NT_WORKSTATION Then Return "Workstation" ElseIf osverinfo.wProductType = VER_NT_SERVER Then Return "Server "

Else Return "Unknown" End If ElseIf osinfo.Version.Major = 5 Then 'Windows 2000 or XP If osverinfo.wProductType = VER_NT_WORKSTATION Then' This is a workstation verion If (osverinfo.wSuiteMask And VER_SUITE_PERSONAL) = _ VER_SUITE_PERSONAL Then Return "Personal" Else Return "Professional" End If ElseIf osverinfo.wProductType = VER_NT_SERVER Then If osinfo.Version.Minor = 0 Then 'This is Win2k If (osverinfo.wSuiteMask And _ VER_SUITE_DATACENTER) = _ VER_SUITE_DATACENTER Then Return "Datacenter Server" ElseIf (osverinfo.wSuiteMask And _ VER_SUITE_ENTERPRISE) = _ VER_SUITE_ENTERPRISE Then Return "Advanced Server" Else Return "Server" End If Else 'This is XP / .NET If (osverinfo.wSuiteMask And _ VER_SUITE_DATACENTER) = _ VER_SUITE_DATACENTER Then Return "Windows.Net Datacenter Server" ElseIf (OSVERINFO.WSUITEMASK and _ VER_SUITE_ENTERPRISE) = _ Ver_suite_Enterprise Then Return "Windows.Net Enterprise Server" Else Return "Server" end if end if end if endiff i we get here, We're Lost Re TURN "failed" End Function End Module 3. Establish an application. 4. Run the application from the command line. Depending on the current operating system, you will see information that reflects the operating system. For example, because I am currently running Windows XP Professional, you will see the following information: c: // drgui / my documents / osverinfo / bin> OsverInfo.exe Windows XP Professional If you want to use C # or other languages, this action is required. Or need further information, see Interoperating with Unmanaged Code.

Other useful links:

OsversesinfoEx structure

Declaration statement

Dllimport attribute

Acknowledgments!

Dr. Gui is here to thank the Experts, including Gray McDonald and its specializes in Maura Baughman and Sharon Watts. If Dr. Gui doesn't have help when answering questions, the problem will not be as satisfactory.

Dr. GUI question and answer

The famous problem solving expert "Dr. Gui" is very happy to provide encyclopedia knowledge in Internet and Windows development, so that developers from all over the world will benefit from it. If you have a problem that you can't resolve, please send your questions to drgui@microsoft.com. Although the busy work arrangement made him unable to reply to all the questions, he will answer more questions as much as possible. If you just select your question, then doctors will send you a GUI Dr. T-shirt! (Please note: The problem may be organized to ensure the correct syntax is correct, the logic is clear.) Does Dr. Gui are not enough?

For other knowledge about doctors, please read the MSDN library monthly released Dr. Gui .Net column articles.

Go to the original English page

(Source: http://www.microsoft.com/china/msdn/library/LANGTOOL/VBNET/SDaskgui06042002.mspx)

转载请注明原文地址:https://www.9cbs.com/read-12710.html

New Post(0)