SQL Server - How to export assembly from a CLR as DLL and reverse engineer to C # source

Views: 447
Reading Time: 4 minutes

Hello people,
All right with you ?

In this post I will demonstrate how to export a CLR assembly in SQL Server as a DLL and reverse engineer it to C # source code. The first time I needed to use this feature was when there was an assembly already created and compiled in the database and I needed to change the source code of a procedure, but the version that was in the database didn't look the same as it was in Team Foundation Server (TFS) ).

How to export assembly from a bank CLR to DLL

The first step in performing this activity is to export the database assembly to DLL. To do this, you will need to identify the assembly name and run one of the scripts below.

Identifying the assembly name

Exporting assembly to DLL with OLE Automation

One way to export the Assembly to DLL is by using OLE Automation. To do this, use the code below:

Exporting assembly to DLL with CLR

Another way to export the Assembly to DLL is by using the CLR itself.

Example of use:

Result:

Reverse Engineering DLL to Source Code

Once you have successfully exported the assembly from your CLR library to a DLL file, you can reserve engineering the source code. To do this there are several tools, but I suggest the tool JetBrains dotPeek.

Once installed, you can open the dotPeek tool, click on the menu and select the “Open…” option.

Now that your CLR will appear in the list, simply click on the button highlighted in the image below, or right click on the library and select “Export to Project” or go to File> “Export to Project” menu.

Select where you want to save the source code and you can leave the default options checked.

Ready. DotPeek has reverse engineered your DLL and regenerated the original source code as shown below:

It is noteworthy that some formatting, spacing, and comments are not retrieved through reserve engineering as this is lost when the code is compiled. In addition, this technique only applies to DLLs that have not undergone obfuscation (source code encryption).

That's it, guys.
I hope you enjoyed this post.

Hug!

SQL Server database as export save export a CLR assembly as DLL and reverse engineer to source code C # csharp disassembly reverse engineering JetBrains dotPeek

SQL Server database as export save export a CLR assembly as DLL and reverse engineer to source code C # csharp disassembly reverse engineering JetBrains dotPeek