Using the. NET framework classes instead of API calls (i)

Source: Internet
Author: User
Tags filter comments continue functions object model requires tostring win32
Using the. NET Framework classes to override API calls
upgrade to Microsoft. NET
Ken Getz
MCW Technologies
February 2002

Summary:By learning some of the specific and useful classes in the Microsoft. NET framework, you can reduce your reliance on WIN32 API calls. Each class discussed in this article can replace one or more Win32 API calls, whereas in Microsoft Visual Basic 6.0 You must invoke one or more Win32 APIs to accomplish the same task.

Goal

    • Find a specific substitute for an existing Win32 API call.
    • Learn about the Registry class.
    • Use the FileVersionInfo class.
    • Use environment information and system information.

Directory

    • Avoid using the Win32 API
    • Using the Registry
    • Use a common dialog box
    • Retrieving file version information
    • Retrieving environment information
    • Summarize
Avoid using the Win32 API
If you are a Microsoft Visual basic®6.0 developer, you cannot avoid invoking the Win32 API. The developer has too many tasks to complete, and Visual Basic does not provide any implementation methods. For example, in Visual Basic 6.0, you have difficulty completing the following tasks:
    • Determines the file version information.
    • Read and write at any location in the registry.
    • Identify specific folders for users, such as Microsoft Windows® favorites or Personal folders.
    • Retrieves a list of all available drives.
    • Locate the user's logon or computer name.
    • Retrieves a list of all open windows.

If you use only the tools provided in Visual Basic 6.0, you will not be able to resolve any of these issues. For each issue, developers need to use the Windows API. Many developers have found ways to accomplish these (and many other) tasks using the Windows API.

What is wrong with the Windows API?


Why not continue using the Windows APIs in the. NET environment? If you use the. NET platform invoke service (called "P/invoke"), you can certainly do so. From the Visual Basic Developer's perspective, calling the Windows APIs is no more difficult than using the Declare statement that they are familiar with. However, there are some serious drawbacks to using the Windows API in a. NET environment, and you may need to consider any possible measures to avoid these problems. For example:
    • The. NET common language runtime is not affected by the platform. When you use the Windows API call, you bind the code to a specific platform that writes code (that is, a particular version of Windows or Windows itself) relative to the other operating system. If necessary, you need to convert the code to another platform, and doing so requires modifying each line of code that uses API calls.
    • Calling the Windows API (or any unmanaged code in a DLL) from. NET is not as simple as in Visual Basic 6.0. For example, restrictions on how a structure works can make it difficult to pass a struct to an API call. In addition, the API declarations for Visual Basic 6.0 need to be changed due to changes in data types and more stringent type conversions.
    • Depending on the language, the techniques for using the Windows API (and often the external code used) are different. If you plan to work in a multiple. NET locale, you will need to master the different skills of each language.
    • Code that invokes the Windows API requires that the user who calls the code have the right to perform this operation. This affects the application's security protection scenarios, and you need to make early arrangements for this requirement.

This is a simple question: although you can continue to use the Windows APIs in Visual Basic. NET applications, you should normally find alternatives that are provided by the. NET Framework. Although the purpose of the. NET framework is not to prevent you from using Windows directly, the framework does provide a number of classes that can help you discard dependencies on Windows API calls.
It may be convenient to give a complete list of Win32 API calls and the appropriate methods (if any) to accomplish the same tasks in the. NET framework, although this is not covered in this article. In this article, you will learn about some of the specific and very useful classes provided by the. NET framework that can solve your problem. In each example, the classes discussed in this article can be used to override one or more Win32 API calls, whereas in Microsoft Visual Basic 6.0 You must invoke one or more Win32 APIs to accomplish the same task. using the Registry
If you are like most Visual Basic 6.0 developers, you will find the SaveSetting, GetSetting, Getallsetti that are built into Microsoft Visual Basic for Applications (VBA) NGS and DeleteSetting methods are useful, but they are likely to be exhausted by their limitations. All of these methods can only be used in the Registry's HKEY_CURRENT_USER\Software\VB and under the VBA program Settings. If you want to read or write registry keys or registry values elsewhere in the registry, you must use a complex API call or rely on someone else's code to handle the problem.
The. NET Framework provides a powerful class (Registry and RegistryKey) in the Microsoft.Win32 namespace, simplifying the use of the registry by eliminating the need for API calls!
As a demo, on the main form of the sample project, click Work with the Registry(Use the Registry) button. This form provides a list of all the registry values in the HKEY_LOCAL_MACHINE hive for the SOFTWARE\Microsoft\Windows\CurrentVersion\Run item. You can right-click any item in the list and choose to insert a new item, or edit or delete the selected item, as shown in Figure 1.
Tip: The sample form is also designed so that when you press Enter in the list box, you can edit the currently selected item. Press the delete key to delete the selected item and press the Insert key to add a new value. These items correspond to the items in the context menu of the list box.


