1

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?

julius daniel
  • 21
  • 1
  • 6
  • 3
    Reverse engineering someone else's code then publishing said code is likely violating several key paragraphs of the T&Cs. Of course if it was your app then there is nothing to worry about. Other than the fact it contains stuff you weren't aware of –  May 23 '18 at 12:53
  • 1
    Hm? Pardon? I don't understand what you mean about that, the program is mine, but the programmer can not be contacted anymore and I need to modify some of the program – julius daniel May 23 '18 at 13:06
  • It is the code that the VB.NET compiler auto-generates to implement the `WithEvents` keyword. No equivalent in C#, don't make any big plans for the next month. – Hans Passant May 23 '18 at 13:49
  • 3
    The fact that the code is marked "debugger non-user code" is there to tell you that this code is *for the debugger, and not written by the user*. – Eric Lippert May 23 '18 at 13:53
  • Comment out such code it will create "member already exists" errors. It is generated by debugger and is safe to comment out or even delete. thanks – MindRoasterMir Jan 12 '19 at 10:58

2 Answers2

2

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.

TnTinMn
  • 11,522
  • 3
  • 18
  • 39
  • You should also mention the reason for VB's way of doing this - to allow the event wireups to also be copied when the WithEvents object is set to another object. – Dave Doknjas May 23 '18 at 13:59
0

My be this answer helps someone understand the problem and find own solution.

  1. Just create your own small application and then compile it to and .exe file.
  2. Then decompile this .exe file with the same decompiler you used.
  3. Now you see the same strange code there and you know you didnot put it there.

Thanks.

MindRoasterMir
  • 324
  • 1
  • 2
  • 18