PreEmptive logo

Protecting Windows Forms Applications with Data Bound GUI Controls

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:  

w3j7IfInDIPxEmO10p59OgK0GfOgqjE4l24s5LhgcvjokrrpVvgmw5cyz46ZOJ5Mg9hYVRsqBv9ArTeMh162H89qI0wVTR7KCs1HrKopV LyElax3zteTkoe9xzGoXGBRp6Flens 1

If I obfuscate with project defaults, I experience a runtime error at app startup:

M9qaSbq0zzNH67NQ8q93qhsfjVsnfEzN7s9G3S2GuTzlV43NHR7y41X2oNSMKYliCM3Kuuuj4T Fz5zc6uIN3pvaLX0HcG1JaOFogSvxCIE0eKvNmynGc2c4kRuJ19uU SBMlRij 1

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:

XTsiMLlWnQxNkbZH Wof4osqEePcylBCJ8OqQ4Wtwr9WXX7YyeBF Z3e2fZ1I oDKGEeOV3ZNZlUXSmL7QKWRgIdvONAz1qV0viWyybYjqgtS xITduqvVXe3 OD2I2J7OHlxlA7 1

After configuring this Rename exclusions, the application starts without the runtime exception, but the “DaysOfWeek” ComboBox appears with obfuscated names:

Screen Shot 2022 02 09 at 8.52.09 AM

In order to fix this, I will configure a Rename exclusion for the members of DaysOfWeek.

LLODxTz4vbezZz3FwOhjKCiU0UirV2Wu5EYLDOsntSyjvASic xvOCCn6OzCxpp200SYb sV4j8Ej6uri66rW2d2dygz8e vvvOrWbNQ9EtHOp4SRyawj1osYeB2N 7ggoxA4Y w 1

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.

Conclusion

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.