Figure 1: Easily retrieve and modify information in the Windows registry using the Registry and RegistryKey classes
The. NET Framework provides two very useful classes that make it easy for you to use the Windows registry. The first class is Registry, which provides the fields that correspond to the fields of the standard Registry hive:
    • Classesroot (HKEY_CLASSES_ROOT)
    • Currentconfig (Hkey_current_config)
    • CurrentUser (HKEY_CURRENT_USER)
    • DynData (HKEY_DYN_DATA)
    • LocalMachine (HKEY_LOCAL_MACHINE)
    • PerformanceData (Hkey_performance_data)
    • Users (HKEY_USERS)

To use the Registry class, simply retrieve the reference for the desired hive. The Loadlist procedure for the sample form includes the following code to use the HKEY_LOCAL_MACHINE hive in the registry:
Imports Microsoft.win32dim reg as RegistryKey = Registry.localmachine

Another class is RegistryKey, which can do all the work. It provides a set of methods for using Registry. Table 1 lists all the useful methods for the RegistryKey class.
table 1:registrykey class method Method DescriptionCreateSubKey creates a new subkey or opens an existing subkey DeleteSubKey deletes the specified subkey. Deletesubkeytree deletes the subkey and all subkeys of the subkey recursively. DeleteValue removes the specified item value from the item. Getsubkeynames retrieves an array of strings containing all the subkey names. GetValue retrieves the specified value. GetValueNames retrieves a string array that contains the names of all item values associated with this item. OpenSubKey retrieves the specified subkey with optional write permissions. SetValue sets the specified value.
The RegistryKey class also provides the following three properties:
    • Name: Retrieves the names of the items.
    • Subkeycount: Retrieves the number of subkeys associated with the item.
    • Valuecount: Retrieves the number of item values associated with the item.

The Listload procedure for the sample form retrieves all the values in the requested item and adds the retrieved values to the form's list box:
Private Const Conregkey as String = _ "SOFTWARE\Microsoft\Windows\CurrentVersion\Run" Private Structure regdata public V    Alue As String public Data As String public Overrides _ Function ToString () As String return Me.Value End FunctionEnd structureprivate Sub listload ()Dim reg as RegistryKey = Registry.localmachineDim astrvalues () As String Dim strvalue As String Dim Rd As Regdata ' clears the existing item in the list box. Lstitems.beginupdate () LstItems.Items.Clear () opens the registry key, and then uses the value of the item to load the list box.reg = Reg. OpenSubKey (Conregkey)astrvalues = Reg. GetValueNames ()For each strvalue in Astrvalues Rd. Value = strvalue.tostringRd. Data = Reg. GetValue (strvalue)LstItems.Items.Add (RD) Next lstitems.endupdate () End Sub

To edit the values in the sample form or add new values, you need to run the following code:
Private Sub Addoredit (_ ByVal Rd as Regdata, _ ByVal Mode as Frmaddvalue.accessmode) Dim reg as RegistryKey = Regi Stry. LocalMachine Dim frm as New frmaddvalue (Mode) frm. keyname = Rd. Value frm. KeyData = Rd. Data If frm. ShowDialog () = DialogResult.OK Then If frm. KeyName <> String.Empty Then reg = Reg. OpenSubKey (Conregkey, True)  Reg. SetValue (frm. KeyName, frm. KeyData) listload () End If End IfEnd Sub

