Solved

conver VB to C#

Posted on 2011-09-27
9
329 Views
Last Modified: 2013-12-17
can someone assist in converting this VB code to C#?
Imports System.Runtime.InteropServices
Imports System.Threading

Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TabBusting.BeginBust(3)
    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        TabBusting.EndBust()
    End Sub
End Class

Public NotInheritable Class TabBusting

    Private Shared _shell As Object = Nothing
    Private Shared _windows As Object = Nothing
    Private Shared _timer As Threading.Timer = Nothing
    Private Shared _maxTabs As UInteger = 0

    Public Shared Sub BeginBust(ByVal maxTabs As UInteger)
        _shell = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application"), True)
        _maxTabs = maxTabs
        ' Setup tab busting every second.
        _timer = New Threading.Timer(AddressOf BustWindows, Nothing, New TimeSpan(0, 0, 0), New TimeSpan(0, 0, 1))
    End Sub

    Public Shared Sub EndBust()
        _timer.Dispose()
        _timer = Nothing
        Marshal.FinalReleaseComObject(_shell)
        _shell = Nothing
    End Sub
   
    Private Shared Sub BustWindows(ByVal state As Object)
         
        Dim windowList As New List(Of Object)
        Dim i As Integer

        ' Populate window collection
        _windows = _shell.Windows

        ' Filters ShellView windows
        For Each obj As Object In _windows
            If obj.Document.GetType.Name.Contains("HTMLDocument") Then
                windowList.Add(obj)
            End If
        Next
         
        If windowList.Count >= _maxTabs Then
            For i = 0 To windowList.Count - 1
                If i >= _maxTabs Then
                    windowList(i).Quit() ' bust tabs above maximum allowed.
                End If
            Next i
        End If

        windowList.Clear()
        windowList = Nothing
        _windows = Nothing

    End Sub
End Class

Open in new window

0
Comment
Question by:OrenRozen
9 Comments
 
LVL 39

Expert Comment

by:Pratima Pharande
Comment Utility

refer
http://www.developerfusion.com/tools/convert/vb-to-csharp/
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;

public class Form1
{
	private void Form1_Load(System.Object sender, System.EventArgs e)
	{
		TabBusting.BeginBust(3);
	}

	private void Form1_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e)
	{
		TabBusting.EndBust();
	}
	public Form1()
	{
		FormClosing += Form1_FormClosing;
		Load += Form1_Load;
	}
}

public sealed class TabBusting
{

	private static object _shell = null;
	private static object _windows = null;
	private static System.Threading.Timer _timer = null;

	private static uint _maxTabs = 0;
	public static void BeginBust(uint maxTabs)
	{
		_shell = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application"), true);
		_maxTabs = maxTabs;
		// Setup tab busting every second.
		_timer = new System.Threading.Timer(BustWindows, null, new TimeSpan(0, 0, 0), new TimeSpan(0, 0, 1));
	}

	public static void EndBust()
	{
		_timer.Dispose();
		_timer = null;
		Marshal.FinalReleaseComObject(_shell);
		_shell = null;
	}

	private static void BustWindows(object state)
	{

		List<object> windowList = new List<object>();
		int i = 0;

		// Populate window collection
		_windows = _shell.Windows;

		// Filters ShellView windows
		foreach (object obj in _windows) {
			if (obj.Document.GetType.Name.Contains("HTMLDocument")) {
				windowList.Add(obj);
			}
		}

		if (windowList.Count >= _maxTabs) {
			for (i = 0; i <= windowList.Count - 1; i++) {
				if (i >= _maxTabs) {
					windowList[i].Quit();
					// bust tabs above maximum allowed.
				}
			}
		}

		windowList.Clear();
		windowList = null;
		_windows = null;

	}
}

Open in new window

0
 
LVL 17

Expert Comment

by:sonawanekiran
Comment Utility
Telerik Converter is one of the best
http://converter.telerik.com/
0
 
LVL 3

Author Comment

by:OrenRozen
Comment Utility
@pratima_mcs
line  59 return an error:
'object' does not contain a definition for 'Windows' and no extension method 'Windows' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)

line 63 return an error:
'object' does not contain a definition for 'Document' and no extension method 'Document' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)

line 71 return an error:
'object' does not contain a definition for 'Quit' and no extension method 'Quit' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)

@sonawanekiran
Same errors with Telerix

Is there a missing invoke? Running this code as VB is working perfectly.
0
 
LVL 16

Expert Comment

by:Easwaran Paramasivam
Comment Utility
Please try the language translator: http://www.carlosag.net/tools/codetranslator/
using System.Runtime.InteropServices;
using System.Threading;

public class Form1 {
    
    private void Form1_Load(object sender, System.EventArgs e) {
        TabBusting.BeginBust(3);
    }
    
    private void Form1_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e) {
        TabBusting.EndBust();
    }
}
public sealed class TabBusting {
    
