Search code examples
c#vb.netdecompilingdotpeek

Decompile VB.NET exe to C# (dotPeek & JustDecompile)


So I have a program which is built from VB.NET but don't have the source code and it's impossible to get the source code, I need to modify the program so I decompile it using dotPeek & JustDecompile to C# because I can code in C# but I never really learn VB.NET (I've tried to decompile to VB.NET with JustDecompile too but it's look much messier than C# for me). But the decompiled project is full of strange code that I don't see when I try to decompile C# exe and dll to C# project. It's full of codes that looks like shouldn't be there (looks like behind the scene codes) like:

private static List<WeakReference> __ENCList;
lock (finvendor.__ENCList)
finvendor.__ENCList.Add(new WeakReference((object) this));
[AccessedThroughProperty("controlname")] //for every controls

it's also full of this kind code for every controls which I don't find in C#:

internal virtual CheckEdit chkNonAktif
{
  [DebuggerNonUserCode] get
  {
    return this._chkNonAktif;
  }
  [DebuggerNonUserCode, MethodImpl(MethodImplOptions.Synchronized)] set
  {
    EventHandler eventHandler = new EventHandler(this.chk_CheckedChanged);
    if (this._chkNonAktif != null)
      this._chkNonAktif.CheckedChanged -= eventHandler;
    this._chkNonAktif = value;
    if (this._chkNonAktif == null)
      return;
    this._chkNonAktif.CheckedChanged += eventHandler;
  }
}

It's use Devexpress version 10, is these codes because of that? Is it normal or could I delete these kind of codes?


Solution

  • You have a debug build of VB Winform project. The weak reference stuff is used by the debugger and is not emitted for release builds.

    VB creates a property for each Dim WithEvents ControlName As ControlType for which there is also a method decorated with Handles ContolName.EventName. The property setter contains the event wiring code that makes the Handles Event stuff work.

    For example a button and its click event.

    Friend WithEvents Button1 As Button
    
    Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)  Handles Button1.Click
        'some code
    End Sub
    

    Will cause this property to be generated:

    Friend Overridable Property Button1 As Button
        <CompilerGenerated> _
        Get
            Return Me._Button1
        End Get
        <MethodImpl(MethodImplOptions.Synchronized), CompilerGenerated> _
        Set(ByVal WithEventsValue As Button)
            Dim handler As EventHandler = New EventHandler(AddressOf Me.Button1_Click)
            Dim button As Button = Me._Button1
            If (Not button Is Nothing) Then
                RemoveHandler button.Click, handler
            End If
            Me._Button1 = WithEventsValue
            button = Me._Button1
            If (Not button Is Nothing) Then
                AddHandler button.Click, handler
            End If
        End Set
    End Property
    

    You will also probably have many classes with a name in the form of My_XYZ that support VB's application framework.

    I would suggest that you create a new VB WinForm project with a few controls/event handlers and then de-compile that so that you can see how your de-compiler reproduces the boiler plate stuff from the IL. Once you know the pattern, it will be a lot easier.