This code will open the registry key again, this time requesting permission to write the item value (this request is issued by the second parameter of OpenSubKey). The code then calls the SetValue method, passing the item name and item value in the dialog box form shown in Figure 1. To simplify your work, you can use the SetValue method to add new values or modify existing values. If the value of the item does not exist, the SetValue method adds an item value.
To delete an item value, the sample form invokes the following code:
Private Sub DeleteKey (ByVal Rd as Regdata) Dim StrText as String Dim reg as RegistryKey = Registry.localmachine I    F lstitems.selectedindex =-1 Then Exit Sub end If ' Deletes the selected item. StrText = String.Format (_ "Are you sure your want to delete" "{0}" "?", _ Rd. Value) If MessageBox.Show (StrText, _ "Delete Registry Value", _ Messageboxbuttons.yesno, _ messageboxicon.q uestion) = Dialogresult.yes Then ' Opens the entry to allow writing.reg = Reg. OpenSubKey (Conregkey, True)Reg. DeleteValue (Rd. Value)' Reload the list box. Listload () End IfEnd Sub

This code opens the item and requests a write operation on it, and then calls the DeleteValue method to delete the selected value.
With the information provided by the sample form and the documentation that came with the. NET framework, you can easily complete any tasks related to the registry without using the Windows API. This is a simple object model, but it provides functionality that is more powerful than the functionality previously owned by the Visual Basic 6.0 developers.
Tip: If you have the necessary permissions, you can also use the registry on the remote computer. Instead of simply using one of the Registry class properties, you can call the Registrykey.openremotebasekey method to retrieve a basic item on another computer to represent the Registry hive on your own computer.
Use a common dialog box
Windows provides a common set of dialog boxes that enable developers to easily request user information. You must have seen and used common dialog boxes for opening and saving files, colors, printers, and font settings. Visual Basic 6.0 developers have two choices when using these dialog boxes. They can:
    • Using a Microsoft ActiveX® control, the control provides a simple object model that contains a common dialog box, but because the control has several different versions and the underlying DLL is different, the control has a serious deployment problem. (For many Visual Basic 6.0 developers, this problem is the most lethal DLL problem.) )
    • Use the Windows API directly to send messages and provide callback functionality to manage each common dialog box.

None of the two solutions is perfect, and since the. NET framework has been updated, neither solution is currently required. When you view the System.Windows.Forms namespace, you will find ColorDialog, FileDialog, FontDialog, and PrintDialog classes. These classes are integrated into the framework (that is, neither API calls nor ActiveX controls are required), which makes it easy to incorporate these standard features into your application.
Each class provides a series of properties that you can use to set these properties before the dialog box is displayed by using the ShowDialog method of the class. This article will not discuss each class in detail, but use the FileDialog class in the sample project so that you can select the file. If you selected on the main form File Version Info(File version information) button, you can use the Select a File(select File) button to display File Open(Open File) dialog box, as shown in Figure 2.

