Add Ons - DevSource
DevSource: Microsoft Developer Resource DevSource Home Sponsored by Microsoft Home Add Ons Architecture Languages Techniques Using VS Forums
Home arrow Add Ons arrow Page 3 - C-Sharpening Your Visual Basic Code
C-Sharpening Your Visual Basic Code
By John Mueller

Rate This Article: Add This Article To:

C-Sharpening Your Visual Basic Code - ' Databases With Aplomb '
( Page 3 of 4 )

Databases with Aplomb

Testing with complex applications went as expected. I tried combinations of both multiple form applications and those with database ties. Both desktop and Web applications converted without any problem. I did start seeing the problem I had anticipated with code that becomes a little unwieldy to view, due to the method that C-Sharpener uses to convert the code. At some point, I think even an expert would start having trouble cleaning up the resulting code, so it appears the same as manually produced code. However, the time savings becomes so significant, at some point, that you might decide the conversion is worthwhile anyway, even if you still have to maintain the code in Visual Basic.

ADVERTISEMENT

I did run into a few problems compiling the converted code. A database application that uses multiple forms to contact a Web service (Amazon.com, in this case) didn't convert very well. Elegant Technologies documented all of the problems I encountered in the "Known Limitations and Workarounds" section of the help file, so I feel that they've taken a reasonable course of action. For example, I ran into a problem with an embedded enumeration, and the help file provides a perfectly acceptable manual workaround for the problem. You'll want to study the "Known Limitations and Workarounds" section of the help file before you begin converting any complex application.

PInvoke is Problematic

I ran tests on five different applications that use PInvoke for various needs. The reason I performed this test is that many developers must use PInvoke to overcome limitations in the .NET Framework. You don't have full access to everything that the Win32 environment provides; PInvoke provides the means for overcoming these limitations. In every case, C-Sharpener failed to provide me with working code. Listing 3 shows the simplest PInvoke example that I tried.

Listing 3: A Simple PInvoke Example

Imports System
Imports System.Runtime.InteropServices

Module Module1
    ' Import the Windows Beep() API function.
    <DllImport("kernel32.dll")> _
    Public Function Beep(ByVal freq As Integer, _
        ByVal dur As Integer) As Boolean
    End Function

    ' Define some constants for using the PlaySound() function.
    Public Const SND_FILENAME = &H20000
    Public Const SND_ASYNC = &H1

    ' Import the Windows PlaySound() function.
    <DllImport("winmm.dll")> _
    Public Function PlaySound(ByVal pszSound As String, _
                          ByVal hmod As Integer, _
                          ByVal fdwSound As Integer) As Boolean
    End Function


    Sub Main()
        ' Create a sound using an escape character.
        Console.Write(Chr(7))
        Console.WriteLine("Press Any Key When Ready...")
        Console.ReadLine()

        ' Create a sound using a Windows API call.
        Beep(800, 200)
        Console.WriteLine("Press Any Key When Ready...")
        Console.ReadLine()

        ' Create a sound using a Visual Basic call.
        Microsoft.VisualBasic.Beep()
        Console.WriteLine("Press Any Key When Ready...")
        Console.ReadLine()

        ' Create a sound using a WAV file.
        PlaySound("BELLS.WAV", _
                  0, _
                  SND_FILENAME Or SND_ASYNC)
        Console.WriteLine("Press Any Key When Ready...")
        Console.ReadLine()
    End Sub

End Module

This example shows several techniques for playing a sound using Visual Basic. Yes, you can use the Beep method(), but that doesn't allow you to change the tonal quality or duration of the output. In addition, you can't easily play WAV files or other forms of media. The four techniques demonstrate methods for overcoming this limitation. Listing 4 shows the non-working output from C-Sharpener.

Listing 4: C-Sharpener Produces Non-working PInvoke Code

using System; 
using System.Runtime.InteropServices; 
using Microsoft.VisualBasic;
using System.Collections;
using System.Data;
using System.Diagnostics;
namespace MakeSound {
    // following class was VB module
    [Microsoft.VisualBasic.CompilerServices.StandardModule]
    sealed public class Module1 { 
        // Import the Windows Beep() API function.
        [ DllImport( "kernel32.dll" ) ]
        public static bool Beep( int freq, int dur ) { 
            return false;
        } 
        
