Win32 API Calls

Snoops

   

 
Migrating to .NET: A Pragmatic Path to Visual Basic .NET, Visual C++ .NET, and ASP.NET
By Dhananjay  Katre, Prashant  Halari, Narayana  Rao  Surapaneni, Manu  Gupta, Meghana  Deshpande

Table of Contents
Chapter 4.   Post-migration Changes


Visual Basic 6.0 applications can access Win32 API. Win32 API consists of the various functions and types that can be accessed by the Visual Basic applications. Visual Studio comes with the API text viewer utility. This utility can be used by application developers to find out the various functions and types that need to be used in Visual Basic 6.0 applications.

Win32 functions provide functions for Windows management, system services, GDI- related functions such as BitBlt , and multimedia-related functionalities. When the Visual Basic 6.0 applications containing Win32 API calls are upgraded to Visual Basic .NET, the code gets upgraded with a runtime warning. Because parameters passed to the function may contain user -defined types, the upgrade wizard converts such types into structures. These types could contain fixed-length strings in Visual Basic 6.0. However, they are no longer supported in Visual Basic .NET. As a result, some modifications have to be done to the Visual Basic .NET code.

CODE EXAMPLES

Example 18

In the following code example, the Visual Basic 6.0 application uses the Win32 API call to the GetSystemInfo function. This application finds three things, the processor type, the number of processors, and the page size , and displays the results in a RichTextBox control. The following code is in the SystemInfo-VB folder for this chapter:

 graphics/icon01.gif Private Sub Command1_Click()     Dim objSystemInfo As SYSTEM_INFO     Call GetSystemInfo(objSystemInfo)     RichTextBox1.Text = ""     RichTextBox1.Text = RichTextBox1.Text & _      "Processor Type = " & vbTab & vbTab & _       objSystemInfo.dwProcessorType & vbCrLf     RichTextBox1.Text = RichTextBox1.Text & _      "Page Size = " & vbTab & vbTab & _       objSystemInfo.dwPageSize & vbCrLf     RichTextBox1.Text = RichTextBox1.Text & _      "Number of   Processors = " & vbTab & _       objSystemInfo.dwNumberOrfProcessors     Command2.SetFocus  End Sub 

The following module is in the same folder. This module contains a Win32 API function declaration.

 graphics/icon01.gif Public Type SYSTEM_INFO     dwOemID As Long     dwPageSize As Long     lpMinimumApplicationAddress As Long     lpMaximumApplicationAddress As Long     dwActiveProcessorMask As Long     dwNumberOrfProcessors As Long     dwProcessorType As Long     dwAllocationGranularity As Long     dwReserved As Long  End Type  Public Declare Sub GetSystemInfo Lib "kernel32" _   (lpSystemInfo As SYSTEM_INFO)  Public Declare Function GetLastError Lib "kernel32" () _   As Long 

When this Visual Basic 6.0 code is upgraded, the Visual Basic form's code gets converted into Windows Forms code. The user-defined type gets changed to a structure. Also the upgrade wizard introduces a runtime warning. The following equivalent Visual Basic .NET code is in the SystemInfo-VB.NET folder for this chapter:

 graphics/icon01.gif Private Sub Command1_Click(ByVal eventSender As _   System.Object, ByVal eventArgs As System.EventArgs) _    Handles Command1.Click     Dim objSystemInfo As SYSTEM_INFO     Call GetSystemInfo(objSystemInfo)     RichTextBox1.Text = ""     RichTextBox1.Text = RichTextBox1.Text & _      "Processor Type = " & vbTab & vbTab & _       objSystemInfo.dwProcessorType & vbCrLf     RichTextBox1.Text = RichTextBox1.Text & _      "Page Size = " & vbTab & vbTab & _       objSystemInfo.dwPageSize & vbCrLf     RichTextBox1.Text = RichTextBox1.Text & _      "Number of Processors = " & vbTab & _       objSystemInfo.dwNumberOrfProcessors     Command2.Focus()  End Sub  Private Sub Command2_Click(ByVal eventSender As _   System.Object, ByVal eventArgs As System.EventArgs) _    Handles Command2.Click   End  End Sub 

The following module declaration has been kept in the same directory.

 graphics/icon01.gif Module Module1        Public Structure SYSTEM_INFO           Dim dwOemID As Integer           Dim dwPageSize As Integer           Dim lpMinimumApplicationAddress As Integer           Dim lpMaximumApplicationAddress As Integer           Dim dwActiveProcessorMask As Integer           Dim dwNumberOrfProcessors As Integer           Dim dwProcessorType As Integer           Dim dwAllocationGranularity As Integer           Dim dwReserved As Integer        End Structure  'UPGRADE_WARNING: Structure SYSTEM_INFO may require marshal- ling attributes to be passed as an argument in this 'Declare  statement. Click for more: 'ms-'help://MS.VSCC/commoner/redir/  redirect.htm?keyword="vbup10'50"'        Public Declare Sub GetSystemInfo Lib "kernel32" _        (ByRef lpSystemInfo As SYSTEM_INFO)        Public Declare Function GetLastError Lib "kernel32" _         ()As Integer     End Module 

As shown in this code for the module, a runtime warning is issued. The warning states that if the type contained in Visual Basic includes fixed-length strings, marshalling attributes have to be used to make the code work in Visual Basic .NET. For this example, because the fixed-length strings are not used, no marshalling attributes are required and the code delivers the same functionality in Visual Basic .NET without requiring any code changes to be made by the developer.


Snoops

   
Top


Migrating to. NET. A Pragmatic Path to Visual Basic. NET, Visual C++. NET, and ASP. NET
Migrating to. NET. A Pragmatic Path to Visual Basic. NET, Visual C++. NET, and ASP. NET
ISBN: 131009621
EAN: N/A
Year: 2001
Pages: 149

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net