There is no doubt the .NET Framework contains some rich and very useful objects to make many task very easy to do. Unfortunately Visual Basic 6.0 does not have direct access to these great features. BUT, all is not lost. You can create a COM Wrapper in Visual Basic.NET (I used VB 2005) to make features of the DotNETFramework available to the Legacy/Classic versions of Visual Basic. You can also use VB 2008 and Visual Basic 2010 (Express Edition should be just fine) if thats what you have.
Note: The example projects for this article has been done for a very long time and I forgot about putting a download link on this article. So here are the download links!
VB 6.0 Project with Compiled .NET Wrapper
VB 2005 Project with Compiled .NET Wrapper
The first thing you would want to do is open up your Visual Basic.NET (Any version is fine as far as I know?.? I am using VB 05 in this article.) IDE and start a new Class Library project. Also, the actual Ping codes are NOT available in Visual Basic.NET 02/03. The principals in the article are the same but the actual Ping code will not work in the earlier dotnet languages. Now, Go ahead and just delete the original Class1.vb file since it is of no use for Fusion. Next goto the Project Menu and click on – Add New Item (Or you can click on ‘Add Class’ for VB 05/08) and select the ComClass Module. I named mine ‘pingWrapperForVB’. I went ahead and added a Reference to System.Windows.Forms since I wanted to use the more advanced MessageBox.Show feature instead of the legacy Msgbox. I also Imported the Namespaces below this image.
Add these Import Statements right above your Class.
Important Note: When adding a COM Class it should automatically check the ‘Register for COM Interop‘ box. If it didn’t then you should go to the ‘Project’ menu and click on the ‘Project Properties‘ item. Click on the Compile option and scroll to the bottom of the page. You should see the ‘Register for Interop‘ checkbox. Make sure that checkbox is checked.
Important Note #2: Just remember if your program is going to be used on a computer other than the Developer computer, then you will need to register the .NET Managed wrapper as a COM object in the Windows registry. Click this link for more information.
Then I created a Public Function named: ping_AddressReply with Two (2x) parameters named (URL as String and TimeoutInMS as Integer) and it returns a Boolean type. I made the TimeoutInMS Parameter a Optional value with a default of 10 Seconds. This Function will be used to send a ping request and get the return value of the request, either ‘True’ that the Ping was successful or ‘False’ if it was not successful.
I then added just a basic Try/Catch/End Try feature that will throw a Ping Exception in a Messagebox if something didn’t go quite right. You can do your own error handling though. Within the Try/Catch structure I added the small piece of code below…
'Simply sends a Ping request using the URL Parameter and the Timeout Parameter. I made the
'timeoutInMS Parameter optional with a default value of 10 seconds.
Return My.Computer.Network.Ping(url, timeoutInMS)
Setup the next Function the same as the first. Except making the Function type a Integer. Remember that VB 6.0 will see the .NET Integer as a Long. I named the Function ‘New ping_AddressTimeItTakes‘ as Integer. Then simple add the codes below inside the Try/Catch structure…
Dim p As New Ping
Dim reply As PingReplyreply = p.Send(url, timeoutInMS)
You are now done with the wrapper. Look over your codes one last time to make sure they are correct and then compile the class library. You are now ready to use the wrapper in Visual Basic 5.0/6.0. Now load your VB 5.0/6.0 IDE and create a new .EXE Project type. I named mine: ‘PingAddressWrapperEx‘. I likewise I added 2x Labels named ‘lblReply’ and ‘lblTime’, a Textbox for the url I named: ‘txtAddress’, and 2x command buttons. Name the buttons whatever you want to. Now goto the ‘Project’ menu and click on the ‘References’ menu item. Click on the Browse button and locate your .NET Project and goto the ‘Bin’ Directory (or where the compiled class is). Add a reference to the ‘TLB’ file and NOT the .DLL file. VB 6 cannot use the .DLL class directly and thus you need to reference the TLB file to get access to the .DLL in VB 5.0 and VB 6.
Once you create a reference to the TLB file, then add the line of code below in the declaration section…
'I’m using ping to refer to the Wrapper.
Dim ping As pingWrapperForVB6.pingWrapperForVB6
Once you take care of that, put the codes below in your command buttons…
Private Sub cmdPing_Click()
'Check if a instance of the class already exists, and if it isn’t then create a new instance of the ping wrapper.
If ping Is Nothing Then
'Create a new instance of the wrapper.
ping = New pingWrapperForVB6.pingWrapperForVB6
'Send the ping request and get the True or False messages.
lblReply.Caption = "Replied: " & ping.ping_AddressReply(txtAddress.Text)
Likewise, put the codes below in the other command button…
Private Sub cmdSendTime_Click()
'Check if a instance of the class already exists, and if it isn't then create a new instance of
'the ping wrapper.
If Ping Is Nothing Then
'Create a new instance of the wrapper.
Ping = New pingWrapperForVB6.pingWrapperForVB6
'Send the ping request and display the time it takes to get a reply.
lblTime.Caption = "Round Trip Time: " & Ping.ping_AddressTimeItTakes(txtAddress.Text, 10000) & "ms"
You can now run your project and put in the URL you want to ping. I put in: vbcodesource.com but you can put the url you want. But Do Not include the http:// portion of the url, it will cause the ping to fail. Now just click on the command buttons that has the code and you should see the status of the pings in the label controls…
Hopefully you had no errors popping up. My example returned True that the url was available and a round trip time of: 42ms. Thats all there is to it! You now have basic Ping capabilites in VB 6.0. Remember though, the target computer MUST have the version of the DotNET Framework you made the wrapper with. IF not then the Ping Wrapper will fail and you won’t have no ping capabilities. I will eventually add the actual projects for the .NET Wrapper and the VB 6 example when I get the time on my other site at: http://www.vbcodesource.com Anyways, have fun 🙂