        // Define some constants for using PlaySound() function.
        public const int SND_FILENAME = 0X20000; 
        public const int SND_ASYNC = 0X1; 
        
        // Import the Windows PlaySound() function.
        [ DllImport( "winmm.dll" ) ]
        public static bool PlaySound( string pszSound, 
                                    int hmod, int fdwSound ) { 
            return false;
        } 
        
        public static void Main() { 
            //  Create a sound using an escape character.
            Console.Write( Strings.Chr( 7 ) ); 
            Console.WriteLine( "Press Any Key When Ready..." ); 
            Console.ReadLine(); 
            
            //  Create a sound using a Windows API call.
            Beep( 800, 200 ); 
            Console.WriteLine( "Press Any Key When Ready..." ); 
            Console.ReadLine(); 
            
            //  Create a sound using a Visual Basic call.
            Microsoft.VisualBasic.Interaction.Beep(); 
            Console.WriteLine( "Press Any Key When Ready..." ); 
            Console.ReadLine(); 
            
            //  Create a sound using a WAV file.
            PlaySound( "BELLS.WAV", 0, SND_FILENAME | SND_ASYNC ); 
            Console.WriteLine( "Press Any Key When Ready..." ); 
            Console.ReadLine(); 
        } 
    } 
}

Listing 5 shows a hand-coded version of the same code that I produced.

Listing 5: A Manual Conversion of the PInvoke Example

using System;
using System.Runtime;
using System.Runtime.InteropServices;

namespace MakeSound
{
   /// <summary>
   /// Summary description for Class1.
   /// </summary>
   class Class1
   {
      /// <summary>
      /// The main entry point for the application.
      /// </summary>
      
      // Import the Windows Beep() API function.
      [DllImport("kernel32.dll")]
      private static extern bool Beep(int freq, int dur);

      // Define some constants for using the PlaySound() function.
      public const int SND_FILENAME = 0x00020000;
      public const int SND_ASYNC = 0x0001;

      // Import the Windows PlaySound() function.
      [DllImport("winmm.dll")]
      public static extern bool PlaySound(string pszSound,  
                                          int hmod,     
                                          int fdwSound);

      [STAThread]
      static void Main(string[] args)
      {
         // Create a sound using an escape character.
         Console.Write("\a");
         Console.WriteLine("Press Any Key When Ready...");
         Console.ReadLine();
         
         // Create a sound using a Windows API call.
         Beep(800, 200);
         Console.WriteLine("Press Any Key When Ready...");
         Console.ReadLine();

         // Create a sound using a Visual Basic call.
         Microsoft.VisualBasic.Interaction.Beep();
         Console.WriteLine("Press Any Key When Ready...");
         Console.ReadLine();

         // Create a sound using a WAV file.
         PlaySound("BELLS.WAV", 
                   0, 
                   SND_FILENAME | SND_ASYNC);
         Console.WriteLine("Press Any Key When Ready...");
         Console.ReadLine();
      }
   }
}

The C-Sharpener output contains a lot of extra baggage that you don't need to make the application run. The amount of extra baggage increases greatly as you convert more complex PInvoke examples. At some point, the complexity increases, until there's little chance of making the output work at all. Elegant Technologies documents some of these issues in the help files; others aren't. For example, I'm not sure why C-Sharpener embedded the example in a Visual Basic module using the

[Microsoft.VisualBasic.CompilerServices.StandardModule]

attribute; this issue isn't documented.

Another problem is that the conversion didn't work well for the external function declarations. Changing the declarations so they look like this:

//  Import the Windows Beep() API function.
[ DllImport( "kernel32.dll" ) ]
public static extern bool Beep( int freq, int dur ); 

...produced working code. I would have expected the help file to address this issue. In fact, the C-Sharpener product should have handled this particular conversion better.



 
 
>>> More Add Ons Articles          >>> More By John Mueller
 



HD VOIP Has Arrived (with Tony Konstner)

Play Video >

All Videos >

Google and blonde jokes?

Read now >

Favorite books!

Read now >

View Now
DevSource RSS FEEDS
XML Want an easy way to keep up with breaking tech news? And the Get DevSource headlines delivered to your desktop with RSS.