Figure 2: Using the FileDialog class to display Windows common dialog boxes
Although the FileDialog class is not as flexible as using the Windows API directly, it provides a number of properties that you can use to control the actions of the dialog box. You can determine the source of the file, select one or more files, and retrieve the selected file name. Table 2 lists some of the properties and methods of the FileDialog class that might be used.
table 2:filedialog Properties and methods of objects Properties/Methods DescriptionAddExtension indicates whether the dialog box automatically adds an extension to the file when the user omits the extension. Checkfileexists indicates whether the dialog box displays a warning message when the user-specified filename does not exist. Checkpathexists indicates whether the dialog box displays a warning message when the user-specified path does not exist. Defaultext The default file name extension. Dereferencelinks indicates whether the dialog box returns the location of the file referenced by the shortcut, or whether to return the location of the shortcut (. lnk). FileName The file name selected in the file dialog box. The file name of all selected files in the FileNames (read-only) dialog box. Filter the current file name filters the string to determine the options displayed in the dialog box, save as file type, or the Files of type box. The index of the filter currently selected in the FilterIndex file dialog box. The initial directory displayed by the InitialDirectory file dialog box. Restoredirectory indicates whether the dialog box restores the current directory before closing. SHOWHELP Indicates whether the Help button is displayed in the file dialog box. Title File dialog box. Validatenames indicates whether the dialog box accepts only valid file names. Reset (method) resets all properties to their default values. ShowDialog (method) Displays the file dialog box. If the user presses OK, it returns DialogResult.OK or returns DIALOGRESULT.CANCEL.
Click Select a File(When selecting a file), the sample form Frmfileversioninfo calls the following code:
Private Sub Btnselectfile_click (_ ByVal sender as System.Object, _ ByVal E As System.EventArgs) _ Handles btnselectfile.c Lick    dim ofd as OpenFileDialog = New OpenFileDialog ()     ofd. Filter = _         "executable files (*.exe;*.dll;*.ocx) |" & _          "*.exe;*.dll;*.ocx|" & _          "Drivers (*.SYS;*.DRV;*.FNT) |" & _         "*.sys;*.drv;*.fnt|" & _         "All Files (*.*) |*.*" &NBSP;&NBSP;&NBSP;&NBSP;OFD. FilterIndex = 1    ofd. ShowReadOnly = False    ofd. Restoredirectory = True    if ofd. ShowDialog () = DialogResult.OK then        if ofd. Filename.length > 0 then            ' Displays the file version information.             displayfileinfo (OFD. FileName)         end if    end IfEnd Sub
tip: As with the Visual Basic 6.0 CommonDialog ActiveX control, you can set the Filter property of the FileDialog class to a string that contains a pair of vertical bar-delimited values. As shown below: "description| Filespec ".

In the example above, once you select a filename, the sample form displays information about the file in the form's ListView control. The next section discusses the FileVersionInfo class, which enables you to do this. Retrieving file version information
Developers and compilers can embed version information into executable files, DLL files, and driver files. You may need to retrieve some or all of the version information as part of your application, which requires a large amount of API calls to perform this operation in Visual Basic 6.0. You need to invoke the Getversioninfosize, VerQueryValue, and GetFileVersionInfo Win32 API functions, and it's not easy to use any of these functions in Visual Basic.
This situation once again shows the power of the. NET framework: Using FileVersionInfo objects makes these tasks very simple. All you need to do is call the shared GetVersionInfo method of the FileVersionInfo object, pass a filename, and all the problems will be solved. The sample form uses the following code:
Dim Fvi as FileVersionInfo = _ Fileversioninfo.getversioninfo (strfile)

After you have done this, retrieving the properties of the FileVersionInfo object is a very simple matter. The sample form uses a short program shown below to add each property name and value to the form's ListView control:
Private Sub AddItem (_byval strproperty As String, _byval strvalue as String) with LvwInfo.Items.Add (Strproperty) . SubItems.Add (strvalue) End WithEnd Sub

What really works is the code, and the code simply calls AddItem repeatedly, and each property is invoked once:
AddItem ("Comments", fvi.comments) AddItem ("CompanyName", Fvi.companyname) AddItem ("Language", Fvi. Language) ' Here deleted some lines ... AddItem ("Product", Fvi. ProductName) AddItem ("Productprivatepart", _ Fvi. Productprivatepart.tostring ()) AddItem ("ProductVersion", Fvi. ProductVersion) AddItem ("Specialbuild", Fvi. Specialbuild)

The resulting form shown in Figure 3 shows all version information that can be used programmatically.

Figure 3: Retrieving file version information using the FileVersionInfo class Retrieving environment information
The Win32 API provides a series of functions that allow you to determine the user's environment settings. GetSystemMetrics and SystemParametersInfo are two of these functions. You can continue to call these API functions from a. NET application, but you probably don't need to. The Environment class (located in the System namespace) and the SystemInformation class (located in the System.Windows.Forms namespace) provide much the same information as API functions. In this section, we will demonstrate the capabilities of these two classes through an example.
Warning: Don't waste time looking for ways to set user environment settings from these classes. All the information shown here is read-only. If you still want to modify the environment settings, you will need to look for additional methods.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.