    private static object _shell = null;
    
    private static object _windows = null;
    
    private static Threading.Timer _timer = null;
    
    private static System.UInt32 _maxTabs = 0;
    
    public static void BeginBust(System.UInt32 maxTabs) {
        _shell = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application"), true);
        _maxTabs = maxTabs;
        //  Setup tab busting every second.
        _timer = new Threading.Timer(new System.EventHandler(this.BustWindows), null, new TimeSpan(0, 0, 0), new TimeSpan(0, 0, 1));
    }
    
    public static void EndBust() {
        _timer.Dispose();
        _timer = null;
        Marshal.FinalReleaseComObject(_shell);
        _shell = null;
    }
    
    private static void BustWindows(object state) {
        List windowList = new List(Of, Object);
        int i;
        //  Populate window collection
        _windows = _shell.Windows;
        //  Filters ShellView windows
        foreach (object obj in _windows) {
            if (obj.Document.GetType.Name.Contains("HTMLDocument")) {
                windowList.Add(obj);
            }
        }
        if ((windowList.Count >= _maxTabs)) {
            for (i = 0; (i 
                        <= (windowList.Count - 1)); i++) {
                if ((i >= _maxTabs)) {
                    windowList[i].Quit();
                    //  bust tabs above maximum allowed.
                }
            }
        }
        windowList.Clear();
        windowList = null;
        _windows = null;
    }
}

Open in new window

0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 16

Expert Comment

by:Easwaran Paramasivam
Comment Utility
Please do ensure that you have added all required DLLs in the reference section of the project.
0
 
LVL 3

Author Comment

by:OrenRozen
Comment Utility
@EaswaranP:
Same errors using this translator

@EaswaranP:
there are no specific dll's. IUf you'll take the VB code and paste it to a new VB solution it will work.
But the converted code is missing somthing. My guess it has something to do with this line:
_shell = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application"), True)
but I don't know how to make it work with C#. This is why I think there is some invoke missing.
0
 
LVL 28

Expert Comment

by:strickdd
Comment Utility
I don't know if this will work, but the concept is there. You need to TypeCast the _shell variable as the Shell.Application type that is actually created. You may need to use some more reflection to do this if the code below doesn't work.

_windows = ((Type.GetTypeFromProgID("Shell.Application"))_shell).Windows;


0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
The VB code "works" because the default for "Option Strict" is Off:
http://msdn.microsoft.com/en-us/library/zcd4xwzs(VS.80).aspx

    "In addition to disallowing implicit narrowing conversions, Option Strict generates an error for late binding. An object is late bound when it is assigned to a variable that is declared to be of type Object."

The variable "_shell" has been declare as Object and is using late binding.

To make it work in C# you'd also have to turn Option Strict Off or use early binding.
0
 
LVL 29

Accepted Solution

by:
nffvrxqgrcfqvvc earned 500 total points
Comment Utility
Here is example using COM references. In the Add Reference choose the COM Tab and add the references next to the using statements in the code section.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Shell32; // Microsoft Shell Controls And Automation
using SHDocVw;// Microsoft Internet Controls


namespace TabBuster
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            TabBusting.BeginBust(3);

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            TabBusting.EndBust();
        }
    }
}

public sealed class TabBusting
{

    private static Shell32.Shell _shell = null;
    private static IShellWindows _windows = null;
    private static System.Threading.Timer _timer = null;

    private static uint _maxTabs = 0;
    public static void BeginBust(uint maxTabs)
    {
        _shell = new Shell32.Shell();
        _maxTabs = maxTabs;
        _timer = new System.Threading.Timer(BustWindows, null, new TimeSpan(0, 0, 0), new TimeSpan(0, 0, 1));
    }

    public static void EndBust()
    {
        _timer.Dispose();
        _timer = null;
        _shell = null;
    }


    private static void BustWindows(object state)
    {
        List<SHDocVw.InternetExplorer> windowList = new List<SHDocVw.InternetExplorer>();
        int i = 0;

        // Populate window collection
        _windows = (IShellWindows)_shell.Windows();

        // Filters ShellView windows
        foreach (SHDocVw.InternetExplorer obj in _windows)
        {
            //Console.WriteLine(obj.GetType().FullName.ToString());
            
            if (obj.Document !=  null)
            {
                if (obj.Document.GetType().Name.Contains("HTMLDocument"))
                {
                    windowList.Add(obj);
                }
            }
        }

        if (windowList.Count >= _maxTabs)
        {
            for (i = 0; i <= windowList.Count - 1; i++)
            {
                if (i >= _maxTabs)
                {
                    ((SHDocVw.InternetExplorer)windowList[i]).Quit();
                    // Bust tabs above maximum allowed.
                }
            }
        }

        windowList.Clear();
        windowList = null;
        _windows = null;

    }

}

Open in new window

0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now