Protecting Windows Forms Applications with Data Bound GUI Controls
Published on February 10, 2022 by John Brawner
Today we will focus on data binding, but first let’s define this. Data binding allows Windows Forms applications to display and update UI controls from a data source, without having to modify source code for the control itself.
When protecting Windows Forms applications, it is important to note how the data bound controls are constructed to determine if they will be impacted by code obfuscation. If the controls bind to a collection of objects, original property names of that object must be preserved to correctly populate “DisplayMember” and “ValueMember” properties of the control. When binding controls to an Enum, the original names of its members must be preserved, or the GUI control might show obfuscated names. On the other hand, if we’re binding directly to a database table (and the table does not map to an object in source code), we don’t need any custom configurations because Dotfuscator does not mangle table and column names.
Consider the Following Example:
This simple Windows Forms application has three UI controls with different data binding techniques: a DataGridView binds to a Customer table in a database, a ListBox binds to a collection of Employee objects, and ComboBox binds to an Enum called DaysOfWeek:
If I obfuscate with project defaults, I experience a runtime error at app startup:
This occurs because original property names of the Employee object are used in “DisplayMember” and “ValueMember” ListBox properties:
listBox1.DataSource = employeeList;
listBox1.DisplayMember = “Name”;
listBox1.ValueMember = “Department”;
To Avoid the Runtime Error:
First, I’ll open my project configuration file (DotfuscatorConfig.xml) in the Dotfuscator Config Editor, and set a Rename exclusion for the properties in the Employee object:
After configuring this Rename exclusions, the application starts without the runtime exception, but the “DaysOfWeek” ComboBox appears with obfuscated names:
In order to fix this, I will configure a Rename exclusion for the members of DaysOfWeek.
After providing this Rename exclusion, the app starts without any issues or erroneous behavior. Please also note the DataGridView, which binds to the Customer table in our database, did not require any Rename configuration to start and display correctly.
There are several different ways to use data binding in Windows Forms applications. We’ve seen a few ways that data bound controls can be impacted by obfuscation. If you experienced a runtime crash or erroneous UI behavior after applying obfuscation, please use the above steps to resolve the issue.
The full example can be downloaded here.
If you have any feedback on this topic or other topics you would like us to discuss in the Support Corner, please feel free to contact our Support Department.